[ISSUE #5766] improve the code quality of nacos-address (#5790)

* [code quality] [nacos-config] [all content] fix the ResponseEntity, the if nest optimize.

* [code quality] [nacos-address] [utils] rollback logic
This commit is contained in:
brotherlu-xcq 2021-05-19 22:08:48 -05:00 committed by GitHub
parent 35c8035861
commit 624646980c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 24 deletions

View File

@ -103,7 +103,7 @@ public class AddressServerGeneratorManager {
StringBuilder ips = new StringBuilder();
instanceList.forEach(instance -> {
ips.append(instance.getIp() + ":" + instance.getPort());
ips.append(instance.getIp()).append(":").append(instance.getPort());
ips.append("\n");
});
@ -118,7 +118,7 @@ public class AddressServerGeneratorManager {
*/
public String generateNacosServiceName(String rawServiceName) {
if (rawServiceName.indexOf(Constants.DEFAULT_GROUP) != -1) {
if (rawServiceName.contains(Constants.DEFAULT_GROUP)) {
return rawServiceName;
}

View File

@ -66,7 +66,7 @@ public class AddressServerClusterController {
* @return result of create new cluster
*/
@RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity postCluster(@RequestParam(required = false) String product,
public ResponseEntity<String> postCluster(@RequestParam(required = false) String product,
@RequestParam(required = false) String cluster, @RequestParam(name = "ips") String ips) {
//1. prepare the storage name for product and cluster
@ -78,7 +78,7 @@ public class AddressServerClusterController {
String rawClusterName = addressServerManager.getRawClusterName(cluster);
Loggers.ADDRESS_LOGGER.info("put cluster node,the cluster name is " + cluster + "; the product name=" + product
+ "; the ip list=" + ips);
ResponseEntity responseEntity;
ResponseEntity<String> responseEntity;
try {
String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName);
@ -133,14 +133,12 @@ public class AddressServerClusterController {
Service service = serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName);
if (service == null) {
responseEntity = ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("product=" + rawProductName + " not found.");
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + rawProductName + " not found.");
}
if (StringUtils.isBlank(ips)) {
// delete all ips from the cluster
responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("ips must not be empty.");
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("ips must not be empty.");
}
// delete specified ip list
String[] ipArray = addressServerManager.splitIps(ips);
String checkResult = IPUtil.checkIPs(ipArray);
@ -152,8 +150,6 @@ public class AddressServerClusterController {
} else {
responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult);
}
}
}
} catch (Exception e) {
responseEntity = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getCause());

View File

@ -52,7 +52,7 @@ public class ServerListController {
* @return result of get
*/
@RequestMapping(value = "/{product}/{cluster}", method = RequestMethod.GET)
public ResponseEntity getCluster(@PathVariable String product, @PathVariable String cluster) {
public ResponseEntity<String> getCluster(@PathVariable String product, @PathVariable String cluster) {
String productName = addressServerBuilderManager.generateProductName(product);
String serviceName = addressServerBuilderManager.generateNacosServiceName(productName);