SpringCloud手寫Ribbon實(shí)現(xiàn)負(fù)載均衡
前言
前面我們學(xué)習(xí)了 SpringCloud整合Consul ,在此基礎(chǔ)上我們手寫本地客戶端實(shí)現(xiàn)類似Ribbon負(fù)載均衡的效果。
注: order
模塊調(diào)用者 記得關(guān)閉 @LoadBalanced
注解。
我們這里只演示 注冊中心 consul,至于 zookeeper 也是一模一樣。
生產(chǎn)者
member模塊
member
服務(wù)需要集群,所以我們copy application-consul.yml
文件命名為 application-consul2.yml
服務(wù)別名一致,只需要修改端口號即可。
application-consul2.yml
配置文件:
##服務(wù)端口號 server: port: 8503 spring: application: ##服務(wù)別名--服務(wù)注冊到consul名稱 name: consul-member ##注冊中心consul地址 cloud: consul: host: localhost port: 8500 discovery: ## consul ip地址 hostname: 192.168.3.91
啟動(dòng) member
集群服務(wù):
idea 運(yùn)行 AppMember.java
啟動(dòng) 8501
端口
再打開 jar 包路徑
shift + 右鍵
啟動(dòng) PowerShell 窗口,運(yùn)行命令:
java -jar E:\ideaworkspaceback\springcloud-zookeeper\springcloud-zookeeper-member\target\springcloud-zookeeper-member-1.0-SNAPSHOT.jar --spring.profiles.active=consul2
啟動(dòng) 8503
端口
打開 http://localhost:8500/ui/dc1/services 可以發(fā)現(xiàn)上面 注冊了 member
集群服務(wù):
消費(fèi)者
order模塊
OrderApiController.java
控制頁面使用原子類來 AtomicInteger
保證操作的原子性。
package com.baba.wlb.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; /** * @Author wulongbo * @Date 2021/1/9 15:32 * @Version 1.0 */@RestController public class OrderApiController { @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; //使用原子類AtomicInteger private AtomicInteger atomicInteger = new AtomicInteger(1); public int add() { return atomicInteger.getAndIncrement(); //先獲取再自增,對應(yīng)a++ //若使用++a 則對應(yīng)方法是a.incrementAndGet(); 先自增再獲取 , //多說一句 a-- 就是 a.getAndDecrement(); //若a = a + 10;————對應(yīng)API a.getAndAdd(10); } /** * springCloud中,兩種方式調(diào)用(rest/feign) * * @return */ // 訂單服務(wù)調(diào)用會員服務(wù) @RequestMapping("/getOrder") public String getOrder() { // 有兩種調(diào)用方式,一種是采用服務(wù)別名方式調(diào)用,另一種是使用別名去注冊中心上獲取對應(yīng)服務(wù)調(diào)用地址 // 第一種方式 String url = "http://dy-202006281547:8000/getMember"; // 第二種方式 url = "http://zk-member/getMember"; String result = restTemplate.getForObject(url, String.class); return "訂單服務(wù)調(diào)用會員服務(wù):" + result; } /** * springCloud中,兩種方式調(diào)用(rest/feign) * * @return */ // 訂單服務(wù)調(diào)用會員服務(wù) @RequestMapping("/getRibbonOrder") public String getRibbonOrder() { // 第一種方式 String url = getUri() + "/getMember"; String result = restTemplate.getForObject(url, String.class); return "純手寫Ribbon本地負(fù)載均衡:" + result; } public String getUri() { List<ServiceInstance> serviceInstances = discoveryClient.getInstances("consul-member"); if (serviceInstances == null || serviceInstances.isEmpty()) { return null; } int serverSize = serviceInstances.size(); int count = add(); int indexServer = count % serverSize; return serviceInstances.get(indexServer).getUri().toString(); } }
AppOrder.java
啟動(dòng)類,關(guān)閉 @LoadBalanced
注解即可:
由于我們是在 springcloud集成zookeeper
項(xiàng)目上改造的,所以 order 模塊
yml 配置也做相應(yīng)的調(diào)整,注冊中心修改為consul,并且在該模塊中引入 consul 的 maven依賴
<!--springcloud 整合consul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
啟動(dòng) AppOrder.java
,并刷新 http://localhost:8500/ui/dc1/services 可以發(fā)現(xiàn)上面 注冊了 order
服務(wù):
這里有一個(gè)巨坑,導(dǎo)致 order
模塊調(diào)用不到 member
模塊,我們需要在yml 配置文件中注釋掉:
## consul ip地址 ##hostname: 192.168.3.91
消費(fèi)者調(diào)用生產(chǎn)者:訪問 http://localhost:8508/getRibbonOrder
OK!我們便達(dá)到了負(fù)載均衡!
到此這篇關(guān)于SpringCloud手寫Ribbon實(shí)現(xiàn)負(fù)載均衡的文章就介紹到這了,更多相關(guān)SpringCloud Ribbon負(fù)載均衡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于java的包Package中同名類的沖突及其理解
這篇文章主要介紹了關(guān)于java的包Package中同名類的沖突及其理解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Java二進(jìn)制操作(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
這篇文章給大家介紹了java二進(jìn)制操作技巧,包括移位、位運(yùn)算操作符等相關(guān)知識點(diǎn),非常不錯(cuò),感興趣的朋友參考下吧2017-03-03SpringMVC中的@RequestMapping注解的使用詳細(xì)教程
@RequestMapping注解的作用就是將請求和處理請求的控制器方法關(guān)聯(lián)起來,建立映射關(guān)系,本文主要來和大家詳細(xì)講講它的具體使用,感興趣的可以了解一下2023-07-07使用springboot時(shí),解決@Scheduled定時(shí)器遇到的問題
這篇文章主要介紹了使用springboot時(shí),解決@Scheduled定時(shí)器遇到的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11kafka消費(fèi)不到數(shù)據(jù)的排查過程
這篇文章主要介紹了kafka消費(fèi)不到數(shù)據(jù)的排查過程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02java語言描述Redis分布式鎖的正確實(shí)現(xiàn)方式
這篇文章主要介紹了java語言描述Redis分布式鎖的正確實(shí)現(xiàn)方式,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12