springcloud负载均衡测试类

坐标

xml 复制代码
<!--负载均衡-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

测试类

java 复制代码
package org.wh;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;

@SpringBootTest
public class OrderLoadBalancerClientTest {

    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Test
    public void test() {
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        System.out.println(choose.getHost()+":"+choose.getPort());
         choose = loadBalancerClient.choose("service-product");

        System.out.println(choose.getHost()+":"+choose.getPort());
        choose = loadBalancerClient.choose("service-product");

        System.out.println(choose.getHost()+":"+choose.getPort());
        choose = loadBalancerClient.choose("service-product");

        System.out.println(choose.getHost()+":"+choose.getPort());
        choose = loadBalancerClient.choose("service-product");

        System.out.println(choose.getHost()+":"+choose.getPort());
        choose = loadBalancerClient.choose("service-product");
        System.out.println(choose.getHost()+":"+choose.getPort());
    }
}

运行结果

相关推荐
半梦半醒*1 小时前
ansible阶段练习题
linux·运维·自动化·ansible·负载均衡·运维开发
IT·陈寒3 小时前
怎么这么多 StringUtils —— Apache、Spring、Hutool 全面对比
java·spring·apache
青鱼入云3 小时前
【面试场景题】spring应用启动时出现内存溢出怎么排查
spring·面试·职场和发展
杨杨杨大侠7 小时前
实战案例:商品详情页数据聚合服务的技术实现
java·spring·github
杨杨杨大侠7 小时前
实战案例:保险理赔线上审核系统的技术实现
java·spring·github
小马爱打代码16 小时前
Spring Cloud LoadBalancer 核心原理
spring cloud
FrankYoou21 小时前
Spring Boot + Spring MVC 项目结构
spring boot·spring·springmvc
zzywxc7871 天前
AI行业应用:金融、医疗、教育、制造业的落地案例全解析
人工智能·深度学习·spring·机器学习·金融·数据挖掘
小马爱打代码1 天前
Spring Cloud Eureka 核心原理
spring cloud·eureka
来一杯龙舌兰1 天前
【Sharding-JDBC】Spring/Spring Boot 集成 Sharding-JDBC,分表策略与 API、YAML 配置实践
java·spring boot·spring