|
|
|
@ -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
|
|
|
|
@ -58,14 +57,13 @@ public class NacosDiscoveryClient implements DiscoveryClient {
|
|
|
|
|
List<Instance> instances = discoveryProperties.namingServiceInstance()
|
|
|
|
|
.selectInstances(serviceId, true);
|
|
|
|
|
return hostToServiceInstanceList(instances, serviceId);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
"Can not get hosts from nacos server. serviceId: " + serviceId, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ServiceInstance hostToServiceInstance(Instance instance,
|
|
|
|
|
public static ServiceInstance hostToServiceInstance(Instance instance,
|
|
|
|
|
String serviceId) {
|
|
|
|
|
NacosServiceInstance nacosServiceInstance = new NacosServiceInstance();
|
|
|
|
|
nacosServiceInstance.setHost(instance.getIp());
|
|
|
|
@ -87,7 +85,7 @@ public class NacosDiscoveryClient implements DiscoveryClient {
|
|
|
|
|
return nacosServiceInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<ServiceInstance> hostToServiceInstanceList(
|
|
|
|
|
public static List<ServiceInstance> hostToServiceInstanceList(
|
|
|
|
|
List<Instance> instances, String serviceId) {
|
|
|
|
|
List<ServiceInstance> result = new ArrayList<>(instances.size());
|
|
|
|
|
for (Instance instance : instances) {
|
|
|
|
@ -103,8 +101,7 @@ public class NacosDiscoveryClient implements DiscoveryClient {
|
|
|
|
|
ListView<String> services = discoveryProperties.namingServiceInstance()
|
|
|
|
|
.getServicesOfServer(1, Integer.MAX_VALUE);
|
|
|
|
|
return services.getData();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("get service name from nacos server fail,", e);
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|