support nacos discovery group

pull/873/head
flystar32 6 years ago
parent 30011ae302
commit e419cac829

@ -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,18 +400,28 @@ 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) {
@ -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…
Cancel
Save