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());
    }
}

运行结果

相关推荐
这是程序猿15 小时前
基于java的ssm框架旅游在线平台
java·开发语言·spring boot·spring·旅游·旅游在线平台
i***t91915 小时前
基于SpringBoot和PostGIS的云南与缅甸的千里边境线实战
java·spring boot·spring
k***082915 小时前
【监控】spring actuator源码速读
java·spring boot·spring
ruleslol16 小时前
Spring Cloud: Sentinel入门讲解
spring cloud·sentinel
q***471818 小时前
Spring中的IOC详解
java·后端·spring
DKPT19 小时前
ZGC和G1收集器相比哪个更好?
java·jvm·笔记·学习·spring
Hollis Chuang19 小时前
Spring Boot 4.0 正式发布,人麻了。。。
java·spring boot·后端·spring
椎4951 天前
苍穹外卖资源点整理+个人错误解析-Day10-订单状态定时处理(Spring Task)、来单提醒和客户催单
java·后端·spring
Y***h1871 天前
eclipse配置Spring
java·spring·eclipse
p***62991 天前
CVE-2024-38819:Spring 框架路径遍历 PoC 漏洞复现
java·后端·spring