引言
說完了注冊(cè)中心Eureka,雖然Eureka可以實(shí)現(xiàn)服務(wù)的發(fā)現(xiàn)和調(diào)用,但在微服務(wù)體系中,服務(wù)的發(fā)現(xiàn)和調(diào)用往往是需要伴隨著負(fù)載均衡這個(gè)概念一體的。而在SpringCloud中自然也存在著與Eureka配套的負(fù)載均衡組件,也就是Ribbon組件。
Ribbon介紹
Spring Cloud Ribbon是基于Netflix Ribbon實(shí)現(xiàn)的一套 客戶端 負(fù)載均衡 工具
簡單的說,Ribbon是Netflix發(fā)布的開源項(xiàng)目,主要功能是提供客戶端的軟件負(fù)載均衡算法,將Netflix的中間層服務(wù)連接在一起。Ribbon客戶端組件提供一系列完善的配置項(xiàng)如連接超時(shí),重試等。簡單的說,就是在配置文件中列出Load Balancer(簡稱LB)后面所有的機(jī)器,Ribbon會(huì)自動(dòng)的幫助你基于某種規(guī)則(如簡單輪詢,隨機(jī)連接等)去連接這次額機(jī)器。我們也很容易使用Ribbon實(shí)現(xiàn)自定義的負(fù)載均衡算法。
使用 Ribbon 工作原理
所有的項(xiàng)目都會(huì)注冊(cè)到 Eureka 中,Eureka 允許不同項(xiàng)目的 spring.application.name 相同。當(dāng)相同時(shí)會(huì)認(rèn)為是這些項(xiàng)目是一個(gè)集群,所以同一個(gè)項(xiàng)目部署多次都是設(shè)置應(yīng)用程序名相同。
Application Client 會(huì)從 Eureka 中根據(jù) spring.application.name 加載 Application Service 的列表。根據(jù)設(shè)定的負(fù)載均衡算法,從列表中取出一個(gè) URL,到此 Ribbon 的事情結(jié)束。剩下的事情由程序員自己進(jìn)行技術(shù)選型,選擇一個(gè) HTTP 協(xié)議工具,通過這個(gè) URL 調(diào)用 Application Service。
注意:以下事情和 Ribbon 沒有關(guān)系的
Application Service 注冊(cè)到 Eureka 過程。這是 Eureka 的功能。
Application Client 從 Eureka 取出注冊(cè)列表。這是 Eureka 的功能。
Application Client 通過 URL 訪問 Application Service 。這個(gè)根據(jù)自己使用的 HTTP 工具。
只有 Application Client 從 Eureka 中取出列表后進(jìn)行負(fù)載均衡算法的過程和Ribbon有關(guān)。
Ribbon使用
- 1.導(dǎo)入依賴,需要在消費(fèi)者中導(dǎo)入ribbon和eureka依賴
<!--Ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!--Eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
- 2.配置文件
#Eureka
eureka:
client:
register-with-eureka: false #不在注冊(cè)中心注冊(cè)自己
service-url:
defaultZone : http://eureka8001.com:8001/eureka/,http://eureka8002.com:8002/eureka/,http://eureka8003.com:8003/eureka/
- 3.添加負(fù)載均衡注解
@Configuration
public class BeanConfig {
@Bean
@LoadBalanced //Ribbon開啟負(fù)載均衡
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
- 4.啟動(dòng)類添加Eureka注解
配置多個(gè)服務(wù)提供者
為了明確的表示,注冊(cè)中心和負(fù)載均衡的功能實(shí)現(xiàn),啟動(dòng)三個(gè)Eureka服務(wù),啟動(dòng)三個(gè)服務(wù)提供者,啟動(dòng)一個(gè)服務(wù)消費(fèi)者,目標(biāo)實(shí)現(xiàn)==調(diào)用服務(wù)消費(fèi)者相同的接口,調(diào)用不同的服務(wù)提供者的服務(wù)。==不同的服務(wù)返回不同的id:7001,7002,7003
項(xiàng)目啟動(dòng)后,每一個(gè)eureka中有兩個(gè)eureka服務(wù),相同的application有對(duì)應(yīng)三個(gè)服務(wù)提供者
訪問相同的服務(wù)地址,負(fù)載均衡到不同的服務(wù)提供者,不影響返回的結(jié)果。(id為了區(qū)分不同的服務(wù)而設(shè)置)文章來源:http://www.zghlxwxcb.cn/news/detail-447905.html
Ribbon核心組件IRule實(shí)現(xiàn)負(fù)載均衡算法
IRule:根據(jù)特定算法從服務(wù)列表中選取一個(gè)要訪問的服務(wù)。默認(rèn)是輪詢
每一接口實(shí)現(xiàn),都是一個(gè)負(fù)載均衡策略
設(shè)置隨機(jī)的負(fù)載均衡策略
在配置類中,生成一個(gè)有spring管理的IRule對(duì)象,當(dāng)項(xiàng)目啟動(dòng)后如果有spring管理的bean,則用相應(yīng)的負(fù)載均衡策略,如果沒有,則使用默認(rèn)的隨機(jī)策略。文章來源地址http://www.zghlxwxcb.cn/news/detail-447905.html
@Configuration
public class BeanConfig {
@Bean
@LoadBalanced //Ribbon開啟負(fù)載君和的注解
//AvailabilityFilteringRule 過濾掉跳閘,加載慢。。的服務(wù),其他的輪詢
//RandomRule 隨機(jī)
//RetryRule 重試
//RoundRobinRule 輪詢
//WeightedResponseTimeRule 設(shè)置權(quán)重
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
//設(shè)置隨機(jī)的負(fù)載均衡策略
@Bean
public IRule myRule(){
return new RandomRule();
}
}
自定義負(fù)載均衡策略(依次訪問3次,循環(huán)訪問)
- .模仿實(shí)現(xiàn)類
- 具體代碼實(shí)現(xiàn)
package com.kuang.myRule;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class myRule extends AbstractLoadBalancerRule {
private int total =0;
private int currentIndex =0;
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
} else {
Server server = null;
while(server == null) {
if (Thread.interrupted()) {
return null;
}
List<Server> upList = lb.getReachableServers();
List<Server> allList = lb.getAllServers();
int serverCount = allList.size();
if (serverCount == 0) {
return null;
}
//自定義部分代碼開始
if(total<3){
server =upList.get(currentIndex);
total++;
}else{
total=1;
currentIndex++;
if(currentIndex>allList.size()-1){//如果有服務(wù)掛起,不會(huì)報(bào)錯(cuò)
currentIndex=0;
}
}
server =upList.get(currentIndex);
//自定義部分代碼結(jié)束
//原隨機(jī)代碼
//int index = this.chooseRandomInt(serverCount); 獲取隨機(jī)下標(biāo)
// server = (Server)upList.get(index); 獲取server
if (server == null) {
Thread.yield();
} else {
if (server.isAlive()) {
return server;
}
server = null;
Thread.yield();
}
}
return server;
}
}
protected int chooseRandomInt(int serverCount) {
return ThreadLocalRandom.current().nextInt(serverCount);
}
public Server choose(Object key) {
return this.choose(this.getLoadBalancer(), key);
}
public void initWithNiwsConfig(IClientConfig clientConfig) {
}
}
- 將配置類中的負(fù)載均衡換成自定義
這樣就可以實(shí)現(xiàn)沒有服務(wù)輪詢?nèi)蔚呢?fù)載均衡策略。
到了這里,關(guān)于springCloud之Eureka之負(fù)載均衡Ribbon的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!