SpringCloud -根据服务名获取服务运行实例并进行负载均衡

Nacos注册中心

每个服务启动之后都要向注册中心发送服务注册请求,注册中心可以和各个注册客户端自定义协议实现服务注册和发现。

  • pom.xml
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

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进行服务的负载均衡。

相关推荐
试行41 分钟前
C++连接使用 MySQL Connector/C++ 库报错bad allocation
java·c++·mysql
内心如初1 小时前
JAVA-Exploit编写(7)--http-request库文件上传使用续篇
java·网络安全·exploit
计算机-秋大田1 小时前
基于SSM的自助购药小程序设计与实现(LW+源码+讲解)
java·后端·微信小程序·小程序·课程设计
神秘的t1 小时前
javaEE初阶————多线程初阶(2)
java·开发语言
好的吖。1 小时前
JavaEE
java·java-ee
Lojarro2 小时前
【Spring MVC】第二站-Spring MVC请求
java·spring·mvc
小高不明2 小时前
仿 RabbitMQ 的消息队列1(实战项目)
java·spring boot·分布式·spring·rabbitmq·mvc
鹿屿二向箔2 小时前
搭建一个基于Spring Boot的驾校管理系统
java·spring boot·后端
hong_zc2 小时前
Spring MVC(一)
java·spring·mvc
洛阳纸贵2 小时前
基于SpringCloud的广告系统设计与实现(一)
java·开发语言