SpringCloud Netflix Ribbon超詳細(xì)講解
一、Ribbon簡(jiǎn)介
1、什么是Ribbon
Spring Cloud Ribbon 是基于Netflix Ribbon 實(shí)現(xiàn)的一套客戶端負(fù)載均衡的工具,它可以很好地控制HTTP和TCP客戶端的行為。
簡(jiǎn)單的說,Ribbon 是 Netflix 發(fā)布的開源項(xiàng)目,主要功能是提供客戶端的軟件負(fù)載均衡算法,將 Netflix 的中間層服務(wù)連接在一起。Ribbon 的客戶端組件提供一系列完整的配置項(xiàng),如:連接超時(shí)、重試等。簡(jiǎn)單的說,就是在配置文件中列出 LoadBalancer (簡(jiǎn)稱LB:負(fù)載均衡) 后面所有的及其,Ribbon 會(huì)自動(dòng)的幫助你基于某種規(guī)則 (如簡(jiǎn)單輪詢,隨機(jī)連接等等) 去連接這些機(jī)器。我們也容易使用 Ribbon 實(shí)現(xiàn)自定義的負(fù)載均衡算法!
2、Ribbon能干什么

- LB,即負(fù)載均衡 (LoadBalancer) ,在微服務(wù)或分布式集群中經(jīng)常用的一種應(yīng)用。
- 負(fù)載均衡簡(jiǎn)單的說就是將用戶的請(qǐng)求平攤的分配到多個(gè)服務(wù)上,從而達(dá)到系統(tǒng)的HA (高用)。
- 常見的負(fù)載均衡軟件有 Nginx、Lvs(中國(guó)人研發(fā)的) 等等。
其中l(wèi)vs是中國(guó)技術(shù)專家章文嵩發(fā)明的
- Dubbo、SpringCloud 中均給我們提供了負(fù)載均衡,SpringCloud 的負(fù)載均衡算法可以自定義。
負(fù)載均衡簡(jiǎn)單分類:
- 集中式LB
即在服務(wù)的提供方和消費(fèi)方之間使用獨(dú)立的LB設(shè)施,如Nginx(反向代理服務(wù)器),由該設(shè)施負(fù)責(zé)把訪問請(qǐng)求通過某種策略轉(zhuǎn)發(fā)至服務(wù)的提供方!
- 進(jìn)程式 LB
將LB邏輯集成到消費(fèi)方,消費(fèi)方從服務(wù)注冊(cè)中心獲知有哪些地址可用,然后自己再?gòu)倪@些地址中選出一個(gè)合適的服務(wù)器。 Ribbon就屬于進(jìn)程內(nèi)LB,它只是一個(gè)類庫(kù),集成于消費(fèi)方進(jìn)程,消費(fèi)方通過它來獲取到服務(wù)提供方的地址!
二、使用Ribbon
1、客戶端導(dǎo)入依賴
<!--引入Eureka的依賴-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--引入ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
2、application.yml配置
server:
port: 801eureka:
client:
register-with-eureka: false #不向eureka注冊(cè)自己
service-url:
defaultZone: http://localhost:7001/eureka/ #去哪個(gè)地方獲取
3、Controller配置
和前面兩節(jié)不一樣的是,用Ribbon做負(fù)載均衡,地址不能寫死,也就是不能和前面的一樣寫成一個(gè)具體的值如:localhost:8001,而是這個(gè)微服務(wù)的名字,也就是這個(gè)名字,如下。

package com.you.controller;
import com.you.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@ResponseBody
public class DeptComsumerController {
@Autowired
RestTemplate restTemplate;
// public static final String REST_URL_PREFIX = "http://localhost:8001";
public static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";
@GetMapping("/consumer/dept/getDept/{id}")
public Dept getDeptOfId(@PathVariable("id") Long id)
{
System.out.println(REST_URL_PREFIX+"/dept"+"/aDept/"+id);
return restTemplate.getForObject(REST_URL_PREFIX + "/dept" + "/aDept/"+id, Dept.class);
}
}4、Config的配置
在此文件里,增加了@LoadBalanced注解,該注解的作用是讓RestTemplate有了負(fù)載均衡的能力,而且默認(rèn)的負(fù)載均衡算法是輪詢(也就是一個(gè)一個(gè)的嘗試),可以使用系統(tǒng)配備的負(fù)載均衡算法,也可以自己寫自己的負(fù)載均衡算法。
package com.you.config;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConfigBean {
@Bean
@LoadBalanced //ribbon
/*配置負(fù)載均衡實(shí)現(xiàn)RestTemplate*/
/*IRule*/
/*RoundRobinRule 輪詢 */
/*RandomRule 隨機(jī)*/
/*AvailabilityFilteringRule 優(yōu)先過濾掉跳閘、訪問故障的服務(wù),對(duì)剩下的進(jìn)行輪詢 */
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}5、啟動(dòng)類的配置
package com.you;
import com.tan.tanRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
@SpringBootApplication
@EnableEurekaClient
/*下面是處理負(fù)載均衡算法*/
@RibbonClient(name = "SPRINGCLOUD-PROVIDER-DEPT",configuration = tanRule.class)
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class,args);
}
}三、Ribbon實(shí)現(xiàn)負(fù)載均衡

