ribbon, restTemplate 负载均衡服务调用
ribbon
- ribbon concept
- ribbon核心組件IRule
- 模仿源碼重寫輪詢
ribbon concept
spring cloud ribbon 是基于Netflix ribbon實(shí)現(xiàn)的一套客戶端負(fù)載均衡的工具。
簡單的說, Ribbon是Netflix發(fā)布的開源項目, 主要功能是提供客戶端的軟件負(fù)載均衡算法和服務(wù)
完善的配置項如連接超時,重試等。簡單的說,就是在配置文件中列出Load Balancer (簡稱LB), 助你基于某種規(guī)則(如簡單輪詢,隨機(jī)連接等)去連接這些機(jī)器。
目前進(jìn)入維護(hù)模式,替代品:spring cloud loadBalance.
restTemplate api
https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html
getObject, getEntity postObject, postEntity負(fù)載均衡
LB負(fù)載均衡(Load Balance)是什么
簡單的說就是將用戶的請求平攤的分配到多個服務(wù)上,從而達(dá)到系統(tǒng)的HA (高可用)。
常見的負(fù)載均衡有軟件Nginx, LVS,硬件F5等。
Ribbon本地負(fù)載均衡客戶端VS Nginx服務(wù)端負(fù)載均衡區(qū)別
Nginx是服務(wù)器負(fù)載均衡,客戶端所有請求都會交給nginx,然后由nginx實(shí)現(xiàn)轉(zhuǎn)發(fā)請求。即負(fù)載均衡是由服務(wù)端實(shí)現(xiàn)的。
Ribbon本地負(fù)載均衡,在調(diào)用微服務(wù)接口時候,會在注冊中心上獲取注冊信息服務(wù)列表之后緩存到JVM本地,從而在本地實(shí)現(xiàn)RPC遠(yuǎn)程服務(wù)調(diào)用技術(shù)。
負(fù)載均衡+RestTemplate調(diào)用。
ribbon核心組件IRule
ribbon客戶端負(fù)載均衡接口及實(shí)現(xiàn)類
接口以及子類
算法實(shí)現(xiàn)
替換默認(rèn)的輪詢算法, 使用隨機(jī)
package top.bitqian.rule; // 不要同mainBoot一個目錄import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** 自定義ribbon 負(fù)載均衡規(guī)則代替默認(rèn)輪詢規(guī)則* @author echo lovely* @date 2020/12/6 17:34*/@Configuration public class SelfRule {@Beanpublic IRule getIRule() {// 隨機(jī)return new RandomRule();}}然后主啟動類添加@RibbonClient注解。
模仿源碼重寫輪詢
負(fù)載均衡算法原理: rest接口的第幾次請求數(shù)%服務(wù)器集群總數(shù)量 = 實(shí)際服務(wù)調(diào)用服務(wù)器的下標(biāo)。 每次重啟服務(wù)后,rest接口從1開始。接口
package top.bitqian.springcloud.lb;import org.springframework.cloud.client.ServiceInstance;import java.util.List;/*** 負(fù)載均衡 接口--> 輪詢~*/ public interface LoadBalance {/*** 根據(jù)可用的服務(wù)實(shí)例列表 輪詢獲取某個實(shí)例~* @param serviceInstanceList 可用服務(wù)實(shí)例列表* @return 輪詢后的某個服務(wù)實(shí)例~*/ServiceInstance getInstance(List<ServiceInstance> serviceInstanceList);}實(shí)現(xiàn)
package top.bitqian.springcloud.lb.impl;import org.springframework.cloud.client.ServiceInstance; import org.springframework.stereotype.Component; import top.bitqian.springcloud.lb.LoadBalance;import java.util.List; import java.util.concurrent.atomic.AtomicInteger;/*** 仿照源碼寫輪詢算法* @author echo lovely* @date 2020/12/8 20:37*/@Component public class MyLoadBalance implements LoadBalance {// init 0private final AtomicInteger atomicInteger = new AtomicInteger(0);public final int getAndIncrease() {int current;int next;do {current = this.atomicInteger.get();next = current >= 2147483647 ? 0 : current + 1;} while (!this.atomicInteger.compareAndSet(current, next)); // 期望值,修改值System.out.println("the next value -----> " + next);return next;}@Overridepublic ServiceInstance getInstance(List<ServiceInstance> serviceInstanceList) { // 機(jī)器列表// 得到服務(wù)器的下標(biāo)位置int index = getAndIncrease() % serviceInstanceList.size();return serviceInstanceList.get(index);} }controller // 測試手寫的輪詢算法~@GetMapping("/consumer/payment/lb")public String getPaymentByLb() {// 根據(jù)服務(wù)名獲取服務(wù)列表List<ServiceInstance> serviceInstanceList =discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");// 1 2, 1 2, 1 2, 獲取服務(wù)~ServiceInstance instance = myLb.getInstance(serviceInstanceList);URI uri = instance.getUri();return restTemplate.getForObject(uri + "/payment/lb", String.class);}
總結(jié)
以上是生活随笔為你收集整理的ribbon, restTemplate 负载均衡服务调用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python,pygame下载与安装详细
- 下一篇: 桌面壁纸所放位置+魔镜壁纸的下载方法