Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use NacosDiscoveryClient

Shulou Source: shulou.com Published: 2022-06-02 07:28:01 10月06日 Update

This article mainly explains "how to use NacosDiscoveryClient". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use NacosDiscoveryClient.

Order

This paper mainly studies 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 NacosDiscoveryClient"; private NacosDiscoveryProperties discoveryProperties; public NacosDiscoveryClient (NacosDiscoveryProperties discoveryProperties) {this.discoveryProperties = discoveryProperties;} @ Override public String description () {return DESCRIPTION } @ Override public List getInstances (String serviceId) {try {List 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 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 hostToServiceInstanceList (List instances, String serviceId) {List result = new ArrayList (instances.size ()); for (Instance instance: instances) {result.add (hostToServiceInstance (instance, serviceId));} return result } @ Override public List getServices () {try {ListView 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 implements the org.springframework.cloud.client.discovery.DiscoveryClient interface; its getInstances method obtains NamingService from discoveryProperties, then obtains instance information through NamingService.selectInstances, and then converts it to org.springframework.cloud.client.ServiceInstance type; getServices method obtains services information through NamingService.getServicesOfServer

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 is registered with NacosDiscoveryClient

Summary

NacosDiscoveryClient implements the org.springframework.cloud.client.discovery.DiscoveryClient interface; its getInstances method obtains NamingService from discoveryProperties, then obtains instance information through NamingService.selectInstances, and then converts it to org.springframework.cloud.client.ServiceInstance type; getServices method obtains services information through NamingService.getServicesOfServer

At this point, I believe that you have a deeper understanding of "the use of NacosDiscoveryClient", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Methods information usage content examples interfaces types learning practicality deeper interest practicality practicality summary simple operation more friends websites channels query Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS OPPO Reno Microsoft Redmi