為了實(shí)現(xiàn)負(fù)載均衡,擴(kuò)充一下服務(wù)提供者,將原來的一個(gè)服務(wù)提供者,改為三個(gè)。
- 新建三個(gè)module,springboot-provider-8002、springboot-provider-8003。
- 參考springboot-provider-8001,修改application.xml(主要是端口號(hào),數(shù)據(jù)庫(kù)名,instance-id),其中application-name要保持一致。和微服務(wù)的名字一樣。

- 啟動(dòng)Eureka_7001,啟動(dòng)這個(gè)三個(gè)提供者,根據(jù)自己的情況,如果電腦性能比較差,可以少啟動(dòng)一個(gè)。啟動(dòng)consumer_80。
訪問consumer_80配置的Getmapping地址,然后不斷的刷新,會(huì)看到依次訪問三個(gè)數(shù)據(jù)庫(kù),并且一直重復(fù),這就是默認(rèn)的負(fù)載均衡算法:輪詢
四、設(shè)計(jì)負(fù)載均衡算法
1、80啟動(dòng)類的改動(dòng)
@RibbonClient()注釋的應(yīng)用,在psvm上面添加該注釋,其具體內(nèi)容為@RibbonClient(name = “SPRINGCLOUD-PROVIDER-DEPT”,configuration = tanRule.class),其中name的值即為微服務(wù)的名字,configuration的值對(duì)應(yīng)的就是自己寫的路由類

2、自定義路由類
需要注意,自定義的路由類,不可以用啟動(dòng)類放在同一目錄,一般要比啟動(dòng)類高一級(jí)目錄,放在同一目錄下,需要配置CompentScan。

package com.tan;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
public class tanRule {
@Bean
public IRule myRule()
{
return new tanRandomRule();
}
}3、負(fù)載均衡算法的實(shí)現(xiàn)
可以模仿系統(tǒng)中的負(fù)載均衡算法,撰寫自己的負(fù)載均衡算法,如下面的例子即為:每個(gè)端口的提供者訪問5次,然后切換下一個(gè)端口,全部訪問完成后則重新開始,代碼如下:
package com.tan;
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 tanRandomRule extends AbstractLoadBalancerRule {
int total = 0;
int currentIndex = 0;
public tanRandomRule() {
}
@SuppressWarnings({"RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"})
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();
if(total<5)
{
server = (Server)upList.get(currentIndex);
total++;
}else{
total = 0;
currentIndex++;
if(currentIndex>2)
{
currentIndex = 0;
}
server = (Server)upList.get(currentIndex);
}
System.out.println("CurrentIndex:"+currentIndex);
System.out.println("Total:"+total);
System.out.println("sever 的值是:"+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) {
}
}到此這篇關(guān)于SpringCloud Netflix Ribbon超詳細(xì)講解的文章就介紹到這了,更多相關(guān)SpringCloud Netflix Ribbon內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中獲取時(shí)間戳的三種方式對(duì)比實(shí)現(xiàn)
這篇文章主要介紹了Java中獲取時(shí)間戳的三種方式對(duì)比實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
詳解Java如何優(yōu)雅的調(diào)用dubbo同時(shí)不使用其它jar包
這篇文章主要介紹了如何在不使用他人jar包的情況下優(yōu)雅的進(jìn)行dubbo調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
通過FeignClient如何獲取文件流steam?is?close問題
這篇文章主要介紹了通過FeignClient如何獲取文件流steam?is?close問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Java通過Timer與TimerTask實(shí)現(xiàn)定時(shí)任務(wù)調(diào)度方式
本文介紹了如何在Java中使用`Timer`和`TimerTask`類來實(shí)現(xiàn)定時(shí)任務(wù)調(diào)度,`Timer`類用于創(chuàng)建計(jì)時(shí)器并安排任務(wù),而`TimerTask`類用于定義具體的任務(wù),文章詳細(xì)介紹了這兩個(gè)類的方法和使用示例,包括創(chuàng)建任務(wù)、安排任務(wù)、取消任務(wù)等操作,通過一個(gè)簡(jiǎn)單的例子2024-12-12
Java 常見的限流算法詳細(xì)分析并實(shí)現(xiàn)
大數(shù)據(jù)量高并發(fā)訪問時(shí),經(jīng)常出現(xiàn)服務(wù)或接口面對(duì)暴漲的請(qǐng)求而不可用的情況,甚至引發(fā)連鎖反映導(dǎo)致整個(gè)系統(tǒng)崩潰。此時(shí)你需要使用的技術(shù)手段之一就是限流,當(dāng)請(qǐng)求達(dá)到一定的并發(fā)數(shù)或速率,就進(jìn)行等待、排隊(duì)、降級(jí)、拒絕服務(wù)等。限流時(shí),常見算法是計(jì)數(shù)器、漏斗、令牌桶算法2022-04-04
SpringBoot @PropertySource與@ImportResource有什么區(qū)別
這篇文章主要介紹了SpringBoot @PropertySource與@ImportResource有什么區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01

