详解spring cloud中使用Ribbon实现客户端的软负载均衡

2025-05-29 0 52

开篇

本例是在springboot整合h2内存数据库,实现单元测试与数据库无关性和使用resttemplate消费spring boot的restful服务两个示例的基础上改造而来

在使用resttemplate来消费spring boot的restful服务示例中,我们提到,调用spring boot服务的时候,需要将服务的url写死或者是写在配置文件中,但这两种方式,无论哪一种,一旦ip地址发生了变化,都需要改动程序,并重新部署服务,使用ribbon的时候,可以有效的避免这个问题。

前言:

软负载均衡的实现方式有两种,分别是服务端的负载均衡和客户端的负载均衡

服务端负载均衡:当浏览器向后台发出请求的时候,会首先向反向代理服务器发送请求,反向代理服务器会根据客户端部署的ip:port映射表以及负载均衡策略,来决定向哪台服务器发送请求,一般会使用到nginx反向代理技术。

客户端负载均衡:当浏览器向后台发出请求的时候,客户端会向服务注册器(例如:eureka server),拉取注册到服务器的可用服务信息,然后根据负载均衡策略,直接命中哪台服务器发送请求。这整个过程都是在客户端完成的,并不需要反向代理服务器的参与。

一、启动eureka server

请参考该例:spring cloud中启动eureka server

二、启动微服务,并注册到eureka server上

spring cloud-将spring boot服务注册到eureka server上

为了演示负载均衡的效果,再启动一个为服务,注意需要将端口号改成不一致

三、添加ribbon支持

1、添加ribbon的依赖

详解spring cloud中使用Ribbon实现客户端的软负载均衡

2、添加负载均衡支持

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
package com.chhliu.springboot.restful;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.boot.springapplication;

import org.springframework.boot.autoconfigure.springbootapplication;

import org.springframework.boot.web.client.resttemplatebuilder;

import org.springframework.cloud.client.loadbalancer.loadbalanced;

import org.springframework.cloud.netflix.eureka.enableeurekaclient;

import org.springframework.context.annotation.bean;

import org.springframework.web.client.resttemplate;

@springbootapplication

@enableeurekaclient

public class springbootresttemplateapplication {

@autowired

private resttemplatebuilder builder;

@bean

@loadbalanced // 添加负载均衡支持,很简单,只需要在resttemplate上添加@loadbalanced注解,那么resttemplate即具有负载均衡的功能,如果不加@loadbalanced注解的话,会报java.net.unknownhostexception:springboot-h2异常,此时无法通过注册到eureka server上的服务名来调用服务,因为resttemplate是无法从服务名映射到ip:port的,映射的功能是由loadbalancerclient来实现的。

public resttemplate resttemplate() {

return builder.build();

}

public static void main(string[] args) {

springapplication.run(springbootresttemplateapplication.class, args);

}

}

3、修改调用微服务的url

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
package com.chhliu.springboot.restful.controller;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.web.bind.annotation.getmapping;

import org.springframework.web.bind.annotation.pathvariable;

import org.springframework.web.bind.annotation.restcontroller;

import org.springframework.web.client.resttemplate;

import com.chhliu.springboot.restful.vo.user;

@restcontroller

public class resttemplatecontroller {

@autowired

private resttemplate resttemplate;

@getmapping("/template/{id}")

public user findbyid(@pathvariable long id) {// 将原来的ip:port的形式,改成注册到eureka server上的应用名即可

user u = this.resttemplate.getforobject("http://springboot-h2/user/" + id, user.class);

system.out.println(u);

return u;

}

}

四、查看eureka server状态

详解spring cloud中使用Ribbon实现客户端的软负载均衡

五,在浏览器中,多次刷新http://localhost:7904/template/2地址

六、测试结果

7900端口服务:

?

1

2

3

4
hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?

hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?

hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?

hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?

7901端口服务:

?

1

2

3

4

5
hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?

hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?

hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?

hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?

hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?

7904端口服务:

?

1

2

3

4

5

6

7

8

9

10
user [id=2, username=user2, name=李四, age=20, balance=100.00]

2017-01-23 09:58:05.682 info 7436 --- [erlistupdater-0] c.netflix.config.chaineddynamicproperty : flipping property: springboot-h2.ribbon.activeconnectionslimit to use next property: niws.loadbalancer.availabilityfilteringrule.activeconnectionslimit = 2147483647

user [id=2, username=user2, name=李四, age=20, balance=100.00]

user [id=2, username=user2, name=李四, age=20, balance=100.00]

user [id=2, username=user2, name=李四, age=20, balance=100.00]

user [id=2, username=user2, name=李四, age=20, balance=100.00]

user [id=2, username=user2, name=李四, age=20, balance=100.00]

user [id=2, username=user2, name=李四, age=20, balance=100.00]

user [id=2, username=user2, name=李四, age=20, balance=100.00]

user [id=2, username=user2, name=李四, age=20, balance=100.00]

从上面的测试结果可以看出,总共调了7904端口服务9次,其中7904端口服务调7900端口服务4次,调7901端口5次,刚好是9次

经过上面的几个步骤,就基本使用ribbon实现了客户端负载均衡的功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/liuchuanhong1/article/details/54691566

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解spring cloud中使用Ribbon实现客户端的软负载均衡 https://www.kuaiidc.com/112868.html

相关文章

发表评论
暂无评论