千家信息网

Spring Cloud中怎么使用Ribbon实现负载均衡

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章给大家介绍Spring Cloud中怎么使用Ribbon实现负载均衡,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。Ribbon简介Ribbon is a client s
千家信息网最后更新 2026年10月31日Spring Cloud中怎么使用Ribbon实现负载均衡

这篇文章给大家介绍Spring Cloud中怎么使用Ribbon实现负载均衡,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

Ribbon简介

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.

Ribbon是负载均衡客户端,可以很好的控制HTTP和TCP客户端的行为。 Feign已经集成了Ribbon。

Spring Cloud Netflix默认为ribbon(BeanType beanName:ClassName)提供以下bean:

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl

  • IRule ribbonRule: ZoneAvoidanceRule

  • IPing ribbonPing: NoOpPing

  • ServerList ribbonServerList: ConfigurationBasedServerList

  • ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

准备工作

这篇文章基于上一篇文章项目,启动eureka-server项目;启动eureka-client项目,端口为8040; 将eureka-client的配置文件端口改为8041,并启动,同一个项目修改端口号,启动多个实例,只需要在IDEA中勾选Allow parallel run:

此时你会发现注册服务中注册了两个eureka-client实例,相当于起了2个节点的集群。 访问http://localhost:9090:

新建一个服务消费者

使用Spring Initializr新建一个项目,取名为ribbon-service, 在Spring Cloud Discovery中勾选Eureka Discovery Client,在Spring Cloud Routing中勾选Ribbon,在Web中勾选Spring Web:

创建成功后,项目pom.xml如下:

    4.0.0            org.springframework.boot        spring-boot-starter-parent        2.1.9.RELEASE                 com.noodles.mars    ribbon-service    0.0.1-SNAPSHOT    ribbon-service    Ribbon Service            1.8        Greenwich.SR3                            org.springframework.cloud            spring-cloud-starter-netflix-eureka-client                            org.springframework.cloud            spring-cloud-starter-netflix-ribbon                            org.springframework.boot            spring-boot-starter-web                            org.springframework.boot            spring-boot-starter-test            test                                                    org.springframework.cloud                spring-cloud-dependencies                ${spring-cloud.version}                pom                import                                                                org.springframework.boot                spring-boot-maven-plugin                        

ribbon-service配置服务中心地址、应用名、端口,配置文件内容:

server:  port: 8050spring:  application:    name: ribbon-serviceeureka:  client:    service-url:       defaultZone: http://localhost:9090/eureka/

在项目启动类上注解@EnableDiscoveryClient, 开启向服务中心注册;定义一个 RestTemplate Bean, 并注解@LoadBalanced开启负载均衡功能:

@EnableEurekaClient@SpringBootApplicationpublic class RibbonServiceApplication {    public static void main(String[] args) {        SpringApplication.run(RibbonServiceApplication.class, args);    }    @Bean    @LoadBalanced    public RestTemplate restTemplate() {        return new RestTemplate();    }}

写一个controller, 通过之前定义的RestTemplate来消费eureka-client服务的/hello接口,url中使用应用名,ribbon会根据应用名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名:

@RestControllerpublic class HelloController {    private final RestTemplate restTemplate;    @Autowired    public HelloController(RestTemplate restTemplate) {        this.restTemplate = restTemplate;    }    @GetMapping("/hello")    public String hello(@RequestParam("name") String name) {        return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class);    }}

在浏览器上多次访问 http://localhost:8050/hello?name=Mars :

Hello, My name is Mars, I'm from port: 8040Hello, My name is Mars, I'm from port: 8041

关于Spring Cloud中怎么使用Ribbon实现负载均衡就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

服务 项目 均衡 实例 内容 端口 篇文章 应用 配置 客户 文件 更多 服务中心 注解 帮助 消费 不错 成功 两个 兴趣 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网络科技 哈尔滨大商圈互联网科技有限公司 广州大一互联网络科技有限公司 互联网科技公司的优势 互联网扶贫科技 互联网科技卖菜 上海互联网科技有限公司 互联网的科技创新 互联网科技公司电话 齐家互联网科技有限公司 互联网科技汛 互联网科技类展会 未来五年的互联网科技 洛阳昕诺飞互联网科技有限公司 广西互联网科技孵化中心 阿里互联网科技企业 科技赋能视角下互联网保险发展 昆明中奇联合互联网科技 黑龙江互联网科技公司 如何强化互联网金融科技 中国互联网科技工作委员会毛军 A股互联网金融科技公司 与互联网科技公司 伴你行互联网科技有限公司 山东吉时兔互联网科技有限公司 电子科技大学互联网创业大赛 国大互联网教育科技有限公司工作 互联网科技如何创业 21世纪互联网科技成就 广东猫王互联网科技有限公司电话
0