[ISSUE #2992] Use new code style for nacos-address module. (#3239)

* reformat with new code style for nacos-address

* Use new code style for nacos-address module.
This commit is contained in:
杨翊 SionYang 2020-07-03 17:30:31 +08:00 committed by GitHub
parent 1a0df78d8a
commit 8c9622fbda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 257 additions and 235 deletions

View File

@ -15,7 +15,7 @@
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.address;
import org.springframework.boot.SpringApplication;
@ -26,6 +27,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
*/
@SpringBootApplication(scanBasePackages = "com.alibaba.nacos")
public class AddressServer {
public static void main(String[] args) {
SpringApplication.run(AddressServer.class, args);

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.address.component;
import com.alibaba.nacos.address.constant.AddressServerConstants;
@ -35,6 +36,12 @@ import java.util.List;
@Component
public class AddressServerGeneratorManager {
/**
* Generate product name.
*
* @param name name
* @return product
*/
public String generateProductName(String name) {
if (StringUtils.isBlank(name) || AddressServerConstants.DEFAULT_PRODUCT.equals(name)) {
@ -48,15 +55,15 @@ public class AddressServerGeneratorManager {
/**
* Note: if the parameter inputted is empty then will return the empty list.
*
* @param serviceName
* @param clusterName
* @param ipArray
* @return
* @param serviceName service name
* @param clusterName cluster name
* @param ipArray array of ips
* @return instance list
*/
public List<Instance> generateInstancesByIps(String serviceName, String rawProductName, String clusterName, String[] ipArray) {
if (StringUtils.isEmpty(serviceName)
|| StringUtils.isEmpty(clusterName)
|| ipArray == null || ipArray.length == 0) {
public List<Instance> generateInstancesByIps(String serviceName, String rawProductName, String clusterName,
String[] ipArray) {
if (StringUtils.isEmpty(serviceName) || StringUtils.isEmpty(clusterName) || ipArray == null
|| ipArray.length == 0) {
return Collections.emptyList();
}
@ -77,18 +84,20 @@ public class AddressServerGeneratorManager {
return instanceList;
}
public String[] generateIpAndPort(String ip) {
private String[] generateIpAndPort(String ip) {
int index = ip.indexOf(AddressServerConstants.IP_PORT_SEPARATOR);
if (index != -1) {
return new String[]{ip.substring(0, index), ip.substring(index + 1)};
return new String[] {ip.substring(0, index), ip.substring(index + 1)};
}
return new String[]{ip, String.valueOf(AddressServerConstants.DEFAULT_SERVER_PORT)};
return new String[] {ip, String.valueOf(AddressServerConstants.DEFAULT_SERVER_PORT)};
}
/**
* Generate response ips.
*
* @param instanceList a instance set will generate string response to client.
* @return the result of response to client
*/
@ -104,7 +113,9 @@ public class AddressServerGeneratorManager {
}
/**
* @param rawServiceName the raw service name will not contains the {@Constans.DEFAULT_GROUP}
* Generate nacos service name.
*
* @param rawServiceName the raw service name will not contains the {@link Constants#DEFAULT_GROUP}.
* @return the nacos service name
*/
public String generateNacosServiceName(String rawServiceName) {

View File

@ -13,9 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.address.component;
import com.alibaba.nacos.address.constant.AddressServerConstants;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
@ -40,13 +42,11 @@ public class AddressServerManager {
}
/**
* <p>
* if the name is empty then return the default {@UtilAndCommons#DEFAULT_CLUSTER_NAME},
* <p>
* or return the source name by input
* If the name is empty then return the default {@link UtilsAndCommons#DEFAULT_CLUSTER_NAME}, or return the source
* name by input.
*
* @param name
* @return
* @param name name
* @return default cluster name
*/
public String getDefaultClusterNameIfEmpty(String name) {
@ -63,8 +63,10 @@ public class AddressServerManager {
}
/**
* Split ips.
*
* @param ips multi ip will separator by the ','
* @return
* @return array of ip
*/
public String[] splitIps(String ips) {

View File

@ -13,12 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.address.constant;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
/**
* Uniform constant parameter naming for address servers and default values for related parameters
* Uniform constant parameter naming for address servers and default values for related parameters.
*
* @author pbting
* @date 2019-06-17 7:23 PM
@ -43,7 +44,7 @@ public interface AddressServerConstants {
String IP_PORT_SEPARATOR = ":";
/**
* the separator for {@Service#name} between raw service name and group
* the separator for service name between raw service name and group.
*/
String GROUP_SERVICE_NAME_SEP = "@@";
@ -53,24 +54,24 @@ public interface AddressServerConstants {
String DEFAULT_GET_CLUSTER = "serverlist";
/**
* post multi ip will use the "," to separator
* post multi ip will use the "," to separator.
*/
String MULTI_IPS_SEPARATOR = ",";
/**
* the default product name when deploy nacos with naming and config
* the default product name when deploy nacos with naming and config.
*/
String ALIWARE_NACOS_DEFAULT_PRODUCT_NAME = "nacos.as.default";
/**
* when the config and naming will separate deploy,then must specify product name by the client
* when the config and naming will separate deploy,then must specify product name by the client.
*/
String ALIWARE_NACOS_PRODUCT_DOM_TEMPLATE = "nacos.as.%s";
/**
* the url for address server prefix
* the url for address server prefix.
*/
String ADDRESS_SERVER_REQUEST_URL =
UtilsAndCommons.NACOS_SERVER_CONTEXT + UtilsAndCommons.NACOS_SERVER_VERSION + "/as";
UtilsAndCommons.NACOS_SERVER_CONTEXT + UtilsAndCommons.NACOS_SERVER_VERSION + "/as";
}

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.address.controller;
import com.alibaba.nacos.address.component.AddressServerGeneratorManager;
@ -38,8 +39,9 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Address server cluster controller.
*
* @author pbting
* @date 2019-06-10 9:59 AM
* @since 1.1.0
*/
@RestController
@ -56,15 +58,16 @@ public class AddressServerClusterController {
private AddressServerGeneratorManager addressServerGeneratorManager;
/**
* Create new cluster.
*
* @param product Ip list of products to be associated
* @param cluster Ip list of product cluster to be associated
* @param ips will post ip list.
* @return
* @return result of create new cluster
*/
@RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity postCluster(@RequestParam(required = false) String product,
@RequestParam(required = false) String cluster,
@RequestParam(name = "ips") String ips) {
@RequestParam(required = false) String cluster, @RequestParam(name = "ips") String ips) {
//1. prepare the storage name for product and cluster
String productName = addressServerGeneratorManager.generateProductName(product);
@ -73,7 +76,8 @@ public class AddressServerClusterController {
//2. prepare the response name for product and cluster to client
String rawProductName = addressServerManager.getRawProductName(product);
String rawClusterName = addressServerManager.getRawClusterName(cluster);
Loggers.addressLogger.info("put cluster node,the cluster name is " + cluster + "; the product name=" + product + "; the ip list=" + ips);
Loggers.ADDRESS_LOGGER.info("put cluster node,the cluster name is " + cluster + "; the product name=" + product
+ "; the ip list=" + ips);
ResponseEntity responseEntity;
try {
String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName);
@ -85,11 +89,14 @@ public class AddressServerClusterController {
String[] ipArray = addressServerManager.splitIps(ips);
String checkResult = AddressServerParamCheckUtil.checkIps(ipArray);
if (AddressServerParamCheckUtil.CHECK_OK.equals(checkResult)) {
List<Instance> instanceList = addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray);
List<Instance> instanceList = addressServerGeneratorManager
.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray);
for (Instance instance : instanceList) {
serviceManager.registerInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, instance);
}
responseEntity = ResponseEntity.ok("product=" + rawProductName + ",cluster=" + rawClusterName + "; put success with size=" + instanceList.size());
responseEntity = ResponseEntity
.ok("product=" + rawProductName + ",cluster=" + rawClusterName + "; put success with size="
+ instanceList.size());
} else {
responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult);
}
@ -101,15 +108,16 @@ public class AddressServerClusterController {
}
/**
* Delete cluster.
*
* @param product Ip list of products to be associated
* @param cluster Ip list of product cluster to be associated
* @param ips will delete ips.
* @return
* @return delete result
*/
@RequestMapping(value = "", method = RequestMethod.DELETE)
public ResponseEntity deleteCluster(@RequestParam(required = false) String product,
@RequestParam(required = false) String cluster,
@RequestParam String ips) {
@RequestParam(required = false) String cluster, @RequestParam String ips) {
//1. prepare the storage name for product and cluster
String productName = addressServerGeneratorManager.generateProductName(product);
String clusterName = addressServerManager.getDefaultClusterNameIfEmpty(cluster);
@ -117,14 +125,16 @@ public class AddressServerClusterController {
//2. prepare the response name for product and cluster to client
String rawProductName = addressServerManager.getRawProductName(product);
String rawClusterName = addressServerManager.getRawClusterName(cluster);
ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.OK).body("product=" + rawProductName + ", cluster=" + rawClusterName + " delete success.");
ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.OK)
.body("product=" + rawProductName + ", cluster=" + rawClusterName + " delete success.");
try {
String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName);
Service service = serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName);
if (service == null) {
responseEntity = ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + rawProductName + " not found.");
responseEntity = ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("product=" + rawProductName + " not found.");
} else {
if (StringUtils.isBlank(ips)) {
@ -135,8 +145,10 @@ public class AddressServerClusterController {
String[] ipArray = addressServerManager.splitIps(ips);
String checkResult = AddressServerParamCheckUtil.checkIps(ipArray);
if (AddressServerParamCheckUtil.CHECK_OK.equals(checkResult)) {
List<Instance> instanceList = addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray);
serviceManager.removeInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, false, instanceList.toArray(new Instance[instanceList.size()]));
List<Instance> 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);
}

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.address.controller;
import com.alibaba.nacos.address.component.AddressServerGeneratorManager;
@ -29,8 +30,9 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* Server list controller.
*
* @author pbting
* @date 2019-06-18 5:04 PM
* @since 1.1.0
*/
@RestController
@ -43,13 +45,14 @@ public class ServerListController {
private AddressServerGeneratorManager addressServerBuilderManager;
/**
* Get cluster.
*
* @param product will get Ip list of that products to be associated
* @param cluster will get Ip list of that product cluster to be associated
* @return
* @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);
@ -61,10 +64,12 @@ public class ServerListController {
if (!service.getClusterMap().containsKey(cluster)) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + product + ",cluster=" + cluster + " not found.");
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("product=" + product + ",cluster=" + cluster + " not found.");
}
Cluster clusterObj = service.getClusterMap().get(cluster);
return ResponseEntity.status(HttpStatus.OK).body(addressServerBuilderManager.generateResponseIps(clusterObj.allIPs(false)));
return ResponseEntity.status(HttpStatus.OK)
.body(addressServerBuilderManager.generateResponseIps(clusterObj.allIPs(false)));
}
}

View File

@ -13,16 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.address.misc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Loggers holder.
*
* @author pbting
* @date 2019-07-04 4:34 PM
*/
public class Loggers {
public static final Logger addressLogger = LoggerFactory.getLogger("com.alibaba.nacos.address.main");
public static final Logger ADDRESS_LOGGER = LoggerFactory.getLogger("com.alibaba.nacos.address.main");
}

View File

@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.address.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Provides a unified tool class for address server parameter verification
* Provides a unified tool class for address server parameter verification.
*
* @author pbting
* @date 2019-06-19 11:19 AM
@ -35,6 +36,12 @@ public class AddressServerParamCheckUtil {
private static final Pattern IP_PATTERN = Pattern.compile(IP_REGEX);
/**
* Check ips.
*
* @param ips ips
* @return 'ok' if check passed, otherwise illegal ip
*/
public static String checkIps(String... ips) {
if (ips == null || ips.length == 0) {

View File

@ -21,7 +21,7 @@
<property name="LOG_HOME" value="${logPath}"/>
<appender name="nacos-address"
class="ch.qos.logback.core.rolling.RollingFileAppender">
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/nacos-address.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">

View File

@ -13,6 +13,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
server.port=8080
server.servlet.context-path=/

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.address;
import org.junit.Ignore;
@ -20,25 +21,21 @@ import org.junit.Test;
import java.util.HashMap;
/**
* @author pbting
* @date 2019-06-18 2:37 PM
*/
@Ignore
public class AddressServerControllerTests {
private static final String PRODUCT_NACOS = "nacos";
private static final String PRODUCT_CONFIG = "config";
private static final String PRODUCT_NAMING = "naming";
private static final String DEFAULT_URL_CLUSTER = "serverlist";
private static final String GET_SERVERLIST_URL_FORMART = "http://127.0.0.1:8080/%s/%s";
//-----------------product=nacos,cluster=DEFAULT -------------------//
/**
* test the default product and cluster
*/
@Test
public void postCluster() {
@ -85,10 +82,6 @@ public class AddressServerControllerTests {
//-----------------product=config,cluster=cluster01 -------------------//
/**
* test with product
*/
@Test
public void postClusterWithProduct() {
@ -136,12 +129,8 @@ public class AddressServerControllerTests {
System.err.println(response);
}
//-----------------product=naming,cluster=cluster01 -------------------//
/**
* test with product and cluster
*/
@Test
public void postClusterWithProductAndCluster() {

View File

@ -13,15 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.address;
import com.alibaba.nacos.address.util.AddressServerParamCheckUtil;
import org.junit.Test;
/**
* @author pbting
* @date 2019-06-19 11:31 AM
*/
public class ParamCheckUtilTests {
@Test

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.address;
import com.alibaba.nacos.common.utils.IoUtils;
@ -26,49 +27,46 @@ import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
/**
* @author pbting
* @date 2019-06-18 2:40 PM
*/
public class SimpleHttpTestUtils {
private static final String REQUEST_METHOD_DELETE = "DELETE";
private static final String REQUEST_METHOD_PUT = "PUT";
private static final String REQUEST_METHOD_POST = "POST";
private static final String REQUEST_METHOD_GET = "GET";
/**
* 连接超时
* 连接超时.
*/
private static int CONNECT_TIME_OUT = 2000;
private static final int CONNECT_TIME_OUT = 2000;
/**
* 读取数据超时
* 读取数据超时.
*/
private static int READ_TIME_OUT = 2000;
private static final int READ_TIME_OUT = 2000;
/**
* 请求编码
* 请求编码.
*/
public static String REQUEST_ENCODING = "UTF-8";
public static final String REQUEST_ENCODING = "UTF-8";
/**
* 接收编码
* 接收编码.
*/
public static String RESPONSE_ENCODING = "UTF-8";
public static final String RESPONSE_ENCODING = "UTF-8";
public static final short OK = 200;
public static final short Bad_Request = 400;
public static final short BAD_REQUEST = 400;
public static final short Internal_Server_Error = 500;
public static final short INTERNAL_SERVER_ERROR = 500;
public static final short PARAM_ERROR_NO_ANALYSESOR = 1000;
/**
* <pre>
* 发送带参数的GET的HTTP请求
* </pre>
* 发送带参数的GET的HTTP请求.
*
* @param reqUrl HTTP请求URL
* @param paramMap 参数映射表
@ -79,9 +77,7 @@ public class SimpleHttpTestUtils {
}
/**
* <pre>
* 发送带参数的POST的HTTP请求
* </pre>
* 发送带参数的POST的HTTP请求.
*
* @param reqUrl HTTP请求URL
* @param paramMap 参数映射表
@ -92,9 +88,7 @@ public class SimpleHttpTestUtils {
}
/**
* <pre>
* 发送带参数的 PUT HTTP 请求
* </pre>
* 发送带参数的 PUT HTTP 请求.
*
* @param reqUrl HTTP请求URL
* @param paramMap 参数映射表
@ -105,9 +99,7 @@ public class SimpleHttpTestUtils {
}
/**
* <pre>
* 发送带参数的 DELETE HTTP 请求
* </pre>
* 发送带参数的 DELETE HTTP 请求.
*
* @param reqUrl HTTP请求URL
* @param paramMap 参数映射表
@ -117,12 +109,14 @@ public class SimpleHttpTestUtils {
return doRequest(reqUrl, paramMap, REQUEST_METHOD_DELETE, recvEncoding);
}
private static String doRequest(String reqUrl, Map<String, String> paramMap, String reqMethod, String recvEncoding) {
private static String doRequest(String reqUrl, Map<String, String> paramMap, String reqMethod,
String recvEncoding) {
return doExecute(reqUrl, paramMap, reqMethod, recvEncoding);
}
private static String doExecute(String reqUrl, Map<String, String> paramMap, String reqMethod, String recvEncoding) {
private static String doExecute(String reqUrl, Map<String, String> paramMap, String reqMethod,
String recvEncoding) {
HttpURLConnection urlCon = null;
String responseContent = null;
try {
@ -139,8 +133,8 @@ public class SimpleHttpTestUtils {
params = params.deleteCharAt(params.length() - 1);
}
if (params.length() > 0 &&
(REQUEST_METHOD_GET.equals(reqMethod) || REQUEST_METHOD_DELETE.equals(reqMethod))) {
if (params.length() > 0 && (REQUEST_METHOD_GET.equals(reqMethod) || REQUEST_METHOD_DELETE
.equals(reqMethod))) {
reqUrl = reqUrl + "?" + params.toString();
}
}