Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use NacosDiscoveryClient

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report