mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
nacos discovery support group
This commit is contained in:
parent
80dcd91221
commit
18cb1e396f
@ -66,7 +66,7 @@ public class NacosDiscoveryProperties {
|
||||
.getLogger(NacosDiscoveryProperties.class);
|
||||
|
||||
/**
|
||||
* nacos discovery server address
|
||||
* nacos discovery server address.
|
||||
*/
|
||||
private String serverAddr;
|
||||
|
||||
@ -87,12 +87,12 @@ public class NacosDiscoveryProperties {
|
||||
private long watchDelay = 30000;
|
||||
|
||||
/**
|
||||
* nacos naming log file name
|
||||
* nacos naming log file name.
|
||||
*/
|
||||
private String logName;
|
||||
|
||||
/**
|
||||
* service name to registry
|
||||
* service name to registry.
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.discovery.service:${spring.application.name:}}")
|
||||
private String service;
|
||||
@ -103,12 +103,17 @@ public class NacosDiscoveryProperties {
|
||||
private float weight = 1;
|
||||
|
||||
/**
|
||||
* cluster name for nacos server.
|
||||
* cluster name for nacos .
|
||||
*/
|
||||
private String clusterName = "DEFAULT";
|
||||
|
||||
/**
|
||||
* naming load from local cache at application start. true is load
|
||||
* group name for nacos
|
||||
*/
|
||||
private String group = "DEFAULT_GROUP";
|
||||
|
||||
/**
|
||||
* naming load from local cache at application start. true is load.
|
||||
*/
|
||||
private String namingLoadCacheAtStart = "false";
|
||||
|
||||
@ -130,18 +135,18 @@ public class NacosDiscoveryProperties {
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* which network interface's ip you want to register
|
||||
* 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
|
||||
* auto detect port works well.
|
||||
*/
|
||||
private int port = -1;
|
||||
|
||||
/**
|
||||
* whether your service is a https service
|
||||
* whether your service is a https service.
|
||||
*/
|
||||
private boolean secure = false;
|
||||
|
||||
@ -189,7 +194,7 @@ public class NacosDiscoveryProperties {
|
||||
}
|
||||
|
||||
serverAddr = Objects.toString(serverAddr, "");
|
||||
if (serverAddr.lastIndexOf("/") != -1) {
|
||||
if (serverAddr.endsWith("/")) {
|
||||
serverAddr = serverAddr.substring(0, serverAddr.length() - 1);
|
||||
}
|
||||
endpoint = Objects.toString(endpoint, "");
|
||||
@ -394,25 +399,38 @@ 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())) {
|
||||
this.setServerAddr(env
|
||||
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"));
|
||||
String serverAddr = env.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}");
|
||||
if (StringUtils.isEmpty(serverAddr)) {
|
||||
serverAddr = env.resolvePlaceholders("${spring.cloud.nacos.server-addr}");
|
||||
}
|
||||
this.setServerAddr(serverAddr);
|
||||
}
|
||||
if (StringUtils.isEmpty(this.getNamespace())) {
|
||||
this.setNamespace(env
|
||||
@ -438,6 +456,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() {
|
||||
|
@ -16,21 +16,20 @@
|
||||
|
||||
package com.alibaba.cloud.nacos.discovery;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||
import com.alibaba.cloud.nacos.NacosServiceInstance;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||
import com.alibaba.cloud.nacos.NacosServiceInstance;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
@ -55,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) {
|
||||
@ -106,8 +106,9 @@ public class NacosDiscoveryClient implements DiscoveryClient {
|
||||
public List<String> getServices() {
|
||||
|
||||
try {
|
||||
String group = discoveryProperties.getGroup();
|
||||
ListView<String> services = discoveryProperties.namingServiceInstance()
|
||||
.getServicesOfServer(1, Integer.MAX_VALUE);
|
||||
.getServicesOfServer(1, Integer.MAX_VALUE, group);
|
||||
return services.getData();
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@ import java.util.List;
|
||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
|
||||
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.AbstractServerList;
|
||||
|
||||
@ -51,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) {
|
||||
@ -64,7 +66,7 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
|
||||
|
||||
private List<NacosServer> instancesToServerList(List<Instance> instances) {
|
||||
List<NacosServer> result = new ArrayList<>();
|
||||
if (null == instances) {
|
||||
if (CollectionUtils.isEmpty(instances)) {
|
||||
return result;
|
||||
}
|
||||
for (Instance instance : instances) {
|
||||
|
@ -16,24 +16,23 @@
|
||||
|
||||
package com.alibaba.cloud.nacos;
|
||||
|
||||
import static com.alibaba.cloud.nacos.test.NacosMockTest.serviceInstance;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClient;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClient;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||
import static com.alibaba.cloud.nacos.test.NacosMockTest.serviceInstance;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
@ -61,7 +60,8 @@ public class NacosDiscoveryClientTests {
|
||||
NamingService namingService = mock(NamingService.class);
|
||||
|
||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
||||
when(namingService.selectInstances(eq(serviceName), eq(true)))
|
||||
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
|
||||
when(namingService.selectInstances(eq(serviceName),eq("DEFAULT"), eq(true)))
|
||||
.thenReturn(instances);
|
||||
|
||||
NacosDiscoveryClient discoveryClient = new NacosDiscoveryClient(
|
||||
@ -104,7 +104,8 @@ public class NacosDiscoveryClientTests {
|
||||
nacosDiscoveryProperties);
|
||||
|
||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
||||
when(namingService.getServicesOfServer(eq(1), eq(Integer.MAX_VALUE)))
|
||||
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
|
||||
when(namingService.getServicesOfServer(eq(1), eq(Integer.MAX_VALUE),eq("DEFAULT")))
|
||||
.thenReturn(nacosServices);
|
||||
|
||||
List<String> services = discoveryClient.getServices();
|
||||
|
@ -28,7 +28,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||
@ -53,11 +52,12 @@ public class NacosServerListTests {
|
||||
NamingService namingService = mock(NamingService.class);
|
||||
|
||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
||||
when(namingService.selectInstances(anyString(), eq(true))).thenReturn(null);
|
||||
when(namingService.selectInstances(anyString(), eq("DEFAULT"), eq(true)))
|
||||
.thenReturn(null);
|
||||
|
||||
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
|
||||
List<NacosServer> servers = serverList.getInitialListOfServers();
|
||||
Assertions.assertThat(servers).isEmpty();
|
||||
assertThat(servers).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -74,7 +74,9 @@ public class NacosServerListTests {
|
||||
NamingService namingService = mock(NamingService.class);
|
||||
|
||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
||||
when(namingService.selectInstances(eq("test-service"), eq(true)))
|
||||
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
|
||||
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
|
||||
when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true)))
|
||||
.thenReturn(instances);
|
||||
|
||||
IClientConfig clientConfig = mock(IClientConfig.class);
|
||||
@ -82,10 +84,10 @@ public class NacosServerListTests {
|
||||
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
|
||||
serverList.initWithNiwsConfig(clientConfig);
|
||||
List<NacosServer> servers = serverList.getInitialListOfServers();
|
||||
Assertions.assertThat(servers).hasSize(1);
|
||||
assertThat(servers).hasSize(1);
|
||||
|
||||
servers = serverList.getUpdatedListOfServers();
|
||||
Assertions.assertThat(servers).hasSize(1);
|
||||
assertThat(servers).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -106,7 +108,8 @@ public class NacosServerListTests {
|
||||
NamingService namingService = mock(NamingService.class);
|
||||
|
||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
||||
when(namingService.selectInstances(eq("test-service"), eq(true)))
|
||||
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
|
||||
when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true)))
|
||||
.thenReturn(instances.stream().filter(Instance::isHealthy)
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
@ -115,7 +118,7 @@ public class NacosServerListTests {
|
||||
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
|
||||
serverList.initWithNiwsConfig(clientConfig);
|
||||
List<NacosServer> servers = serverList.getInitialListOfServers();
|
||||
Assertions.assertThat(servers).hasSize(1);
|
||||
assertThat(servers).hasSize(1);
|
||||
|
||||
NacosServer nacosServer = servers.get(0);
|
||||
|
||||
@ -143,7 +146,8 @@ public class NacosServerListTests {
|
||||
NamingService namingService = mock(NamingService.class);
|
||||
|
||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
||||
when(namingService.selectInstances(eq("test-service"), eq(true)))
|
||||
when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
|
||||
when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true)))
|
||||
.thenReturn(instances.stream().filter(Instance::isHealthy)
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
@ -153,7 +157,7 @@ public class NacosServerListTests {
|
||||
serverList.initWithNiwsConfig(clientConfig);
|
||||
|
||||
List<NacosServer> servers = serverList.getUpdatedListOfServers();
|
||||
Assertions.assertThat(servers).hasSize(1);
|
||||
assertThat(servers).hasSize(1);
|
||||
|
||||
assertThat(servers.get(0).getInstance().isHealthy()).isEqualTo(true);
|
||||
assertThat(servers.get(0).getInstance().getMetadata().get("instanceNum"))
|
||||
|
Loading…
x
Reference in New Issue
Block a user