SpringCloud?客戶(hù)端Ribbon負(fù)載均衡的實(shí)現(xiàn)方法
Ribbon 介紹
Ribbon 是 Netflix 提供的一個(gè)基于 Http 和 TCP 的客戶(hù)端負(fù)載均衡工具,且已集成在 Eureka 依賴(lài)中。

實(shí)現(xiàn)原理:SpringCloud Ribbon 的底層采用了一個(gè)攔截器,攔截了 RestTemplate 發(fā)出的請(qǐng)求,對(duì)地址做了修改。

開(kāi)啟客戶(hù)端負(fù)載均衡,簡(jiǎn)化 RestTemplate 調(diào)用
1)在服務(wù)調(diào)用者的 RestTemplate 配置類(lèi)上添加注解:
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced // 開(kāi)啟客戶(hù)端負(fù)載均衡(默認(rèn)輪詢(xún)策略)
public RestTemplate restTemplate(){
return new RestTemplate();
}
}2)在調(diào)用時(shí)指定服務(wù)名:
package com.controller;
import com.domain.Goods;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* 服務(wù)調(diào)用方
*/
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/goods/{id}")
public Goods findOrderByGoodsId(@PathVariable("id") int id) {
String url = String.format("http://eureka-provider/goods/findOne/%d", id);
Goods goods = restTemplate.getForObject(url, Goods.class);
return goods;
}
}負(fù)載均衡策略
負(fù)載均衡策略:
- 輪詢(xún)(默認(rèn))
- 隨機(jī)
- 最小并發(fā)
- 過(guò)濾
- 響應(yīng)時(shí)間
- 輪詢(xún)重試
- 性能可用性
使用負(fù)載均衡:
方式一:使用 bean 的方式
- 在消費(fèi)者端配置負(fù)載均衡策略 Bean:
package com.config;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
public class MyRule {
@Bean
public IRule rule() {
return new RandomRule(); // 隨機(jī)策略
}
}- 在啟動(dòng)類(lèi)添加注解:
package com;
import com.config.MyRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
@EnableDiscoveryClient // 激活DiscoveryClient
@EnableEurekaClient
@SpringBootApplication
@RibbonClient(name="eureka-provider", configuration= MyRule.class) // 指定服務(wù)提供方并配置負(fù)載均衡策略
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}方式二:使用配置文件
server:
port: 9000
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: eureka-consumer
# 設(shè)置 Ribbon 的負(fù)載均衡策略:隨機(jī)策略
EUREKA-PROVIDER:
ribbon:
NFloadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule.RandomRule饑餓加載
Ribbon 默認(rèn)是采用懶加載,即第一次訪(fǎng)問(wèn)時(shí)才會(huì)去創(chuàng)建 LoadBalanceClient,請(qǐng)求時(shí)間會(huì)很長(zhǎng)。而饑餓加載則會(huì)在項(xiàng)目啟動(dòng)時(shí)創(chuàng)建,達(dá)到降低第一次訪(fǎng)問(wèn)的耗時(shí)。
可以通過(guò)下面配置開(kāi)啟饑餓加載:
ribbon:
eager-load:
enabled: true
clients: userservice到此這篇關(guān)于SpringCloud 客戶(hù)端Ribbon負(fù)載均衡的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)SpringCloud Ribbon負(fù)載均衡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
本地編譯打包項(xiàng)目部署到服務(wù)器并且啟動(dòng)方式
這篇文章主要介紹了本地編譯打包項(xiàng)目部署到服務(wù)器并且啟動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Java通過(guò)XPath獲取XML文件中符合特定條件的節(jié)點(diǎn)
今天小編就為大家分享一篇關(guān)于Java通過(guò)XPath獲取XML文件中符合特定條件的節(jié)點(diǎn),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
Java實(shí)現(xiàn)帶有權(quán)重隨機(jī)算法的示例詳解
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)帶有權(quán)重隨機(jī)算法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
Redis內(nèi)存數(shù)據(jù)庫(kù)示例分析
Redis本身的內(nèi)容比較復(fù)雜。如果你上來(lái),你應(yīng)該研究一個(gè)細(xì)節(jié)點(diǎn),比如連接池和數(shù)據(jù)結(jié)構(gòu)。雖然可以直接了解某一點(diǎn)的詳細(xì)來(lái)源內(nèi)容,甚至盡快解決一些意外,但是容易淹沒(méi)在失眠的細(xì)節(jié)中,整體控制不了Redis2022-12-12
Java Collections集合繼承結(jié)構(gòu)圖_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java Collections集合繼承結(jié)構(gòu)圖_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下2017-04-04

