From 624646980c31c981d19b0685e2feea6c9e68cac6 Mon Sep 17 00:00:00 2001 From: brotherlu-xcq <1285823170@qq.com> Date: Wed, 19 May 2021 22:08:48 -0500 Subject: [PATCH] [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 --- .../AddressServerGeneratorManager.java | 4 +- .../AddressServerClusterController.java | 38 +++++++++---------- .../controller/ServerListController.java | 2 +- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java b/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java index 8c41d5e79..990fb8709 100644 --- a/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java +++ b/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java @@ -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; } diff --git a/address/src/main/java/com/alibaba/nacos/address/controller/AddressServerClusterController.java b/address/src/main/java/com/alibaba/nacos/address/controller/AddressServerClusterController.java index 9560f4462..6e1d3e5d3 100644 --- a/address/src/main/java/com/alibaba/nacos/address/controller/AddressServerClusterController.java +++ b/address/src/main/java/com/alibaba/nacos/address/controller/AddressServerClusterController.java @@ -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 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 responseEntity; try { String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName); @@ -133,26 +133,22 @@ 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."); + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + rawProductName + " not found."); + } + if (StringUtils.isBlank(ips)) { + // delete all ips from the cluster + 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); + if (IPUtil.checkOK(checkResult)) { + List instanceList = addressServerGeneratorManager + .generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray); + serviceManager.removeInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, false, + instanceList.toArray(new Instance[instanceList.size()])); } else { - - if (StringUtils.isBlank(ips)) { - // delete all ips from the cluster - responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("ips must not be empty."); - } else { - // delete specified ip list - String[] ipArray = addressServerManager.splitIps(ips); - String checkResult = IPUtil.checkIPs(ipArray); - if (IPUtil.checkOK(checkResult)) { - List instanceList = addressServerGeneratorManager - .generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray); - serviceManager.removeInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, false, - instanceList.toArray(new Instance[instanceList.size()])); - } else { - responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult); - } - } + responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult); } } catch (Exception e) { diff --git a/address/src/main/java/com/alibaba/nacos/address/controller/ServerListController.java b/address/src/main/java/com/alibaba/nacos/address/controller/ServerListController.java index c41ccef94..c310ecb9c 100644 --- a/address/src/main/java/com/alibaba/nacos/address/controller/ServerListController.java +++ b/address/src/main/java/com/alibaba/nacos/address/controller/ServerListController.java @@ -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 getCluster(@PathVariable String product, @PathVariable String cluster) { String productName = addressServerBuilderManager.generateProductName(product); String serviceName = addressServerBuilderManager.generateNacosServiceName(productName);