spring cloud 集成 ribbon負載均衡的實例代碼
本文比較簡單集成ribbon,如需要更詳細,請查看我的更多博客內(nèi)容。
首先創(chuàng)建兩個服務提供者
服務一,集成的nacos注冊中心,這塊隨便寫一個同名接口
端口配置8301
服務二,同名接口內(nèi)容修改,其他跟上一個服務一大體內(nèi)容一致
端口配置成8302
創(chuàng)建服務消費者
RibbonConfig.java
package com.example.nacosribbonconsumers.config; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RoundRobinRule; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration // 如果多個服務可以選擇不同的策略 /*@RibbonClients({ @RibbonClient(name = "other",configuration = OtherConfig.class), @RibbonClient(name = "provider",configuration = ProviderConfig.class) })*/ @RibbonClient(name = "nacos-ribbon-provider") public class RibbonConfig { //定義負載均衡規(guī)則 @Bean public IRule ribbonRule(){ return new RoundRobinRule(); /** * RoundRobinRule: * 輪詢規(guī)則 * * RandomRule: * 隨機規(guī)則 * * WeightedResponseTimeRule: * 使用響應時間的平均或者百分比為每個服務分配權(quán)重的規(guī)則,如果沒法收集響應時間信息,會默認使用輪詢規(guī)則 * * BestAvailableRule: * 會先根據(jù)斷路器過濾掉處于故障的服務,然后選擇并發(fā)量最小的服務 * * ZoneAvoidanceRule: * 根據(jù)server所在Zone和其性能,選擇服務器,默認規(guī)則 * * AvailabilityFilteringRule: * 先根據(jù)斷路器規(guī)則過濾掉有問題的服務,然后對剩余的服務按照輪詢的策略進行訪問 * * RetryRule: * 先按照RoundRobinRule規(guī)則進行服務獲取,如果調(diào)用服務失敗會在指定時間內(nèi)進行重試,直到獲取到可用的服務。 */ } @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
RibbonTest.java
package com.example.nacosribbonconsumers.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class RibbonTest { @Autowired private RestTemplate restTemplate; @GetMapping(value = "/ribbon-consumers/ribbon-test") public String printProviderLog(){ String result = restTemplate.getForObject("http://nacos-ribbon-provider/ribbon-test", String.class); return result; } }
pom包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
配置文件
先啟動兩個服務提供者,然后在啟動服務消費者,瀏覽訪問
不斷刷新 發(fā)現(xiàn)使用的輪詢方式交替執(zhí)行。
到此這篇關(guān)于spring cloud 集成 ribbon負載均衡的文章就介紹到這了,更多相關(guān)spring cloud ribbon負載均衡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于springboot redirect重定向路徑問題總結(jié)
這篇文章主要介紹了springboot redirect重定向路徑問題總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Java 實戰(zhàn)范例之精美網(wǎng)上音樂平臺的實現(xiàn)
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+vue+Springboot+ssm+mysql+maven+redis實現(xiàn)一個前后端分離的精美網(wǎng)上音樂平臺,大家可以在過程中查缺補漏,提升水平2021-11-11java的各種集合為什么不安全(List、Set、Map)以及代替方案
這篇文章主要介紹了java的各種集合為什么不安全(List、Set、Map)以及代替方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10Java中HashMap與String字符串互轉(zhuǎn)的問題解決
本文介紹了Java中HashMap與String字符串互轉(zhuǎn)的問題解決,當我們有需求將HashMap轉(zhuǎn)為Json格式的String時,需要使用FastJson/Gson將HashMap轉(zhuǎn)為String,感興趣的可以了解一下2022-03-03