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

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

開啟客戶端負(fù)載均衡,簡化 RestTemplate 調(diào)用
1)在服務(wù)調(diào)用者的 RestTemplate 配置類上添加注解:
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced // 開啟客戶端負(fù)載均衡(默認(rèn)輪詢策略)
public RestTemplate restTemplate(){
return new RestTemplate();
}
}2)在調(diào)用時指定服務(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ù)載均衡策略:
- 輪詢(默認(rèn))
- 隨機(jī)
- 最小并發(fā)
- 過濾
- 響應(yīng)時間
- 輪詢重試
- 性能可用性
使用負(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ī)策略
}
}- 在啟動類添加注解:
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)是采用懶加載,即第一次訪問時才會去創(chuàng)建 LoadBalanceClient,請求時間會很長。而饑餓加載則會在項(xiàng)目啟動時創(chuàng)建,達(dá)到降低第一次訪問的耗時。
可以通過下面配置開啟饑餓加載:
ribbon:
eager-load:
enabled: true
clients: userservice到此這篇關(guān)于SpringCloud 客戶端Ribbon負(fù)載均衡的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)SpringCloud Ribbon負(fù)載均衡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中Timer的schedule()方法參數(shù)詳解
今天小編就為大家分享一篇關(guān)于Java中Timer的schedule()方法參數(shù)詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
SpringBoot實(shí)現(xiàn)elasticsearch索引操作的代碼示例
這篇文章主要給大家介紹了SpringBoot如何實(shí)現(xiàn)elasticsearch 索引操作,文中有詳細(xì)的代碼示例,感興趣的同學(xué)可以參考閱讀下2023-07-07
使用spring aop 統(tǒng)一捕獲異常和寫日志的示例demo
本文通過一個小demo給大家介紹spring AOP 實(shí)現(xiàn)的異常捕獲和日志的方法技巧,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-08-08
淺談Spring Boot: 接口壓測及簡要優(yōu)化策略
這篇文章主要介紹了淺談Spring Boot: 接口壓測及簡要優(yōu)化策略,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
SpringBoot隨機(jī)數(shù)設(shè)置及參數(shù)間引用的操作步驟
在Spring Boot配置文件中設(shè)置屬性時,除了可以像前面示例中顯示的配置屬性值外,還可以使用隨機(jī)值和參數(shù)間引用對屬性值進(jìn)行設(shè)置。下面給大家介紹SpringBoot參數(shù)間引用隨機(jī)數(shù)設(shè)置的操作步驟,感興趣的朋友一起看看吧2021-06-06

