5.使用Ribbon实现客户端侧负载均衡
Ribbon实现客户端侧负载均衡
5.1. Ribbon简介
Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。
Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer
后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们也很容易
使用Ribbon实现自定义的负载均衡算法。简单地说,Ribbon是一个客户端负载均衡器。
Ribbon工作时分为两步:
第一步先选择 Eureka Server, 它优先选择在同一个Zone且负载较少的Server;
第二步再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址。其中Ribbon提供了多种策略,例如轮询、随机、根据响应时间加权等。
5.2. 为服务消费者整合Ribbon
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <artifactId>microservice-consumer-movie-ribbon</artifactId>
<packaging>jar</packaging> <parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
实体类
package com.itmuch.cloud.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
Controller类
package com.itmuch.cloud.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.itmuch.cloud.entity.User; @RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate; @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) { return this.restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);
} }
启动类
package com.itmuch.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableEurekaClient
public class RibbonApplication { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
配置文件
spring:
application:
name: microservice-consumer-movie-ribbon
server:
port:
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
访问路径:


5.3. 使用Java代码自定义Ribbon配置
Spring Cloud还允许通过使用@RibbonClient声明其他配置(位于RibbonClientConfiguration之上)来完全控制客户端。例:
启动类
package com.itmuch.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; import com.itmuch.config.TestConfiguration; @SpringBootApplication
@EnableEurekaClient
@RibbonClient(name="microservice-provider-user",configuration=TestConfiguration.class)
public class RibbonApplication { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
package com.itmuch.config; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule; @Configuration
public class TestConfiguration {
@Autowired
IClientConfig config;
@Bean
public IRule ribbonRule(IClientConfig config) {
return new RandomRule();
}
}
图文:

--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
--->172.20.10.4::microservice-provider-user
--->172.20.10.4::microservice-provider-user2
5.4. 使用属性自定义Ribbon配置
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <artifactId>microservice-consumer-movie-ribbon-config</artifactId>
<packaging>jar</packaging> <parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
配置文件
spring:
application:
name: microservice-consumer-movie-ribbon-config
server:
port:
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
microservice-provider-user:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
实体类
package com.itmuch.cloud.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
Controller类
package com.itmuch.cloud.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
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.itmuch.cloud.entity.User; @RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient; @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) { return this.restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);
}
@GetMapping("/test")
public String test(){
ServiceInstance serviceInstance=this.loadBalancerClient.choose("microservice-provider-user");
System.out.println("cyj:--->"+serviceInstance.getHost()+":"+serviceInstance.getPort()+":"+serviceInstance.getServiceId()); ServiceInstance serviceInstance2=this.loadBalancerClient.choose("microservice-provider-user2");
System.out.println("lxq:--->"+serviceInstance2.getHost()+":"+serviceInstance2.getPort()+":"+serviceInstance2.getServiceId()); return "";
}
}
图文

