From a6b9f6c5dc888f1987c90ef656fe3fda32761b28 Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Thu, 10 Jan 2019 22:16:02 +0800 Subject: [PATCH] Polish spring-cloud-incubator/spring-cloud-alibaba#266 --- .../NacosDiscoveryAutoConfiguration.java | 4 +- .../nacos/NacosDiscoveryProperties.java | 704 +++++++++--------- .../NacosAutoServiceRegistration.java | 120 +-- .../nacos/registry/NacosServiceRegistry.java | 129 ++-- 4 files changed, 488 insertions(+), 469 deletions(-) diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java index 34d3dfaf..0a87e58e 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java @@ -45,8 +45,8 @@ import org.springframework.context.annotation.Configuration; public class NacosDiscoveryAutoConfiguration { @Bean - public NacosServiceRegistry nacosServiceRegistry() { - return new NacosServiceRegistry(); + public NacosServiceRegistry nacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) { + return new NacosServiceRegistry(nacosDiscoveryProperties); } @Bean diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java index 86b73bf3..fa6e126c 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java @@ -19,6 +19,7 @@ package org.springframework.cloud.alibaba.nacos; import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.client.naming.utils.UtilAndComs; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -27,366 +28,375 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.commons.util.InetUtils; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; + import javax.annotation.PostConstruct; import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; -import java.util.*; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Properties; -import static com.alibaba.nacos.api.PropertyKeyConst.*; +import static com.alibaba.nacos.api.PropertyKeyConst.ACCESS_KEY; +import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME; +import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT; +import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE; +import static com.alibaba.nacos.api.PropertyKeyConst.NAMING_LOAD_CACHE_AT_START; +import static com.alibaba.nacos.api.PropertyKeyConst.SECRET_KEY; +import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR; /** * @author dungu.zpf * @author xiaojing + * @author Mercy */ - @ConfigurationProperties("spring.cloud.nacos.discovery") public class NacosDiscoveryProperties { - private static final Logger LOGGER = LoggerFactory - .getLogger(NacosDiscoveryProperties.class); - - /** - * nacos discovery server address - */ - private String serverAddr; - - /** - * the domain name of a service, through which the server address can be dynamically - * obtained. - */ - private String endpoint; - - /** - * namespace, separation registry of different environments. - */ - private String namespace; - - /** - * nacos naming log file name - */ - private String logName; - - /** - * service name to registry - */ - @Value("${spring.cloud.nacos.discovery.service:${spring.application.name:}}") - private String service; - - /** - * weight for service instance, the larger the value, the larger the weight. - */ - private float weight = 1; - - /** - * cluster name for nacos server. - */ - private String clusterName = "DEFAULT"; - - /** - * naming load from local cache at application start. true is load - */ - private String namingLoadCacheAtStart = "false"; - - /** - * extra metadata to register. - */ - private Map metadata = new HashMap<>(); - - /** - * if you just want to subscribe, but don't want to register your service, set it to - * false. - */ - private boolean registerEnabled = true; - - /** - * The ip address your want to register for your service instance, needn't to set it - * if the auto detect ip works well. - */ - private String ip; - - /** - * which network interface's ip you want to register - */ - private String networkInterface = ""; - - /** - * The port your want to register for your service instance, needn't to set it if the - * auto detect port works well - */ - private int port = -1; - - /** - * whether your service is a https service - */ - private boolean secure = false; - - /** - * access key for namespace. - */ - private String accessKey; - - /** - * secret key for namespace. - */ - private String secretKey; - - @Autowired - private InetUtils inetUtils; - - @Autowired - private Environment environment; - - private NamingService namingService; - - @PostConstruct - public void init() throws SocketException { - - if (secure) { - metadata.put("secure", "true"); - } - - serverAddr = Objects.toString(serverAddr, ""); - endpoint = Objects.toString(endpoint, ""); - namespace = Objects.toString(namespace, ""); - logName = Objects.toString(logName, ""); - - if (StringUtils.isEmpty(ip)) { - // traversing network interfaces if didn't specify a interface - if (StringUtils.isEmpty(networkInterface)) { - ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); - } - else { - NetworkInterface netInterface = NetworkInterface - .getByName(networkInterface); - if (null == networkInterface) { - throw new IllegalArgumentException( - "no such interface " + networkInterface); - } - - Enumeration inetAddress = netInterface.getInetAddresses(); - while (inetAddress.hasMoreElements()) { - InetAddress currentAddress = inetAddress.nextElement(); - if (currentAddress instanceof Inet4Address - && !currentAddress.isLoopbackAddress()) { - ip = currentAddress.getHostAddress(); - break; - } - } - - if (StringUtils.isEmpty(ip)) { - throw new RuntimeException("cannot find available ip from" - + " network interface " + networkInterface); - } - - } - } - - this.overrideFromEnv(environment); - } - - public String getEndpoint() { - return endpoint; - } - - public void setEndpoint(String endpoint) { - this.endpoint = endpoint; - } - - public String getNamespace() { - return namespace; - } - - public void setNamespace(String namespace) { - this.namespace = namespace; - } - - public String getLogName() { - return logName; - } - - public void setLogName(String logName) { - this.logName = logName; - } - - public void setInetUtils(InetUtils inetUtils) { - this.inetUtils = inetUtils; - } - - public float getWeight() { - return weight; - } - - public void setWeight(float weight) { - this.weight = weight; - } - - public String getClusterName() { - return clusterName; - } - - public void setClusterName(String clusterName) { - this.clusterName = clusterName; - } - - public String getService() { - return service; - } - - public void setService(String service) { - this.service = service; - } - - public boolean isRegisterEnabled() { - return registerEnabled; - } - - public void setRegisterEnabled(boolean registerEnabled) { - this.registerEnabled = registerEnabled; - } - - public String getIp() { - return ip; - } - - public void setIp(String ip) { - this.ip = ip; - } - - public String getNetworkInterface() { - return networkInterface; - } - - public void setNetworkInterface(String networkInterface) { - this.networkInterface = networkInterface; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - public boolean isSecure() { - return secure; - } - - public void setSecure(boolean secure) { - this.secure = secure; - } - - public Map getMetadata() { - return metadata; - } - - public void setMetadata(Map metadata) { - this.metadata = metadata; - } - - public String getServerAddr() { - return serverAddr; - } - - public void setServerAddr(String serverAddr) { - this.serverAddr = serverAddr; - } - - public String getAccessKey() { - return accessKey; - } - - public void setAccessKey(String accessKey) { - this.accessKey = accessKey; - } - - public String getSecretKey() { - return secretKey; - } - - public void setSecretKey(String secretKey) { - this.secretKey = secretKey; - } - - public String getNamingLoadCacheAtStart() { - return namingLoadCacheAtStart; - } - - public void setNamingLoadCacheAtStart(String namingLoadCacheAtStart) { - this.namingLoadCacheAtStart = namingLoadCacheAtStart; - } - - @Override - public String toString() { - return "NacosDiscoveryProperties{" + "serverAddr='" + serverAddr + '\'' - + ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\'' - + ", logName='" + logName + '\'' + ", service='" + service + '\'' - + ", weight=" + weight + ", clusterName='" + clusterName + '\'' - + ", metadata=" + metadata + ", registerEnabled=" + registerEnabled - + ", ip='" + ip + '\'' + ", networkInterface='" + networkInterface + '\'' - + ", port=" + port + ", secure=" + secure + ", accessKey='" + accessKey - + ", namingLoadCacheAtStart=" + namingLoadCacheAtStart + '\'' - + ", secretKey='" + secretKey + '\'' + '}'; - } - - public void overrideFromEnv(Environment env) { - - if (StringUtils.isEmpty(this.getServerAddr())) { - this.setServerAddr(env - .resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}")); - } - if (StringUtils.isEmpty(this.getNamespace())) { - this.setNamespace(env - .resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}")); - } - if (StringUtils.isEmpty(this.getAccessKey())) { - this.setAccessKey(env - .resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}")); - } - if (StringUtils.isEmpty(this.getSecretKey())) { - this.setSecretKey(env - .resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}")); - } - if (StringUtils.isEmpty(this.getLogName())) { - this.setLogName( - env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}")); - } - if (StringUtils.isEmpty(this.getClusterName())) { - this.setClusterName(env.resolvePlaceholders( - "${spring.cloud.nacos.discovery.cluster-name:}")); - } - if (StringUtils.isEmpty(this.getEndpoint())) { - this.setEndpoint( - env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}")); - } - } - - public NamingService namingServiceInstance() { - - if (null != namingService) { - return namingService; - } - - Properties properties = new Properties(); - properties.put(SERVER_ADDR, serverAddr); - properties.put(NAMESPACE, namespace); - properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME, logName); - properties.put(ENDPOINT, endpoint); - properties.put(ACCESS_KEY, accessKey); - properties.put(SECRET_KEY, secretKey); - properties.put(CLUSTER_NAME, clusterName); - properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart); - - try { - namingService = NacosFactory.createNamingService(properties); - return namingService; - } - catch (Exception e) { - LOGGER.error("create naming service error!properties={},e=,", this, e); - return null; - } - } + private static final Logger LOGGER = LoggerFactory + .getLogger(NacosDiscoveryProperties.class); + + /** + * nacos discovery server address + */ + private String serverAddr; + + /** + * the domain name of a service, through which the server address can be dynamically + * obtained. + */ + private String endpoint; + + /** + * namespace, separation registry of different environments. + */ + private String namespace; + + /** + * nacos naming log file name + */ + private String logName; + + /** + * service name to registry + */ + @Value("${spring.cloud.nacos.discovery.service:${spring.application.name:}}") + private String service; + + /** + * weight for service instance, the larger the value, the larger the weight. + */ + private float weight = 1; + + /** + * cluster name for nacos server. + */ + private String clusterName = "DEFAULT"; + + /** + * naming load from local cache at application start. true is load + */ + private String namingLoadCacheAtStart = "false"; + + /** + * extra metadata to register. + */ + private Map metadata = new HashMap<>(); + + /** + * if you just want to subscribe, but don't want to register your service, set it to + * false. + */ + private boolean registerEnabled = true; + + /** + * The ip address your want to register for your service instance, needn't to set it + * if the auto detect ip works well. + */ + private String ip; + + /** + * which network interface's ip you want to register + */ + private String networkInterface = ""; + + /** + * The port your want to register for your service instance, needn't to set it if the + * auto detect port works well + */ + private int port = -1; + + /** + * whether your service is a https service + */ + private boolean secure = false; + + /** + * access key for namespace. + */ + private String accessKey; + + /** + * secret key for namespace. + */ + private String secretKey; + + @Autowired + private InetUtils inetUtils; + + @Autowired + private Environment environment; + + private NamingService namingService; + + @PostConstruct + public void init() throws SocketException { + + if (secure) { + metadata.put("secure", "true"); + } + + serverAddr = Objects.toString(serverAddr, ""); + endpoint = Objects.toString(endpoint, ""); + namespace = Objects.toString(namespace, ""); + logName = Objects.toString(logName, ""); + + if (StringUtils.isEmpty(ip)) { + // traversing network interfaces if didn't specify a interface + if (StringUtils.isEmpty(networkInterface)) { + ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); + } else { + NetworkInterface netInterface = NetworkInterface + .getByName(networkInterface); + if (null == networkInterface) { + throw new IllegalArgumentException( + "no such interface " + networkInterface); + } + + Enumeration inetAddress = netInterface.getInetAddresses(); + while (inetAddress.hasMoreElements()) { + InetAddress currentAddress = inetAddress.nextElement(); + if (currentAddress instanceof Inet4Address + && !currentAddress.isLoopbackAddress()) { + ip = currentAddress.getHostAddress(); + break; + } + } + + if (StringUtils.isEmpty(ip)) { + throw new RuntimeException("cannot find available ip from" + + " network interface " + networkInterface); + } + + } + } + + this.overrideFromEnv(environment); + } + + public String getEndpoint() { + return endpoint; + } + + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + public String getLogName() { + return logName; + } + + public void setLogName(String logName) { + this.logName = logName; + } + + public void setInetUtils(InetUtils inetUtils) { + this.inetUtils = inetUtils; + } + + public float getWeight() { + return weight; + } + + public void setWeight(float weight) { + this.weight = weight; + } + + public String getClusterName() { + return clusterName; + } + + public void setClusterName(String clusterName) { + this.clusterName = clusterName; + } + + public String getService() { + return service; + } + + public void setService(String service) { + this.service = service; + } + + public boolean isRegisterEnabled() { + return registerEnabled; + } + + public void setRegisterEnabled(boolean registerEnabled) { + this.registerEnabled = registerEnabled; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getNetworkInterface() { + return networkInterface; + } + + public void setNetworkInterface(String networkInterface) { + this.networkInterface = networkInterface; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public boolean isSecure() { + return secure; + } + + public void setSecure(boolean secure) { + this.secure = secure; + } + + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + + public String getServerAddr() { + return serverAddr; + } + + public void setServerAddr(String serverAddr) { + this.serverAddr = serverAddr; + } + + public String getAccessKey() { + return accessKey; + } + + public void setAccessKey(String accessKey) { + this.accessKey = accessKey; + } + + public String getSecretKey() { + return secretKey; + } + + public void setSecretKey(String secretKey) { + this.secretKey = secretKey; + } + + public String getNamingLoadCacheAtStart() { + return namingLoadCacheAtStart; + } + + public void setNamingLoadCacheAtStart(String namingLoadCacheAtStart) { + this.namingLoadCacheAtStart = namingLoadCacheAtStart; + } + + @Override + public String toString() { + return "NacosDiscoveryProperties{" + "serverAddr='" + serverAddr + '\'' + + ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\'' + + ", logName='" + logName + '\'' + ", service='" + service + '\'' + + ", weight=" + weight + ", clusterName='" + clusterName + '\'' + + ", metadata=" + metadata + ", registerEnabled=" + registerEnabled + + ", ip='" + ip + '\'' + ", networkInterface='" + networkInterface + '\'' + + ", port=" + port + ", secure=" + secure + ", accessKey='" + accessKey + + ", namingLoadCacheAtStart=" + namingLoadCacheAtStart + '\'' + + ", secretKey='" + secretKey + '\'' + '}'; + } + + public void overrideFromEnv(Environment env) { + + if (StringUtils.isEmpty(this.getServerAddr())) { + this.setServerAddr(env + .resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}")); + } + if (StringUtils.isEmpty(this.getNamespace())) { + this.setNamespace(env + .resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}")); + } + if (StringUtils.isEmpty(this.getAccessKey())) { + this.setAccessKey(env + .resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}")); + } + if (StringUtils.isEmpty(this.getSecretKey())) { + this.setSecretKey(env + .resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}")); + } + if (StringUtils.isEmpty(this.getLogName())) { + this.setLogName( + env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}")); + } + if (StringUtils.isEmpty(this.getClusterName())) { + this.setClusterName(env.resolvePlaceholders( + "${spring.cloud.nacos.discovery.cluster-name:}")); + } + if (StringUtils.isEmpty(this.getEndpoint())) { + this.setEndpoint( + env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}")); + } + } + + public NamingService namingServiceInstance() { + + if (null != namingService) { + return namingService; + } + + Properties properties = new Properties(); + properties.put(SERVER_ADDR, serverAddr); + properties.put(NAMESPACE, namespace); + properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME, logName); + properties.put(ENDPOINT, endpoint); + properties.put(ACCESS_KEY, accessKey); + properties.put(SECRET_KEY, secretKey); + properties.put(CLUSTER_NAME, clusterName); + properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart); + + try { + namingService = NacosFactory.createNamingService(properties); + } catch (Exception e) { + LOGGER.error("create naming service error!properties={},e=,", this, e); + return null; + } + return namingService; + } } diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java index ec60041c..24b0a848 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistration.java @@ -20,83 +20,85 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; +import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** * @author xiaojing + * @author Mercy */ public class NacosAutoServiceRegistration - extends AbstractAutoServiceRegistration { - private static final Logger LOGGER = LoggerFactory - .getLogger(NacosAutoServiceRegistration.class); + extends AbstractAutoServiceRegistration { + private static final Logger LOGGER = LoggerFactory + .getLogger(NacosAutoServiceRegistration.class); - private NacosRegistration registration; + private NacosRegistration registration; - public NacosAutoServiceRegistration( - ServiceRegistry serviceRegistry, - AutoServiceRegistrationProperties autoServiceRegistrationProperties, - NacosRegistration registration) { - super(serviceRegistry, autoServiceRegistrationProperties); - this.registration = registration; - } + public NacosAutoServiceRegistration( + ServiceRegistry serviceRegistry, + AutoServiceRegistrationProperties autoServiceRegistrationProperties, + NacosRegistration registration) { + super(serviceRegistry, autoServiceRegistrationProperties); + this.registration = registration; + } - @Deprecated - public void setPort(int port) { - getPort().set(port); - } + @Deprecated + public void setPort(int port) { + getPort().set(port); + } - @Override - protected NacosRegistration getRegistration() { - if (this.registration.getPort() < 0 && this.getPort().get() > 0) { - this.registration.setPort(this.getPort().get()); - } - Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set"); - return this.registration; - } + @Override + protected NacosRegistration getRegistration() { + if (this.registration.getPort() < 0 && this.getPort().get() > 0) { + this.registration.setPort(this.getPort().get()); + } + Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set"); + return this.registration; + } - @Override - protected NacosRegistration getManagementRegistration() { - return null; - } + @Override + protected NacosRegistration getManagementRegistration() { + return null; + } - @Override - protected void register() { - if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) { - LOGGER.debug("Registration disabled."); - return; - } - if (this.registration.getPort() < 0) { - this.registration.setPort(getPort().get()); - } - super.register(); - } + @Override + protected void register() { + if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) { + LOGGER.debug("Registration disabled."); + return; + } + if (this.registration.getPort() < 0) { + this.registration.setPort(getPort().get()); + } + super.register(); + } - @Override - protected void registerManagement() { - if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) { - return; - } - super.registerManagement(); + @Override + protected void registerManagement() { + if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) { + return; + } + super.registerManagement(); - } + } - @Override - protected Object getConfiguration() { - return this.registration.getNacosDiscoveryProperties(); - } + @Override + protected Object getConfiguration() { + return this.registration.getNacosDiscoveryProperties(); + } - @Override - protected boolean isEnabled() { - return this.registration.getNacosDiscoveryProperties().isRegisterEnabled(); - } + @Override + protected boolean isEnabled() { + return this.registration.getNacosDiscoveryProperties().isRegisterEnabled(); + } - @Override - @SuppressWarnings("deprecation") - protected String getAppName() { - String appName = registration.getNacosDiscoveryProperties().getService(); - return StringUtils.isEmpty(appName) ? super.getAppName() : appName; - } + @Override + @SuppressWarnings("deprecation") + protected String getAppName() { + String appName = registration.getNacosDiscoveryProperties().getService(); + return StringUtils.isEmpty(appName) ? super.getAppName() : appName; + } } diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosServiceRegistry.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosServiceRegistry.java index f3850a18..121d9f9e 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosServiceRegistry.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosServiceRegistry.java @@ -18,90 +18,97 @@ package org.springframework.cloud.alibaba.nacos.registry; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; +import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.util.StringUtils; /** * @author xiaojing + * @author Mercy */ -public class NacosServiceRegistry implements ServiceRegistry { +public class NacosServiceRegistry implements ServiceRegistry { - private static Logger logger = LoggerFactory.getLogger(NacosServiceRegistry.class); + private static Logger logger = LoggerFactory.getLogger(NacosServiceRegistry.class); - @Override - public void register(NacosRegistration registration) { + private final NacosDiscoveryProperties nacosDiscoveryProperties; - if (!registration.isRegisterEnabled()) { - logger.info("Nacos Registration is disabled..."); - return; - } - if (StringUtils.isEmpty(registration.getServiceId())) { - logger.info("No service to register for nacos client..."); - return; - } + private final NamingService namingService; - NamingService namingService = registration.getNacosNamingService(); - String serviceId = registration.getServiceId(); + public NacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) { + this.nacosDiscoveryProperties = nacosDiscoveryProperties; + this.namingService = nacosDiscoveryProperties.namingServiceInstance(); + } - Instance instance = new Instance(); - instance.setIp(registration.getHost()); - instance.setPort(registration.getPort()); - instance.setWeight(registration.getRegisterWeight()); - instance.setClusterName(registration.getCluster()); - instance.setMetadata(registration.getMetadata()); - try { - namingService.registerInstance(serviceId, instance); - logger.info("nacos registry, {} {}:{} register finished", serviceId, - instance.getIp(), instance.getPort()); - } - catch (Exception e) { - logger.error("nacos registry, {} register failed...{},", serviceId, - registration.toString(), e); - } - } + @Override + public void register(Registration registration) { - @Override - public void deregister(NacosRegistration registration) { + if (StringUtils.isEmpty(registration.getServiceId())) { + logger.info("No service to register for nacos client..."); + return; + } - logger.info("De-registering from Nacos Server now..."); + String serviceId = registration.getServiceId(); - if (StringUtils.isEmpty(registration.getServiceId())) { - logger.info("No dom to de-register for nacos client..."); - return; - } + Instance instance = new Instance(); + instance.setIp(registration.getHost()); + instance.setPort(registration.getPort()); + instance.setWeight(nacosDiscoveryProperties.getWeight()); + instance.setClusterName(nacosDiscoveryProperties.getClusterName()); + instance.setMetadata(registration.getMetadata()); - NamingService namingService = registration.getNacosNamingService(); - String serviceId = registration.getServiceId(); + try { + namingService.registerInstance(serviceId, instance); + logger.info("nacos registry, {} {}:{} register finished", serviceId, + instance.getIp(), instance.getPort()); + } catch (Exception e) { + logger.error("nacos registry, {} register failed...{},", serviceId, + registration.toString(), e); + } + } - try { - namingService.deregisterInstance(serviceId, registration.getHost(), - registration.getPort(), registration.getCluster()); - } - catch (Exception e) { - logger.error("ERR_NACOS_DEREGISTER, de-register failed...{},", - registration.toString(), e); - } + @Override + public void deregister(Registration registration) { - logger.info("De-registration finished."); - } + logger.info("De-registering from Nacos Server now..."); - @Override - public void close() { + if (StringUtils.isEmpty(registration.getServiceId())) { + logger.info("No dom to de-register for nacos client..."); + return; + } - } + NamingService namingService = nacosDiscoveryProperties.namingServiceInstance(); + String serviceId = registration.getServiceId(); - @Override - public void setStatus(NacosRegistration registration, String status) { - // nacos doesn't support set status of a particular registration. - } + try { + namingService.deregisterInstance(serviceId, registration.getHost(), + registration.getPort(), nacosDiscoveryProperties.getClusterName()); + } catch (Exception e) { + logger.error("ERR_NACOS_DEREGISTER, de-register failed...{},", + registration.toString(), e); + } - @Override - public T getStatus(NacosRegistration registration) { - // nacos doesn't support query status of a particular registration. - return null; - } + logger.info("De-registration finished."); + } + + @Override + public void close() { + + } + + @Override + public void setStatus(Registration registration, String status) { + // nacos doesn't support set status of a particular registration. + } + + @Override + public T getStatus(Registration registration) { + // nacos doesn't support query status of a particular registration. + return null; + } }