To filter null , disable or unhealthy Nacos instances
@@ -65,6 +65,9 @@ public class NacosDiscoveryClient implements DiscoveryClient {
public static ServiceInstance hostToServiceInstance(Instance instance,
String serviceId) {
+ if (instance == null || !instance.isEnabled() || !instance.isHealthy()) {
+ return null;
+ }
NacosServiceInstance nacosServiceInstance = new NacosServiceInstance();
nacosServiceInstance.setHost(instance.getIp());
nacosServiceInstance.setPort(instance.getPort());
@@ -89,7 +92,10 @@ public class NacosDiscoveryClient implements DiscoveryClient {
List<Instance> instances, String serviceId) {
List<ServiceInstance> result = new ArrayList<>(instances.size());
for (Instance instance : instances) {
- result.add(hostToServiceInstance(instance, serviceId));
+ ServiceInstance serviceInstance = hostToServiceInstance(instance, serviceId);
+ if (serviceInstance != null) {
+ result.add(serviceInstance);
+ }
}
return result;
}