5.5. 脱离Eureka使用Ribbon
Eureka是一种方便的方式来抽象远程服务器的发现,因此您不必在客户端中对其URL进行硬编码,
但如果您不想使用它,Ribbon和Feign仍然是适用的。假设您已经为“商店”申请了@RibbonClient,
并且Eureka未被使用(甚至不在类路径上)。Ribbon客户端默认为已配置的服务器列表,
您可以提供这样的配置
stores:
ribbon:
listOfServers: example.com,google.com
在Ribbon中禁用Eureka使用
ribbon:
eureka:
enabled: false
设置属性ribbon.eureka.enabled = false将明确禁用在Ribbon中使用Eureka。
spring:
application:
name: microservice-consumer-movie-ribbon-config
server:
port:
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
ribbon:
eureka:
enabled: false
microservice-provider-user:
ribbon:
listOfServers: localhost:
5.使用Ribbon实现客户端侧负载均衡的更多相关文章
- SpringCloud系列七:使用Ribbon实现客户端侧负载均衡
1. 回顾 在前面,已经实现了微服务的注册与发现.启动各个微服务时,Eureka Client会把自己的网络信息注册到Eureka Server上. 但是,在生成环境中,各个微服务都会部署多个实例,因 ...
- Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡
接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...
- 手把手带你利用Ribbon实现客户端的负载均衡
之前的文章<SpringCloud搭建注册中心与服务注册>介绍了注册中心的搭建和服务的注册,本文将介绍下服务消费者通过Ribbon调用服务实现负载均衡的过程. 本文目录 一.Ribbon服 ...
- 关于spring cloud eureka整合ribbon实现客户端的负载均衡
1. 实现eureka整合ribbon非常简单, 1.1.首先引入所需maven依赖 <dependency> <groupId>org.springframework.boo ...
- SpringCloud之实现客户端的负载均衡Ribbon(二)
一 Ribbon简介 Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为.为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务 ...
- Spring Cloud Ribbon源码分析---负载均衡实现
上一篇结合 Eureka 和 Ribbon 搭建了服务注册中心,利用Ribbon实现了可配置负载均衡的服务调用.这一篇我们来分析Ribbon实现负载均衡的过程. 从 @LoadBalanced入手 还 ...
- Zuul + Ribbon 脱离Eureka完成负载均衡+重试机制
Zuul + Ribbon 脱离Eureka完成负载均衡+重试机制 因为没有注册中心,所以需要网关对下游服务做负载均衡,然后果断集成Ribbon.中间遇到很多坑,最后终于解决了. 其实Ribbon里面 ...
- 客户端实现负载均衡:springCloud Ribbon的使用
Netfilx发布的负载均衡器,是一个基于http.tcp的客户端负载均衡工具,具有控制http.tcp客户端的行为,为ribbon配置服务提供者的地址后,ribbon就 可以经过springClou ...
- Spring cloud 之Ribbon(二)负载均衡原理
ribbon实现负载均衡的原理 我们从Ribbon实现负载均衡的代码可以看到,Ribbon是通过RestTemPlate实现客户端负载均衡的,准确的说是RestTemPlate上的@LoadBalan ...
随机推荐
- BMP RGB888转RGB565 +上下翻转+缩放
典型的BMP图像文件由四部分组成: (1) 位图头文件数据结构,它包含BMP图像文件的类型.文件大小和位图起始位置等信息: typedef struct tagBITMAPFILEHEADER { ...
- Centos7--从最小化系统发开发环境
Centos7--从最小化系统发开发环境 程序员总是离不开"环境"的困扰,从进入新手村的那一天就开始手动搞各种环境.虽然阿里云学生服务很方便,但是想弄集群真的买不起.正好实验室有 ...
- 一个下午整理的Web前端常见的英文缩写
PV (Page View)页面浏览量 FED(Front-End Development)前端开发 F2E(Front-End Engineer)前端工程师 WWW(World Wide Web)万 ...
- MongoDB divide 使用之mongotempalte divide
需求:求一组数的两个字段计算结果的平均值 如有一组这样的数: 列名1 列名2 列名3 第一组数 a 2 5 第二组数 b 4 8 按照列名1分组 ...
- 基于SAML2.0的SAP云产品Identity Authentication过程介绍
SAP官网的架构图 https://cloudplatform.sap.com/scenarios/usecases/authentication.html 上图介绍了用户访问SAP云平台时经历的Au ...
- spring boot 的一些高级用法
1 spring boot 项目的创建 参考 https://www.cnblogs.com/PerZhu/p/10708809.html 2 首先我们先把Maven里面的配置完成 <depen ...
- Linux学习笔记(一)分区
一.硬件设备文件名 二.设备文件名 /dev/hda1(IDE硬盘接口) /dev/sda1(SCSI硬盘接口.SATA硬盘接口) 其中,a代表第1个硬盘(以此类推,b为第2个硬盘),1代表第1个分区 ...
- CentOS 7 系统初始化
0.安装系统基础依赖工具包 yum install net-tools gcc-c++ wget lrzsz vim ntpdate cronolog make psmisc 1.修改主机名 cent ...
- Gym - 102141D 通项公式 最短路
题目很长,但是意思就是给你n,A,B,C,D n表示有n个城市 A是飞机的重量 B是一个常数表示转机代价 C是单位燃油的价格 D是一个常数 假设一个点到另外一个点的距离为整数L 起飞前的油量为f 则 ...
- 打开myeclipse提示An internal error occurred during: "CheckLicensesAndNotify". com/genuitec/pulse2/client/targetcfg/ui/PulseActivator
打开myeclipse提示An internal error occurred during: "CheckLicensesAndNotify". com/genuitec/pul ...