您好,登錄后才能下訂單哦!
本篇內容主要講解“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信息
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信息
到此,相信大家對“NacosDiscoveryClient的使用方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。