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

相关推荐
工业互联网专业10 分钟前
基于springboot+vue的二手车交易系统
java·vue.js·spring boot·毕业设计·源码·课程设计·二手车交易系统
IT技术图谱11 分钟前
【绝非标题党】Android 如何优化网络请求
java·面试
kfhj23 分钟前
Spring Cloud 框架为什么能处理高并发
后端·spring·spring cloud
殷世杰30 分钟前
springai完成mcp+知识库实现智能助手
java
同志3271335 分钟前
手搓Java控制台进度条打印工具
java
Excuse_lighttime1 小时前
JAVA阻塞队列
java·开发语言·jvm
luoluoal2 小时前
Java项目之基于ssm的怀旧唱片售卖系统(源码+文档)
java·mysql·mybatis·ssm·源码
green5+12 小时前
LeetCode18四数之和
java·开发语言·算法
lzjava20242 小时前
Redis数据结构之Set
java·数据结构·redis
Excuse_lighttime3 小时前
JAVA单例模式
java·开发语言·单例模式