Nacos注册中心
每个服务启动之后都要向注册中心发送服务注册请求,注册中心可以和各个注册客户端自定义协议实现服务注册和发现。
- pom.xml
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
- 启动服务
获取服务实例测试
java">@EnableDiscoveryClient
@SpringBootApplication
public class Main83
{public static void main(String[] args){ConfigurableApplicationContext applicationContext = SpringApplication.run(Main83.class, args);DiscoveryClient discoveryClient = applicationContext.getBean(DiscoveryClient.class);List<ServiceInstance> instances = discoveryClient.getInstances("nacos-payment-provider");for (ServiceInstance instance : instances) {System.out.println("instance.getHost() = " + instance.getHost());System.out.println("instance.getPort() = " + instance.getPort());}NacosDiscoveryClient nacosDiscoveryClient = applicationContext.getBean(NacosDiscoveryClient.class);List<ServiceInstance> serviceInstances = nacosDiscoveryClient.getInstances("nacos-payment-provider");for (ServiceInstance instance : serviceInstances) {System.out.println("Nacos instance.getHost() = " + instance.getHost());System.out.println("instance.getPort() = " + instance.getPort());}LoadBalancerClient balancerClient = applicationContext.getBean(LoadBalancerClient.class);System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());}}
在引入注册中心相关的依赖后,注册中心的相关API会实现SpringCloud规范,自动给容器中存入DiscoveryClient对象,引入了Nacos,就是NacosDiscoveryClient。通过DiscoveryClient接口提供的能力可以从注册中心实时拉取服务列表。
pom.xml
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency>
java">LoadBalancerClient balancerClient = applicationContext.getBean(LoadBalancerClient.class);System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());System.out.println("balancerClient.choose(\"nacos-payment-provider\").getPort() = " + balancerClient.choose("nacos-payment-provider").getPort());
LoadBalancerClient类会自动使用容器中的DiscoveryClient进行服务的负载均衡。