|
| 1 | +package cn.springcloud.gray.server.plugin.discovery.nacos; |
| 2 | + |
| 3 | +import cn.springcloud.gray.model.InstanceInfo; |
| 4 | +import cn.springcloud.gray.model.InstanceStatus; |
| 5 | +import cn.springcloud.gray.server.discovery.ServiceDiscovery; |
| 6 | +import cn.springcloud.gray.server.discovery.ServiceInfo; |
| 7 | +import com.alibaba.cloud.nacos.NacosDiscoveryProperties; |
| 8 | +import com.alibaba.nacos.api.exception.NacosException; |
| 9 | +import com.alibaba.nacos.api.naming.pojo.Instance; |
| 10 | +import org.apache.commons.collections.ListUtils; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +public class NacosServiceDiscovery implements ServiceDiscovery { |
| 19 | + |
| 20 | + private static final Logger log = LoggerFactory.getLogger(NacosServiceDiscovery.class); |
| 21 | + |
| 22 | + private NacosDiscoveryProperties discoveryProperties; |
| 23 | + |
| 24 | + public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties) { |
| 25 | + this.discoveryProperties = discoveryProperties; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public List<ServiceInfo> listAllSerivceInfos() { |
| 30 | + return getServices().stream().map(service -> new ServiceInfo(service)).collect(Collectors.toList()); |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public ServiceInfo getServiceInfo(String serviceId) { |
| 36 | + if (!getServices().contains(serviceId)) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + return new ServiceInfo(serviceId); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public List<InstanceInfo> listInstanceInfos(String serviceId) { |
| 45 | + List<Instance> instances = null; |
| 46 | + try { |
| 47 | + instances = discoveryProperties.namingServiceInstance().getAllInstances(serviceId, false); |
| 48 | + } catch (NacosException e) { |
| 49 | + log.error(e.getMessage(), e); |
| 50 | + return ListUtils.EMPTY_LIST; |
| 51 | + } |
| 52 | + return instances.stream().map(this::createInstanceInfo).collect(Collectors.toList()); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public InstanceInfo getInstanceInfo(String serviceId, String instanceId) { |
| 57 | + return getInstanceInfos(serviceId).get(instanceId); |
| 58 | + } |
| 59 | + |
| 60 | + private List<String> getServices() { |
| 61 | + try { |
| 62 | + return discoveryProperties.namingServiceInstance() |
| 63 | + .getServicesOfServer(1, Integer.MAX_VALUE).getData(); |
| 64 | + } catch (NacosException e) { |
| 65 | + return Collections.emptyList(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private InstanceInfo createInstanceInfo(Instance instance) { |
| 70 | + InstanceStatus instanceStatus = InstanceStatus.DOWN; |
| 71 | + if (instance.isEnabled()) { |
| 72 | + if (instance.isHealthy()) { |
| 73 | + instanceStatus = InstanceStatus.UP; |
| 74 | + } else { |
| 75 | + instanceStatus = InstanceStatus.OUT_OF_SERVICE; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return InstanceInfo.builder() |
| 80 | + .serviceId(instance.getServiceName()) |
| 81 | + .instanceId(instance.getInstanceId()) |
| 82 | + .host(instance.getIp()) |
| 83 | + .port(instance.getPort()) |
| 84 | + .instanceStatus(instanceStatus) |
| 85 | + .build(); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments