SpringCloud -根据服务名获取服务运行实例并进行负载均衡
Nacos注册中心
每个服务启动之后都要向注册中心发送服务注册请求,注册中心可以和各个注册客户端自定义协议实现服务注册和发现。
- pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
- 启动服务
获取服务实例测试
@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>
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进行服务的负载均衡。