|
|
|
@ -19,8 +19,6 @@ package com.alibaba.cloud.nacos.discovery;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import org.springframework.cloud.client.ServiceInstance;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
|
|
|
|
import com.alibaba.cloud.nacos.NacosServiceManager;
|
|
|
|
|
import com.alibaba.cloud.nacos.util.NacosServiceInstanceConverter;
|
|
|
|
@ -29,6 +27,7 @@ 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.springframework.cloud.client.ServiceInstance;
|
|
|
|
|
/**
|
|
|
|
|
* @author <a href="mailto:echooy.mxq@gmail.com">echooymxq</a>
|
|
|
|
|
* @author changjin wei(魏昌进)
|
|
|
|
@ -39,8 +38,7 @@ public class NacosServiceDiscovery {
|
|
|
|
|
|
|
|
|
|
private NacosServiceManager nacosServiceManager;
|
|
|
|
|
|
|
|
|
|
public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties,
|
|
|
|
|
NacosServiceManager nacosServiceManager) {
|
|
|
|
|
public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties, NacosServiceManager nacosServiceManager) {
|
|
|
|
|
this.discoveryProperties = discoveryProperties;
|
|
|
|
|
this.nacosServiceManager = nacosServiceManager;
|
|
|
|
|
}
|
|
|
|
@ -54,8 +52,7 @@ public class NacosServiceDiscovery {
|
|
|
|
|
*/
|
|
|
|
|
public List<ServiceInstance> getInstances(String serviceId) throws NacosException {
|
|
|
|
|
String group = discoveryProperties.getGroup();
|
|
|
|
|
List<Instance> instances = namingService().selectInstances(serviceId, group,
|
|
|
|
|
true);
|
|
|
|
|
List<Instance> instances = namingService().selectInstances(serviceId, group, true);
|
|
|
|
|
return hostToServiceInstanceList(instances, serviceId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -67,13 +64,11 @@ public class NacosServiceDiscovery {
|
|
|
|
|
*/
|
|
|
|
|
public List<String> getServices() throws NacosException {
|
|
|
|
|
String group = discoveryProperties.getGroup();
|
|
|
|
|
ListView<String> services = namingService().getServicesOfServer(1,
|
|
|
|
|
Integer.MAX_VALUE, group);
|
|
|
|
|
ListView<String> services = namingService().getServicesOfServer(1, Integer.MAX_VALUE, group);
|
|
|
|
|
return services.getData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<ServiceInstance> hostToServiceInstanceList(
|
|
|
|
|
List<Instance> instances, String serviceId) {
|
|
|
|
|
public static List<ServiceInstance> hostToServiceInstanceList(List<Instance> instances, String serviceId) {
|
|
|
|
|
List<ServiceInstance> result = new ArrayList<>(instances.size());
|
|
|
|
|
for (Instance instance : instances) {
|
|
|
|
|
ServiceInstance serviceInstance = NacosServiceInstanceConverter.fromInstanceAndServiceId(instance, serviceId);
|
|
|
|
|