详解spring cloud中使用Ribbon实现客户端的软负载均衡
作者:牛奋lch 发布时间:2022-10-26 15:54:45
开篇
本例是在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的依赖
2、添加负载均衡支持
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
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状态
五,在浏览器中,多次刷新http://localhost:7904/template/2地址
六、测试结果
7900端口服务:
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端口服务:
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端口服务:
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实现了客户端负载均衡的功能
来源:http://blog.csdn.net/liuchuanhong1/article/details/54691566


猜你喜欢
- 所谓动态折线图,就是折线图能随着手指的滑动进行动态绘制,这里很定会产生动画效果。基于这个效果,这里使用SurfaceView进行制图。实现步
- 在TextView中添加文本时有时会改变一些文本字体的颜色,今天主要分享三种实现方法及相关优缺点。1、通过html标签改变文本颜色tv.se
- 前言:1.最近项目上在测试人员压测过程中发现了OOM问题,项目使用springboot搭建项目工程,通过查看日志中包含信息:unable t
- 前言前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务my
- 在说struts2的线程安全之前,先说一下,什么是线程安全?这是一个网友讲的。如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会
- 通常,反射用于动态获取对象的类型、属性和方法等信息。今天带你玩转反射,来汇总一下反射的各种常见操作,捡漏看看有没有你不知道的。获取类型的成员
- AlertDialog的几种用法xml代码:<?xml version="1.0" encoding="
- 自定义封装 banner 组件,供大家参考,具体内容如下1. 效果图预览 2.基本功能一个简单方便的轮播图组件,基于viewpag
- 一、下载rocketmq对应版本源码修改消息存储路径需要修改rocketmq源码,因为rocketmq取的默认路径是user.home路径,
- java带T的时间格式化public static void main(String[] args) { &nb
- 前言此前部门内的一个线上系统上线后内存一路飙高、一段时间后直接占满。协助开发人员去分析定位,发现内存中某个Object的量远远超出了预期的范
- 表单的重复提交: 没有完整的进行一次,先请求表单页面->再提交表单过程而完成数据提交造成的根本原因: 没有完整的进行一次,先请求表单页
- 本文实例为大家分享了Java实现发送邮件并携带附件的具体代码,供大家参考,具体内容如下一、 邮件服务器与传输协议要在网络上实现邮件功能,必须
- 如何配置 * step1: 自定义 * /** * 自定义 * */public class MyInterceptor implemen
- 由于项目上的需要侧滑条目展示收藏按钮,记得之前代码家有写过一个厉害的开源控件 AndroidSwipeLayout 本来准备直接拿来使用,但
- 查了网上的资料,有比较全面的,但有一个问题就是容易出现一个文字和框子不符合的现象。(仔细看,蓝色字母和背景的灰色有空白)要消除这个空白,很简
- 在上节使用了H2之后感觉很爽,很轻便,正好有个项目要求简单,最好不适用外部数据库,于是就想着把H2数据库集成进来,这个系统已经存在了一个Or
- 本文给大家分享Android视频播放器屏幕左侧边随手指上下滑动亮度调节功能的原理实现,具体代码如下所示:import android.app
- 最近刚开始接触机器学习,在这里使用c#模拟一元线性回归,先上图看效果因为源码中有一些控件是自己封装的,所以就不上传可运行的程序集了,贴出核心
- Android 集成FlutterFlutter 作为 Google 开源的新一代跨平台、高性能 UI 框架,旨在帮助开发者高效地构建出跨平