详解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
猜你喜欢
- 在使用 springboot 或者 springcloud 开发的时候,通常为了保证系统的安全性,配置文件中的密码等铭感信息都会进行加密处理
- 带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义。通过
- 一、效果展示初级难度中级难度高级难度测试界面二、项目介绍项目背景扫雷是一款大众类的益智小游戏。根据点击格子出现的数字找出所有非雷格子,同时避
- 本文实例为大家分享了java实现省市区转换成树形结构的具体代码,供大家参考,具体内容如下前言:为什我想写这篇博客呢?第一方面是记录,另一方面
- 今天本文与大家分享如何得到数组中的最大值和最小值的实例。很适合Java初学者复习数组的基本用法与流程控制语句的使用。具体如下:这个程序主要是
- 1、下载64位rxtx for java 链接:http://fizzed.com/oss/rxtx-for-java2、下载下来的包解压后
- 本文实例讲述了JAVA基于数组实现的商品信息查询功能。分享给大家供大家参考,具体如下:综合一维数组和二维数组的相关知识,以及数组排序的多种算
- Spring boot默认使用的是SimpleCacheConfiguration,即使用ConcurrentMapCacheManager
- disruptor不过多介绍了,描述下当前的业务场景,两个应用A,B,应用 A 向应用 B 传递数据 . 数据传送比较快,如果用http直接
- 目录Spring事件驱动源码实战在项目实际开发过程中,我们有很多这样的业务场景:一个事务中处理完一个业务逻辑后需要跟着处理另外一个业务逻辑,
- 本文实例讲述了Android单选按钮对话框用法。分享给大家供大家参考。具体如下:main.xml布局文件<?xml version=&
- 应用场景:在Android开发过程中,有时需要调用手机自身设备的功能,本文侧重摄像头拍照功能的调用。知识点介绍:使用权限:调用手机自身设备功
- 一个简单的微服务架构图本文设计的 Spring Cloud 版本以及用到的 Spring Cloud 组件Spring Cloud Hoxt
- Android获取系统cpu信息,内存,版本,电量等信息 1、CPU频率,CPU信息:/proc/cpuinfo和/proc/stat 通过
- 因为线程重用导致的信息错乱的bugThreadLocal一般用于线程间的数据隔离,通过将数据缓存在ThreadLocal中,可以极大的提升性
- Java事件处理机制和适配器最重要的是理解事件源,监视器,处理事件的接口的概念。1.事件源:是能够产生时间的对象都可以叫事件源,比如文本框,
- Spring框架提供了事务管理的标准实现,且可以通过注解或者XML文件的方式声明和配置事务。通过异步事件的方式解耦服务调用,可以提高程序的响
- 本文实例讲述了WCF实现的计算器功能。分享给大家供大家参考,具体如下:对于WCF,我们有了前面的理论基础,今天通过一个计算器的实例主要给大家
- 以一个M×N的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的
- 1、很多资料说,添加以下代码,可以隐藏地址栏,但我试了很多次,貌似不成功啊。<meta name="apple-mobile