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);
|
.getLogger(NacosDiscoveryProperties.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* nacos discovery server address
|
* nacos discovery server address.
|
||||||
*/
|
*/
|
||||||
private String serverAddr;
|
private String serverAddr;
|
||||||
|
|
||||||
@ -87,12 +87,12 @@ public class NacosDiscoveryProperties {
|
|||||||
private long watchDelay = 30000;
|
private long watchDelay = 30000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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;
|
||||||
@ -103,12 +103,17 @@ 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";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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";
|
private String namingLoadCacheAtStart = "false";
|
||||||
|
|
||||||
@ -130,18 +135,18 @@ public class NacosDiscoveryProperties {
|
|||||||
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;
|
||||||
|
|
||||||
@ -189,7 +194,7 @@ public class NacosDiscoveryProperties {
|
|||||||
}
|
}
|
||||||
|
|
||||||
serverAddr = Objects.toString(serverAddr, "");
|
serverAddr = Objects.toString(serverAddr, "");
|
||||||
if (serverAddr.lastIndexOf("/") != -1) {
|
if (serverAddr.endsWith("/")) {
|
||||||
serverAddr = serverAddr.substring(0, serverAddr.length() - 1);
|
serverAddr = serverAddr.substring(0, serverAddr.length() - 1);
|
||||||
}
|
}
|
||||||
endpoint = Objects.toString(endpoint, "");
|
endpoint = Objects.toString(endpoint, "");
|
||||||
@ -394,25 +399,38 @@ 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())) {
|
||||||
this.setServerAddr(env
|
String serverAddr = env.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}");
|
||||||
.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())) {
|
if (StringUtils.isEmpty(this.getNamespace())) {
|
||||||
this.setNamespace(env
|
this.setNamespace(env
|
||||||
@ -438,6 +456,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() {
|
||||||
|
@ -16,21 +16,20 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.nacos.discovery;
|
package com.alibaba.cloud.nacos.discovery;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
import java.util.Collections;
|
import com.alibaba.cloud.nacos.NacosServiceInstance;
|
||||||
import java.util.HashMap;
|
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||||
import java.util.List;
|
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
|
|
||||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
import java.util.ArrayList;
|
||||||
import com.alibaba.cloud.nacos.NacosServiceInstance;
|
import java.util.Collections;
|
||||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
import java.util.HashMap;
|
||||||
import com.alibaba.nacos.api.naming.pojo.ListView;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
@ -55,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) {
|
||||||
@ -106,8 +106,9 @@ public class NacosDiscoveryClient implements DiscoveryClient {
|
|||||||
public List<String> getServices() {
|
public List<String> getServices() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
String group = discoveryProperties.getGroup();
|
||||||
ListView<String> services = discoveryProperties.namingServiceInstance()
|
ListView<String> services = discoveryProperties.namingServiceInstance()
|
||||||
.getServicesOfServer(1, Integer.MAX_VALUE);
|
.getServicesOfServer(1, Integer.MAX_VALUE, group);
|
||||||
return services.getData();
|
return services.getData();
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ import java.util.List;
|
|||||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
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.client.config.IClientConfig;
|
||||||
import com.netflix.loadbalancer.AbstractServerList;
|
import com.netflix.loadbalancer.AbstractServerList;
|
||||||
|
|
||||||
@ -51,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) {
|
||||||
@ -64,7 +66,7 @@ public class NacosServerList extends AbstractServerList<NacosServer> {
|
|||||||
|
|
||||||
private List<NacosServer> instancesToServerList(List<Instance> instances) {
|
private List<NacosServer> instancesToServerList(List<Instance> instances) {
|
||||||
List<NacosServer> result = new ArrayList<>();
|
List<NacosServer> result = new ArrayList<>();
|
||||||
if (null == instances) {
|
if (CollectionUtils.isEmpty(instances)) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
for (Instance instance : instances) {
|
for (Instance instance : instances) {
|
||||||
|
@ -16,24 +16,23 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.nacos;
|
package com.alibaba.cloud.nacos;
|
||||||
|
|
||||||
import static com.alibaba.cloud.nacos.test.NacosMockTest.serviceInstance;
|
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClient;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import com.alibaba.nacos.api.naming.NamingService;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||||
import static org.mockito.Mockito.mock;
|
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||||
import static org.mockito.Mockito.when;
|
import org.junit.Test;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static com.alibaba.cloud.nacos.test.NacosMockTest.serviceInstance;
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClient;
|
import static org.mockito.Mockito.mock;
|
||||||
import com.alibaba.nacos.api.naming.NamingService;
|
import static org.mockito.Mockito.when;
|
||||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
|
||||||
import com.alibaba.nacos.api.naming.pojo.ListView;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
@ -61,7 +60,8 @@ public class NacosDiscoveryClientTests {
|
|||||||
NamingService namingService = mock(NamingService.class);
|
NamingService namingService = mock(NamingService.class);
|
||||||
|
|
||||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
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);
|
.thenReturn(instances);
|
||||||
|
|
||||||
NacosDiscoveryClient discoveryClient = new NacosDiscoveryClient(
|
NacosDiscoveryClient discoveryClient = new NacosDiscoveryClient(
|
||||||
@ -104,7 +104,8 @@ public class NacosDiscoveryClientTests {
|
|||||||
nacosDiscoveryProperties);
|
nacosDiscoveryProperties);
|
||||||
|
|
||||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
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);
|
.thenReturn(nacosServices);
|
||||||
|
|
||||||
List<String> services = discoveryClient.getServices();
|
List<String> services = discoveryClient.getServices();
|
||||||
|
@ -28,7 +28,6 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.assertj.core.api.Assertions;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
@ -53,11 +52,12 @@ public class NacosServerListTests {
|
|||||||
NamingService namingService = mock(NamingService.class);
|
NamingService namingService = mock(NamingService.class);
|
||||||
|
|
||||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
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);
|
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
|
||||||
List<NacosServer> servers = serverList.getInitialListOfServers();
|
List<NacosServer> servers = serverList.getInitialListOfServers();
|
||||||
Assertions.assertThat(servers).isEmpty();
|
assertThat(servers).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -74,7 +74,9 @@ public class NacosServerListTests {
|
|||||||
NamingService namingService = mock(NamingService.class);
|
NamingService namingService = mock(NamingService.class);
|
||||||
|
|
||||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
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);
|
.thenReturn(instances);
|
||||||
|
|
||||||
IClientConfig clientConfig = mock(IClientConfig.class);
|
IClientConfig clientConfig = mock(IClientConfig.class);
|
||||||
@ -82,10 +84,10 @@ public class NacosServerListTests {
|
|||||||
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
|
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
|
||||||
serverList.initWithNiwsConfig(clientConfig);
|
serverList.initWithNiwsConfig(clientConfig);
|
||||||
List<NacosServer> servers = serverList.getInitialListOfServers();
|
List<NacosServer> servers = serverList.getInitialListOfServers();
|
||||||
Assertions.assertThat(servers).hasSize(1);
|
assertThat(servers).hasSize(1);
|
||||||
|
|
||||||
servers = serverList.getUpdatedListOfServers();
|
servers = serverList.getUpdatedListOfServers();
|
||||||
Assertions.assertThat(servers).hasSize(1);
|
assertThat(servers).hasSize(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -106,7 +108,8 @@ public class NacosServerListTests {
|
|||||||
NamingService namingService = mock(NamingService.class);
|
NamingService namingService = mock(NamingService.class);
|
||||||
|
|
||||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
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)
|
.thenReturn(instances.stream().filter(Instance::isHealthy)
|
||||||
.collect(Collectors.toList()));
|
.collect(Collectors.toList()));
|
||||||
|
|
||||||
@ -115,7 +118,7 @@ public class NacosServerListTests {
|
|||||||
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
|
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
|
||||||
serverList.initWithNiwsConfig(clientConfig);
|
serverList.initWithNiwsConfig(clientConfig);
|
||||||
List<NacosServer> servers = serverList.getInitialListOfServers();
|
List<NacosServer> servers = serverList.getInitialListOfServers();
|
||||||
Assertions.assertThat(servers).hasSize(1);
|
assertThat(servers).hasSize(1);
|
||||||
|
|
||||||
NacosServer nacosServer = servers.get(0);
|
NacosServer nacosServer = servers.get(0);
|
||||||
|
|
||||||
@ -143,7 +146,8 @@ public class NacosServerListTests {
|
|||||||
NamingService namingService = mock(NamingService.class);
|
NamingService namingService = mock(NamingService.class);
|
||||||
|
|
||||||
when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
|
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)
|
.thenReturn(instances.stream().filter(Instance::isHealthy)
|
||||||
.collect(Collectors.toList()));
|
.collect(Collectors.toList()));
|
||||||
|
|
||||||
@ -153,7 +157,7 @@ public class NacosServerListTests {
|
|||||||
serverList.initWithNiwsConfig(clientConfig);
|
serverList.initWithNiwsConfig(clientConfig);
|
||||||
|
|
||||||
List<NacosServer> servers = serverList.getUpdatedListOfServers();
|
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().isHealthy()).isEqualTo(true);
|
||||||
assertThat(servers.get(0).getInstance().getMetadata().get("instanceNum"))
|
assertThat(servers.get(0).getInstance().getMetadata().get("instanceNum"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user