成都网站建设设计

将想法与焦点和您一起共享

NacosDiscoveryClient的使用方法

本篇内容主要讲解“NacosDiscoveryClient的使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“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 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实现了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信息

到此,相信大家对“NacosDiscoveryClient的使用方法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


标题名称:NacosDiscoveryClient的使用方法
本文来源:http://chengdu.cdxwcx.cn/article/jgecpg.html