mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
support nacos discovery group
This commit is contained in:
parent
30011ae302
commit
e419cac829
@ -65,7 +65,7 @@ public class NacosDiscoveryProperties {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(NacosDiscoveryProperties.class);
|
||||
|
||||
|
||||
/**
|
||||
* nacos discovery server address.
|
||||
*/
|
||||
@ -104,10 +104,15 @@ public class NacosDiscoveryProperties {
|
||||
private float weight = 1;
|
||||
|
||||
/**
|
||||
* cluster name for nacos server.
|
||||
* cluster name for nacos .
|
||||
*/
|
||||
private String clusterName = "DEFAULT";
|
||||
|
||||
/**
|
||||
* group name for nacos
|
||||
*/
|
||||
private String group = "DEFAULT";
|
||||
|
||||
/**
|
||||
* naming load from local cache at application start. true is load.
|
||||
*/
|
||||
@ -395,25 +400,35 @@ public class NacosDiscoveryProperties {
|
||||
this.watchDelay = watchDelay;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public void setGroup(String group) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NacosDiscoveryProperties{" + "serverAddr='" + serverAddr + '\''
|
||||
+ ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\''
|
||||
+ ", watchDelay=" + watchDelay + ", logName='" + logName + '\''
|
||||
+ ", service='" + service + '\'' + ", weight=" + weight
|
||||
+ ", clusterName='" + clusterName + '\'' + ", namingLoadCacheAtStart='"
|
||||
+ namingLoadCacheAtStart + '\'' + ", metadata=" + metadata
|
||||
+ ", registerEnabled=" + registerEnabled + ", ip='" + ip + '\''
|
||||
+ ", networkInterface='" + networkInterface + '\'' + ", port=" + port
|
||||
+ ", secure=" + secure + ", accessKey='" + accessKey + '\''
|
||||
+ ", secretKey='" + secretKey + '\'' + '}';
|
||||
+ ", clusterName='" + clusterName + '\'' + ", group='" + group + '\''
|
||||
+ ", namingLoadCacheAtStart='" + namingLoadCacheAtStart + '\''
|
||||
+ ", metadata=" + metadata + ", registerEnabled=" + registerEnabled
|
||||
+ ", ip='" + ip + '\'' + ", networkInterface='" + networkInterface + '\''
|
||||
+ ", port=" + port + ", secure=" + secure + ", accessKey='" + accessKey
|
||||
+ '\'' + ", secretKey='" + secretKey + '\'' + ", heartBeatInterval="
|
||||
+ heartBeatInterval + ", heartBeatTimeout=" + heartBeatTimeout
|
||||
+ ", ipDeleteTimeout=" + ipDeleteTimeout + '}';
|
||||
}
|
||||
|
||||
public void overrideFromEnv(Environment env) {
|
||||
|
||||
if (StringUtils.isEmpty(this.getServerAddr())) {
|
||||
String serverAddr = env.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}");
|
||||
if(StringUtils.isEmpty(serverAddr)) {
|
||||
if (StringUtils.isEmpty(serverAddr)) {
|
||||
serverAddr = env.resolvePlaceholders("${spring.cloud.nacos.server-addr}");
|
||||
}
|
||||
this.setServerAddr(serverAddr);
|
||||
@ -442,6 +457,10 @@ public class NacosDiscoveryProperties {
|
||||
this.setEndpoint(
|
||||
env.resolvePlaceholders("${spring.cloud.nacos.discovery.endpoint:}"));
|
||||
}
|
||||
if (StringUtils.isEmpty(this.getGroup())) {
|
||||
this.setGroup(
|
||||
env.resolvePlaceholders("${spring.cloud.nacos.discovery.group:}"));
|
||||
}
|
||||
}
|
||||
|
||||
public NamingService namingServiceInstance() {
|
||||
|
@ -54,8 +54,9 @@ public class NacosDiscoveryClient implements DiscoveryClient {
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(String serviceId) {
|
||||
try {
|
||||
String group = discoveryProperties.getGroup();
|
||||
List<Instance> instances = discoveryProperties.namingServiceInstance()
|
||||
.selectInstances(serviceId, true);
|
||||
.selectInstances(serviceId, group, true);
|
||||
return hostToServiceInstanceList(instances, serviceId);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(
|
||||
|
@ -54,11 +54,12 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
}
|
||||
|
||||
String serviceId = registration.getServiceId();
|
||||
String group = nacosDiscoveryProperties.getGroup();
|
||||
|
||||
Instance instance = getNacosInstanceFromRegistration(registration);
|
||||
|
||||
try {
|
||||
namingService.registerInstance(serviceId, instance);
|
||||
namingService.registerInstance(serviceId, group, instance);
|
||||
log.info("nacos registry, {} {}:{} register finished", serviceId,
|
||||
instance.getIp(), instance.getPort());
|
||||
}
|
||||
@ -80,9 +81,10 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
|
||||
NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();
|
||||
String serviceId = registration.getServiceId();
|
||||
String group = nacosDiscoveryProperties.getGroup();
|
||||
|
||||
try {
|
||||
namingService.deregisterInstance(serviceId, registration.getHost(),
|
||||
namingService.deregisterInstance(serviceId, group, registration.getHost(),
|
||||
registration.getPort(), nacosDiscoveryProperties.getClusterName());
|
||||
}
|
||||
catch (Exception e) {
|
||||
@ -153,6 +155,7 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
instance.setWeight(nacosDiscoveryProperties.getWeight());
|
||||
instance.setClusterName(nacosDiscoveryProperties.getClusterName());
|
||||
instance.setMetadata(registration.getMetadata());
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
@ -52,8 +52,9 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
|
||||
|
||||
private List<NacosServer> getServers() {
|
||||
try {
|
||||
String group = discoveryProperties.getGroup();
|
||||
List<Instance> instances = discoveryProperties.namingServiceInstance()
|
||||
.selectInstances(serviceId, true);
|
||||
.selectInstances(serviceId, group,true);
|
||||
return instancesToServerList(instances);
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user