转载

聊聊NacosDiscoveryClient

本文主要研究一下NacosDiscoveryClient

NacosDiscoveryClient

spring-cloud-alibaba-0.9.0.RELEASE/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/discovery/NacosDiscoveryClient.java

public class NacosDiscoveryClient implements DiscoveryClient {

    private static final Logger log = LoggerFactory.getLogger(NacosDiscoveryClient.class);
    public static final String DESCRIPTION = "Spring Cloud Nacos Discovery Client";

    private NacosDiscoveryProperties discoveryProperties;

    public NacosDiscoveryClient(NacosDiscoveryProperties discoveryProperties) {
        this.discoveryProperties = discoveryProperties;
    }

    @Override
    public String description() {
        return DESCRIPTION;
    }

    @Override
    public List<ServiceInstance> getInstances(String serviceId) {
        try {
            List<Instance> instances = discoveryProperties.namingServiceInstance()
                    .selectInstances(serviceId, true);
            return hostToServiceInstanceList(instances, serviceId);
        }
        catch (Exception e) {
            throw new RuntimeException(
                    "Can not get hosts from nacos server. serviceId: " + serviceId, e);
        }
    }

    private static ServiceInstance hostToServiceInstance(Instance instance,
            String serviceId) {
        NacosServiceInstance nacosServiceInstance = new NacosServiceInstance();
        nacosServiceInstance.setHost(instance.getIp());
        nacosServiceInstance.setPort(instance.getPort());
        nacosServiceInstance.setServiceId(serviceId);

        Map<String, String> metadata = new HashMap<>();
        metadata.put("nacos.instanceId", instance.getInstanceId());
        metadata.put("nacos.weight", instance.getWeight() + "");
        metadata.put("nacos.healthy", instance.isHealthy() + "");
        metadata.put("nacos.cluster", instance.getClusterName() + "");
        metadata.putAll(instance.getMetadata());
        nacosServiceInstance.setMetadata(metadata);

        if (metadata.containsKey("secure")) {
            boolean secure = Boolean.parseBoolean(metadata.get("secure"));
            nacosServiceInstance.setSecure(secure);
        }
        return nacosServiceInstance;
    }

    private static List<ServiceInstance> hostToServiceInstanceList(
            List<Instance> instances, String serviceId) {
        List<ServiceInstance> result = new ArrayList<>(instances.size());
        for (Instance instance : instances) {
            result.add(hostToServiceInstance(instance, serviceId));
        }
        return result;
    }

    @Override
    public List<String> getServices() {

        try {
            ListView<String> services = discoveryProperties.namingServiceInstance()
                    .getServicesOfServer(1, Integer.MAX_VALUE);
            return services.getData();
        }
        catch (Exception e) {
            log.error("get service name from nacos server fail,", e);
            return Collections.emptyList();
        }
    }
}
  • NacosDiscoveryClient实现了org.springframework.cloud.client.discovery.DiscoveryClient接口;其getInstances方法从discoveryProperties获取NamingService,然后通过NamingService.selectInstances获取实例信息,然后转换为org.springframework.cloud.client.ServiceInstance类型;getServices方法通过NamingService.getServicesOfServer获取services信息

NacosDiscoveryClientAutoConfiguration

spring-cloud-alibaba-0.9.0.RELEASE/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/discovery/NacosDiscoveryClientAutoConfiguration.java

@Configuration
@ConditionalOnNacosDiscoveryEnabled
@AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class,
        CommonsClientAutoConfiguration.class })
public class NacosDiscoveryClientAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public NacosDiscoveryProperties nacosProperties() {
        return new NacosDiscoveryProperties();
    }

    @Bean
    public DiscoveryClient nacosDiscoveryClient(
            NacosDiscoveryProperties discoveryProperties) {
        return new NacosDiscoveryClient(discoveryProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(value = "spring.cloud.nacos.discovery.watch.enabled", matchIfMissing = true)
    public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties) {
        return new NacosWatch(nacosDiscoveryProperties);
    }
}
  • NacosDiscoveryClientAutoConfiguration注册了NacosDiscoveryClient

小结

NacosDiscoveryClient实现了org.springframework.cloud.client.discovery.DiscoveryClient接口;其getInstances方法从discoveryProperties获取NamingService,然后通过NamingService.selectInstances获取实例信息,然后转换为org.springframework.cloud.client.ServiceInstance类型;getServices方法通过NamingService.getServicesOfServer获取services信息

doc

  • NacosDiscoveryClient
原文  https://segmentfault.com/a/1190000019911849
正文到此结束
Loading...