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