mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
This commit is contained in:
parent
571d0eca15
commit
a6b9f6c5dc
@ -45,8 +45,8 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
public class NacosDiscoveryAutoConfiguration {
|
public class NacosDiscoveryAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public NacosServiceRegistry nacosServiceRegistry() {
|
public NacosServiceRegistry nacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
|
||||||
return new NacosServiceRegistry();
|
return new NacosServiceRegistry(nacosDiscoveryProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -19,6 +19,7 @@ package org.springframework.cloud.alibaba.nacos;
|
|||||||
import com.alibaba.nacos.api.NacosFactory;
|
import com.alibaba.nacos.api.NacosFactory;
|
||||||
import com.alibaba.nacos.api.naming.NamingService;
|
import com.alibaba.nacos.api.naming.NamingService;
|
||||||
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.cloud.commons.util.InetUtils;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.NetworkInterface;
|
import java.net.NetworkInterface;
|
||||||
import java.net.SocketException;
|
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 dungu.zpf
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ConfigurationProperties("spring.cloud.nacos.discovery")
|
@ConfigurationProperties("spring.cloud.nacos.discovery")
|
||||||
public class NacosDiscoveryProperties {
|
public class NacosDiscoveryProperties {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory
|
private static final Logger LOGGER = LoggerFactory
|
||||||
.getLogger(NacosDiscoveryProperties.class);
|
.getLogger(NacosDiscoveryProperties.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* nacos discovery server address
|
* nacos discovery server address
|
||||||
*/
|
*/
|
||||||
private String serverAddr;
|
private String serverAddr;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the domain name of a service, through which the server address can be dynamically
|
* the domain name of a service, through which the server address can be dynamically
|
||||||
* obtained.
|
* obtained.
|
||||||
*/
|
*/
|
||||||
private String endpoint;
|
private String endpoint;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* namespace, separation registry of different environments.
|
* namespace, separation registry of different environments.
|
||||||
*/
|
*/
|
||||||
private String namespace;
|
private String namespace;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* nacos naming log file name
|
* nacos naming log file name
|
||||||
*/
|
*/
|
||||||
private String logName;
|
private String logName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* service name to registry
|
* service name to registry
|
||||||
*/
|
*/
|
||||||
@Value("${spring.cloud.nacos.discovery.service:${spring.application.name:}}")
|
@Value("${spring.cloud.nacos.discovery.service:${spring.application.name:}}")
|
||||||
private String service;
|
private String service;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* weight for service instance, the larger the value, the larger the weight.
|
* weight for service instance, the larger the value, the larger the weight.
|
||||||
*/
|
*/
|
||||||
private float weight = 1;
|
private float weight = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* cluster name for nacos server.
|
* cluster name for nacos server.
|
||||||
*/
|
*/
|
||||||
private String clusterName = "DEFAULT";
|
private String clusterName = "DEFAULT";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* naming load from local cache at application start. true is load
|
* naming load from local cache at application start. true is load
|
||||||
*/
|
*/
|
||||||
private String namingLoadCacheAtStart = "false";
|
private String namingLoadCacheAtStart = "false";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* extra metadata to register.
|
* extra metadata to register.
|
||||||
*/
|
*/
|
||||||
private Map<String, String> metadata = new HashMap<>();
|
private Map<String, String> metadata = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* if you just want to subscribe, but don't want to register your service, set it to
|
* if you just want to subscribe, but don't want to register your service, set it to
|
||||||
* false.
|
* false.
|
||||||
*/
|
*/
|
||||||
private boolean registerEnabled = true;
|
private boolean registerEnabled = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ip address your want to register for your service instance, needn't to set it
|
* The ip address your want to register for your service instance, needn't to set it
|
||||||
* if the auto detect ip works well.
|
* if the auto detect ip works well.
|
||||||
*/
|
*/
|
||||||
private String ip;
|
private String ip;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* which network interface's ip you want to register
|
* which network interface's ip you want to register
|
||||||
*/
|
*/
|
||||||
private String networkInterface = "";
|
private String networkInterface = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The port your want to register for your service instance, needn't to set it if the
|
* The port your want to register for your service instance, needn't to set it if the
|
||||||
* auto detect port works well
|
* auto detect port works well
|
||||||
*/
|
*/
|
||||||
private int port = -1;
|
private int port = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* whether your service is a https service
|
* whether your service is a https service
|
||||||
*/
|
*/
|
||||||
private boolean secure = false;
|
private boolean secure = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* access key for namespace.
|
* access key for namespace.
|
||||||
*/
|
*/
|
||||||
private String accessKey;
|
private String accessKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* secret key for namespace.
|
* secret key for namespace.
|
||||||
*/
|
*/
|
||||||
private String secretKey;
|
private String secretKey;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private InetUtils inetUtils;
|
private InetUtils inetUtils;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Environment environment;
|
private Environment environment;
|
||||||
|
|
||||||
private NamingService namingService;
|
private NamingService namingService;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() throws SocketException {
|
public void init() throws SocketException {
|
||||||
|
|
||||||
if (secure) {
|
if (secure) {
|
||||||
metadata.put("secure", "true");
|
metadata.put("secure", "true");
|
||||||
}
|
}
|
||||||
|
|
||||||
serverAddr = Objects.toString(serverAddr, "");
|
serverAddr = Objects.toString(serverAddr, "");
|
||||||
endpoint = Objects.toString(endpoint, "");
|
endpoint = Objects.toString(endpoint, "");
|
||||||
namespace = Objects.toString(namespace, "");
|
namespace = Objects.toString(namespace, "");
|
||||||
logName = Objects.toString(logName, "");
|
logName = Objects.toString(logName, "");
|
||||||
|
|
||||||
if (StringUtils.isEmpty(ip)) {
|
if (StringUtils.isEmpty(ip)) {
|
||||||
// traversing network interfaces if didn't specify a interface
|
// traversing network interfaces if didn't specify a interface
|
||||||
if (StringUtils.isEmpty(networkInterface)) {
|
if (StringUtils.isEmpty(networkInterface)) {
|
||||||
ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
|
ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
|
||||||
}
|
} else {
|
||||||
else {
|
NetworkInterface netInterface = NetworkInterface
|
||||||
NetworkInterface netInterface = NetworkInterface
|
.getByName(networkInterface);
|
||||||
.getByName(networkInterface);
|
if (null == networkInterface) {
|
||||||
if (null == networkInterface) {
|
throw new IllegalArgumentException(
|
||||||
throw new IllegalArgumentException(
|
"no such interface " + networkInterface);
|
||||||
"no such interface " + networkInterface);
|
}
|
||||||
}
|
|
||||||
|
Enumeration<InetAddress> inetAddress = netInterface.getInetAddresses();
|
||||||
Enumeration<InetAddress> inetAddress = netInterface.getInetAddresses();
|
while (inetAddress.hasMoreElements()) {
|
||||||
while (inetAddress.hasMoreElements()) {
|
InetAddress currentAddress = inetAddress.nextElement();
|
||||||
InetAddress currentAddress = inetAddress.nextElement();
|
if (currentAddress instanceof Inet4Address
|
||||||
if (currentAddress instanceof Inet4Address
|
&& !currentAddress.isLoopbackAddress()) {
|
||||||
&& !currentAddress.isLoopbackAddress()) {
|
ip = currentAddress.getHostAddress();
|
||||||
ip = currentAddress.getHostAddress();
|
break;
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
if (StringUtils.isEmpty(ip)) {
|
||||||
if (StringUtils.isEmpty(ip)) {
|
throw new RuntimeException("cannot find available ip from"
|
||||||
throw new RuntimeException("cannot find available ip from"
|
+ " network interface " + networkInterface);
|
||||||
+ " network interface " + networkInterface);
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
this.overrideFromEnv(environment);
|
||||||
this.overrideFromEnv(environment);
|
}
|
||||||
}
|
|
||||||
|
public String getEndpoint() {
|
||||||
public String getEndpoint() {
|
return endpoint;
|
||||||
return endpoint;
|
}
|
||||||
}
|
|
||||||
|
public void setEndpoint(String endpoint) {
|
||||||
public void setEndpoint(String endpoint) {
|
this.endpoint = endpoint;
|
||||||
this.endpoint = endpoint;
|
}
|
||||||
}
|
|
||||||
|
public String getNamespace() {
|
||||||
public String getNamespace() {
|
return namespace;
|
||||||
return namespace;
|
}
|
||||||
}
|
|
||||||
|
public void setNamespace(String namespace) {
|
||||||
public void setNamespace(String namespace) {
|
this.namespace = namespace;
|
||||||
this.namespace = namespace;
|
}
|
||||||
}
|
|
||||||
|
public String getLogName() {
|
||||||
public String getLogName() {
|
return logName;
|
||||||
return logName;
|
}
|
||||||
}
|
|
||||||
|
public void setLogName(String logName) {
|
||||||
public void setLogName(String logName) {
|
this.logName = logName;
|
||||||
this.logName = logName;
|
}
|
||||||
}
|
|
||||||
|
public void setInetUtils(InetUtils inetUtils) {
|
||||||
public void setInetUtils(InetUtils inetUtils) {
|
this.inetUtils = inetUtils;
|
||||||
this.inetUtils = inetUtils;
|
}
|
||||||
}
|
|
||||||
|
public float getWeight() {
|
||||||
public float getWeight() {
|
return weight;
|
||||||
return weight;
|
}
|
||||||
}
|
|
||||||
|
public void setWeight(float weight) {
|
||||||
public void setWeight(float weight) {
|
this.weight = weight;
|
||||||
this.weight = weight;
|
}
|
||||||
}
|
|
||||||
|
public String getClusterName() {
|
||||||
public String getClusterName() {
|
return clusterName;
|
||||||
return clusterName;
|
}
|
||||||
}
|
|
||||||
|
public void setClusterName(String clusterName) {
|
||||||
public void setClusterName(String clusterName) {
|
this.clusterName = clusterName;
|
||||||
this.clusterName = clusterName;
|
}
|
||||||
}
|
|
||||||
|
public String getService() {
|
||||||
public String getService() {
|
return service;
|
||||||
return service;
|
}
|
||||||
}
|
|
||||||
|
public void setService(String service) {
|
||||||
public void setService(String service) {
|
this.service = service;
|
||||||
this.service = service;
|
}
|
||||||
}
|
|
||||||
|
public boolean isRegisterEnabled() {
|
||||||
public boolean isRegisterEnabled() {
|
return registerEnabled;
|
||||||
return registerEnabled;
|
}
|
||||||
}
|
|
||||||
|
public void setRegisterEnabled(boolean registerEnabled) {
|
||||||
public void setRegisterEnabled(boolean registerEnabled) {
|
this.registerEnabled = registerEnabled;
|
||||||
this.registerEnabled = registerEnabled;
|
}
|
||||||
}
|
|
||||||
|
public String getIp() {
|
||||||
public String getIp() {
|
return ip;
|
||||||
return ip;
|
}
|
||||||
}
|
|
||||||
|
public void setIp(String ip) {
|
||||||
public void setIp(String ip) {
|
this.ip = ip;
|
||||||
this.ip = ip;
|
}
|
||||||
}
|
|
||||||
|
public String getNetworkInterface() {
|
||||||
public String getNetworkInterface() {
|
return networkInterface;
|
||||||
return networkInterface;
|
}
|
||||||
}
|
|
||||||
|
public void setNetworkInterface(String networkInterface) {
|
||||||
public void setNetworkInterface(String networkInterface) {
|
this.networkInterface = networkInterface;
|
||||||
this.networkInterface = networkInterface;
|
}
|
||||||
}
|
|
||||||
|
public int getPort() {
|
||||||
public int getPort() {
|
return port;
|
||||||
return port;
|
}
|
||||||
}
|
|
||||||
|
public void setPort(int port) {
|
||||||
public void setPort(int port) {
|
this.port = port;
|
||||||
this.port = port;
|
}
|
||||||
}
|
|
||||||
|
public boolean isSecure() {
|
||||||
public boolean isSecure() {
|
return secure;
|
||||||
return secure;
|
}
|
||||||
}
|
|
||||||
|
public void setSecure(boolean secure) {
|
||||||
public void setSecure(boolean secure) {
|
this.secure = secure;
|
||||||
this.secure = secure;
|
}
|
||||||
}
|
|
||||||
|
public Map<String, String> getMetadata() {
|
||||||
public Map<String, String> getMetadata() {
|
return metadata;
|
||||||
return metadata;
|
}
|
||||||
}
|
|
||||||
|
public void setMetadata(Map<String, String> metadata) {
|
||||||
public void setMetadata(Map<String, String> metadata) {
|
this.metadata = metadata;
|
||||||
this.metadata = metadata;
|
}
|
||||||
}
|
|
||||||
|
public String getServerAddr() {
|
||||||
public String getServerAddr() {
|
return serverAddr;
|
||||||
return serverAddr;
|
}
|
||||||
}
|
|
||||||
|
public void setServerAddr(String serverAddr) {
|
||||||
public void setServerAddr(String serverAddr) {
|
this.serverAddr = serverAddr;
|
||||||
this.serverAddr = serverAddr;
|
}
|
||||||
}
|
|
||||||
|
public String getAccessKey() {
|
||||||
public String getAccessKey() {
|
return accessKey;
|
||||||
return accessKey;
|
}
|
||||||
}
|
|
||||||
|
public void setAccessKey(String accessKey) {
|
||||||
public void setAccessKey(String accessKey) {
|
this.accessKey = accessKey;
|
||||||
this.accessKey = accessKey;
|
}
|
||||||
}
|
|
||||||
|
public String getSecretKey() {
|
||||||
public String getSecretKey() {
|
return secretKey;
|
||||||
return secretKey;
|
}
|
||||||
}
|
|
||||||
|
public void setSecretKey(String secretKey) {
|
||||||
public void setSecretKey(String secretKey) {
|
this.secretKey = secretKey;
|
||||||
this.secretKey = secretKey;
|
}
|
||||||
}
|
|
||||||
|
public String getNamingLoadCacheAtStart() {
|
||||||
public String getNamingLoadCacheAtStart() {
|
return namingLoadCacheAtStart;
|
||||||
return namingLoadCacheAtStart;
|
}
|
||||||
}
|
|
||||||
|
public void setNamingLoadCacheAtStart(String namingLoadCacheAtStart) {
|
||||||
public void setNamingLoadCacheAtStart(String namingLoadCacheAtStart) {
|
this.namingLoadCacheAtStart = namingLoadCacheAtStart;
|
||||||
this.namingLoadCacheAtStart = namingLoadCacheAtStart;
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
@Override
|
public String toString() {
|
||||||
public String toString() {
|
return "NacosDiscoveryProperties{" + "serverAddr='" + serverAddr + '\''
|
||||||
return "NacosDiscoveryProperties{" + "serverAddr='" + serverAddr + '\''
|
+ ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\''
|
||||||
+ ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\''
|
+ ", logName='" + logName + '\'' + ", service='" + service + '\''
|
||||||
+ ", logName='" + logName + '\'' + ", service='" + service + '\''
|
+ ", weight=" + weight + ", clusterName='" + clusterName + '\''
|
||||||
+ ", weight=" + weight + ", clusterName='" + clusterName + '\''
|
+ ", metadata=" + metadata + ", registerEnabled=" + registerEnabled
|
||||||
+ ", metadata=" + metadata + ", registerEnabled=" + registerEnabled
|
+ ", ip='" + ip + '\'' + ", networkInterface='" + networkInterface + '\''
|
||||||
+ ", ip='" + ip + '\'' + ", networkInterface='" + networkInterface + '\''
|
+ ", port=" + port + ", secure=" + secure + ", accessKey='" + accessKey
|
||||||
+ ", port=" + port + ", secure=" + secure + ", accessKey='" + accessKey
|
+ ", namingLoadCacheAtStart=" + namingLoadCacheAtStart + '\''
|
||||||
+ ", namingLoadCacheAtStart=" + namingLoadCacheAtStart + '\''
|
+ ", secretKey='" + secretKey + '\'' + '}';
|
||||||
+ ", secretKey='" + secretKey + '\'' + '}';
|
}
|
||||||
}
|
|
||||||
|
public void overrideFromEnv(Environment env) {
|
||||||
public void overrideFromEnv(Environment env) {
|
|
||||||
|
if (StringUtils.isEmpty(this.getServerAddr())) {
|
||||||
if (StringUtils.isEmpty(this.getServerAddr())) {
|
this.setServerAddr(env
|
||||||
this.setServerAddr(env
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
|
}
|
||||||
}
|
if (StringUtils.isEmpty(this.getNamespace())) {
|
||||||
if (StringUtils.isEmpty(this.getNamespace())) {
|
this.setNamespace(env
|
||||||
this.setNamespace(env
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}"));
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.discovery.namespace:}"));
|
}
|
||||||
}
|
if (StringUtils.isEmpty(this.getAccessKey())) {
|
||||||
if (StringUtils.isEmpty(this.getAccessKey())) {
|
this.setAccessKey(env
|
||||||
this.setAccessKey(env
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}"));
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.discovery.access-key:}"));
|
}
|
||||||
}
|
if (StringUtils.isEmpty(this.getSecretKey())) {
|
||||||
if (StringUtils.isEmpty(this.getSecretKey())) {
|
this.setSecretKey(env
|
||||||
this.setSecretKey(env
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}"));
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.discovery.secret-key:}"));
|
}
|
||||||
}
|
if (StringUtils.isEmpty(this.getLogName())) {
|
||||||
if (StringUtils.isEmpty(this.getLogName())) {
|
this.setLogName(
|
||||||
this.setLogName(
|
env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}"));
|
||||||
env.resolvePlaceholders("${spring.cloud.nacos.discovery.log-name:}"));
|
}
|
||||||
}
|
if (StringUtils.isEmpty(this.getClusterName())) {
|
||||||
if (StringUtils.isEmpty(this.getClusterName())) {
|
this.setClusterName(env.resolvePlaceholders(
|
||||||
this.setClusterName(env.resolvePlaceholders(
|
"${spring.cloud.nacos.discovery.cluster-name:}"));
|
||||||
"${spring.cloud.nacos.discovery.cluster-name:}"));
|
}
|
||||||
}
|
if (StringUtils.isEmpty(this.getEndpoint())) {
|
||||||
if (StringUtils.isEmpty(this.getEndpoint())) {
|
this.setEndpoint(
|
||||||
this.setEndpoint(
|
env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}"));
|
||||||
env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}"));
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public NamingService namingServiceInstance() {
|
||||||
public NamingService namingServiceInstance() {
|
|
||||||
|
if (null != namingService) {
|
||||||
if (null != namingService) {
|
return namingService;
|
||||||
return namingService;
|
}
|
||||||
}
|
|
||||||
|
Properties properties = new Properties();
|
||||||
Properties properties = new Properties();
|
properties.put(SERVER_ADDR, serverAddr);
|
||||||
properties.put(SERVER_ADDR, serverAddr);
|
properties.put(NAMESPACE, namespace);
|
||||||
properties.put(NAMESPACE, namespace);
|
properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME, logName);
|
||||||
properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME, logName);
|
properties.put(ENDPOINT, endpoint);
|
||||||
properties.put(ENDPOINT, endpoint);
|
properties.put(ACCESS_KEY, accessKey);
|
||||||
properties.put(ACCESS_KEY, accessKey);
|
properties.put(SECRET_KEY, secretKey);
|
||||||
properties.put(SECRET_KEY, secretKey);
|
properties.put(CLUSTER_NAME, clusterName);
|
||||||
properties.put(CLUSTER_NAME, clusterName);
|
properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart);
|
||||||
properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart);
|
|
||||||
|
try {
|
||||||
try {
|
namingService = NacosFactory.createNamingService(properties);
|
||||||
namingService = NacosFactory.createNamingService(properties);
|
} catch (Exception e) {
|
||||||
return namingService;
|
LOGGER.error("create naming service error!properties={},e=,", this, e);
|
||||||
}
|
return null;
|
||||||
catch (Exception e) {
|
}
|
||||||
LOGGER.error("create naming service error!properties={},e=,", this, e);
|
return namingService;
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,83 +20,85 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
|
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
|
||||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
||||||
|
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||||
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
*/
|
*/
|
||||||
public class NacosAutoServiceRegistration
|
public class NacosAutoServiceRegistration
|
||||||
extends AbstractAutoServiceRegistration<NacosRegistration> {
|
extends AbstractAutoServiceRegistration<Registration> {
|
||||||
private static final Logger LOGGER = LoggerFactory
|
private static final Logger LOGGER = LoggerFactory
|
||||||
.getLogger(NacosAutoServiceRegistration.class);
|
.getLogger(NacosAutoServiceRegistration.class);
|
||||||
|
|
||||||
private NacosRegistration registration;
|
private NacosRegistration registration;
|
||||||
|
|
||||||
public NacosAutoServiceRegistration(
|
public NacosAutoServiceRegistration(
|
||||||
ServiceRegistry<NacosRegistration> serviceRegistry,
|
ServiceRegistry<Registration> serviceRegistry,
|
||||||
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
|
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
|
||||||
NacosRegistration registration) {
|
NacosRegistration registration) {
|
||||||
super(serviceRegistry, autoServiceRegistrationProperties);
|
super(serviceRegistry, autoServiceRegistrationProperties);
|
||||||
this.registration = registration;
|
this.registration = registration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public void setPort(int port) {
|
public void setPort(int port) {
|
||||||
getPort().set(port);
|
getPort().set(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NacosRegistration getRegistration() {
|
protected NacosRegistration getRegistration() {
|
||||||
if (this.registration.getPort() < 0 && this.getPort().get() > 0) {
|
if (this.registration.getPort() < 0 && this.getPort().get() > 0) {
|
||||||
this.registration.setPort(this.getPort().get());
|
this.registration.setPort(this.getPort().get());
|
||||||
}
|
}
|
||||||
Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set");
|
Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set");
|
||||||
return this.registration;
|
return this.registration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NacosRegistration getManagementRegistration() {
|
protected NacosRegistration getManagementRegistration() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void register() {
|
protected void register() {
|
||||||
if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
|
if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
|
||||||
LOGGER.debug("Registration disabled.");
|
LOGGER.debug("Registration disabled.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (this.registration.getPort() < 0) {
|
if (this.registration.getPort() < 0) {
|
||||||
this.registration.setPort(getPort().get());
|
this.registration.setPort(getPort().get());
|
||||||
}
|
}
|
||||||
super.register();
|
super.register();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void registerManagement() {
|
protected void registerManagement() {
|
||||||
if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
|
if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
super.registerManagement();
|
super.registerManagement();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object getConfiguration() {
|
protected Object getConfiguration() {
|
||||||
return this.registration.getNacosDiscoveryProperties();
|
return this.registration.getNacosDiscoveryProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isEnabled() {
|
protected boolean isEnabled() {
|
||||||
return this.registration.getNacosDiscoveryProperties().isRegisterEnabled();
|
return this.registration.getNacosDiscoveryProperties().isRegisterEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
protected String getAppName() {
|
protected String getAppName() {
|
||||||
String appName = registration.getNacosDiscoveryProperties().getService();
|
String appName = registration.getNacosDiscoveryProperties().getService();
|
||||||
return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
|
return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,90 +18,97 @@ package org.springframework.cloud.alibaba.nacos.registry;
|
|||||||
|
|
||||||
import com.alibaba.nacos.api.naming.NamingService;
|
import com.alibaba.nacos.api.naming.NamingService;
|
||||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
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.cloud.client.serviceregistry.ServiceRegistry;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
*/
|
*/
|
||||||
public class NacosServiceRegistry implements ServiceRegistry<NacosRegistration> {
|
public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(NacosServiceRegistry.class);
|
private static Logger logger = LoggerFactory.getLogger(NacosServiceRegistry.class);
|
||||||
|
|
||||||
@Override
|
private final NacosDiscoveryProperties nacosDiscoveryProperties;
|
||||||
public void register(NacosRegistration registration) {
|
|
||||||
|
|
||||||
if (!registration.isRegisterEnabled()) {
|
private final NamingService namingService;
|
||||||
logger.info("Nacos Registration is disabled...");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (StringUtils.isEmpty(registration.getServiceId())) {
|
|
||||||
logger.info("No service to register for nacos client...");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
NamingService namingService = registration.getNacosNamingService();
|
public NacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
|
||||||
String serviceId = registration.getServiceId();
|
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 {
|
@Override
|
||||||
namingService.registerInstance(serviceId, instance);
|
public void register(Registration registration) {
|
||||||
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
|
if (StringUtils.isEmpty(registration.getServiceId())) {
|
||||||
public void deregister(NacosRegistration registration) {
|
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())) {
|
Instance instance = new Instance();
|
||||||
logger.info("No dom to de-register for nacos client...");
|
instance.setIp(registration.getHost());
|
||||||
return;
|
instance.setPort(registration.getPort());
|
||||||
}
|
instance.setWeight(nacosDiscoveryProperties.getWeight());
|
||||||
|
instance.setClusterName(nacosDiscoveryProperties.getClusterName());
|
||||||
|
instance.setMetadata(registration.getMetadata());
|
||||||
|
|
||||||
NamingService namingService = registration.getNacosNamingService();
|
try {
|
||||||
String serviceId = registration.getServiceId();
|
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 {
|
@Override
|
||||||
namingService.deregisterInstance(serviceId, registration.getHost(),
|
public void deregister(Registration registration) {
|
||||||
registration.getPort(), registration.getCluster());
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
logger.error("ERR_NACOS_DEREGISTER, de-register failed...{},",
|
|
||||||
registration.toString(), e);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("De-registration finished.");
|
logger.info("De-registering from Nacos Server now...");
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
if (StringUtils.isEmpty(registration.getServiceId())) {
|
||||||
public void close() {
|
logger.info("No dom to de-register for nacos client...");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();
|
||||||
|
String serviceId = registration.getServiceId();
|
||||||
|
|
||||||
@Override
|
try {
|
||||||
public void setStatus(NacosRegistration registration, String status) {
|
namingService.deregisterInstance(serviceId, registration.getHost(),
|
||||||
// nacos doesn't support set status of a particular registration.
|
registration.getPort(), nacosDiscoveryProperties.getClusterName());
|
||||||
}
|
} catch (Exception e) {
|
||||||
|
logger.error("ERR_NACOS_DEREGISTER, de-register failed...{},",
|
||||||
|
registration.toString(), e);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
logger.info("De-registration finished.");
|
||||||
public <T> T getStatus(NacosRegistration registration) {
|
}
|
||||||
// nacos doesn't support query status of a particular registration.
|
|
||||||
return null;
|
@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> T getStatus(Registration registration) {
|
||||||
|
// nacos doesn't support query status of a particular registration.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user