国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

詳解spring cloud中使用Ribbon實現(xiàn)客戶端的軟負(fù)載均衡

2024-07-13 10:16:42
字體:
供稿:網(wǎng)友

開篇

本例是在springboot整合H2內(nèi)存數(shù)據(jù)庫,實現(xiàn)單元測試與數(shù)據(jù)庫無關(guān)性和使用RestTemplate消費spring boot的Restful服務(wù)兩個示例的基礎(chǔ)上改造而來

在使用RestTemplate來消費spring boot的Restful服務(wù)示例中,我們提到,調(diào)用spring boot服務(wù)的時候,需要將服務(wù)的URL寫死或者是寫在配置文件中,但這兩種方式,無論哪一種,一旦ip地址發(fā)生了變化,都需要改動程序,并重新部署服務(wù),使用Ribbon的時候,可以有效的避免這個問題。

前言:

軟負(fù)載均衡的實現(xiàn)方式有兩種,分別是服務(wù)端的負(fù)載均衡和客戶端的負(fù)載均衡

服務(wù)端負(fù)載均衡:當(dāng)瀏覽器向后臺發(fā)出請求的時候,會首先向反向代理服務(wù)器發(fā)送請求,反向代理服務(wù)器會根據(jù)客戶端部署的ip:port映射表以及負(fù)載均衡策略,來決定向哪臺服務(wù)器發(fā)送請求,一般會使用到nginx反向代理技術(shù)。

客戶端負(fù)載均衡:當(dāng)瀏覽器向后臺發(fā)出請求的時候,客戶端會向服務(wù)注冊器(例如:Eureka Server),拉取注冊到服務(wù)器的可用服務(wù)信息,然后根據(jù)負(fù)載均衡策略,直接命中哪臺服務(wù)器發(fā)送請求。這整個過程都是在客戶端完成的,并不需要反向代理服務(wù)器的參與。

一、啟動Eureka Server

請參考該例:spring cloud中啟動Eureka Server

二、啟動微服務(wù),并注冊到Eureka Server上

spring cloud-將spring boot服務(wù)注冊到Eureka Server上

為了演示負(fù)載均衡的效果,再啟動一個為服務(wù),注意需要將端口號改成不一致

三、添加Ribbon支持

1、添加Ribbon的依賴

spring,cloud,Ribbon,軟負(fù)載均衡,springcloud,負(fù)載均衡

2、添加負(fù)載均衡支持

package com.chhliu.springboot.restful; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient public class SpringbootRestTemplateApplication {      @Autowired   private RestTemplateBuilder builder;    @Bean   @LoadBalanced // 添加負(fù)載均衡支持,很簡單,只需要在RestTemplate上添加@LoadBalanced注解,那么RestTemplate即具有負(fù)載均衡的功能,如果不加@LoadBalanced注解的話,會報java.net.UnknownHostException:springboot-h2異常,此時無法通過注冊到Eureka Server上的服務(wù)名來調(diào)用服務(wù),因為RestTemplate是無法從服務(wù)名映射到ip:port的,映射的功能是由LoadBalancerClient來實現(xiàn)的。   public RestTemplate restTemplate() {     return builder.build();   }    public static void main(String[] args) {     SpringApplication.run(SpringbootRestTemplateApplication.class, args);   } } 

3、修改調(diào)用微服務(wù)的URL

package com.chhliu.springboot.restful.controller; 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.RestController; import org.springframework.web.client.RestTemplate;  import com.chhliu.springboot.restful.vo.User;  @RestController public class RestTemplateController {   @Autowired   private RestTemplate restTemplate;      @GetMapping("/template/{id}")   public User findById(@PathVariable Long id) {// 將原來的ip:port的形式,改成注冊到Eureka Server上的應(yīng)用名即可     User u = this.restTemplate.getForObject("http://springboot-h2/user/" + id, User.class);     System.out.println(u);     return u;   } } 

四、查看Eureka Server狀態(tài)

spring,cloud,Ribbon,軟負(fù)載均衡,springcloud,負(fù)載均衡

五,在瀏覽器中,多次刷新http://localhost:7904/template/2地址

六、測試結(jié)果

7900端口服務(wù):

Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 

7901端口服務(wù):

Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 

7904端口服務(wù):

User [id=2, username=user2, name=李四, age=20, balance=100.00] 2017-01-23 09:58:05.682 INFO 7436 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty : Flipping property: springboot-h2.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647 User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] User [id=2, username=user2, name=李四, age=20, balance=100.00] 

從上面的測試結(jié)果可以看出,總共調(diào)了7904端口服務(wù)9次,其中7904端口服務(wù)調(diào)7900端口服務(wù)4次,調(diào)7901端口5次,剛好是9次

經(jīng)過上面的幾個步驟,就基本使用Ribbon實現(xiàn)了客戶端負(fù)載均衡的功能

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 绥棱县| 商水县| 屯留县| 潼南县| 军事| 吉木萨尔县| 三原县| 宽城| 弋阳县| 大连市| 荔波县| 四子王旗| 海原县| 兰溪市| 工布江达县| 北流市| 苍溪县| 兴仁县| 方正县| 航空| 麟游县| 启东市| 镶黄旗| 望江县| 海阳市| 孝义市| 宁都县| 建瓯市| 东安县| 佛教| 正定县| 祁门县| 灵寿县| 贵阳市| 富民县| 泽州县| 哈尔滨市| 左权县| 上饶市| 丰城市| 望奎县|