本篇文章為大家展示了怎么在SpringCloud客戶端中實(shí)現(xiàn)Ribbon負(fù)載均衡,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)公司基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺為眾多戶提供內(nèi)蒙古服務(wù)器托管 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。
一、Ribbon簡介
Ribbon是Netflix發(fā)布的負(fù)載均衡器,它有助于控制HTTP和TCP客戶端的行為。為Ribbon配置服務(wù)提供者地址列表后,Ribbon就可基于某種負(fù)載均衡算法,自動地幫助服務(wù)消費(fèi)者去請求。Ribbon默認(rèn)為我們提供了很多的負(fù)載均衡算法,例如:輪詢,隨機(jī)等,也可自定義;
Ribbon的GitHub: https://github.com/Netflix/ribbon
而在SpringCloud中使用Ribbon和Eureka時,Ribbon會自動從EurekaServer中獲取服務(wù)提供者地址列表,并基于負(fù)載均衡算法。
二、Ribbon實(shí)戰(zhàn)
1、創(chuàng)建EurekaServer,EurekaClient1,EurekaClient2,之前已經(jīng)說過了Eureka的使用,這里直接上代碼:
EurekaServer:
ServerApplication.java
@SpringBootApplication @EnableEurekaServer public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class,args); } }
pom.xml
4.0.0 com.cn eureka-ribbon-server 1.0-SNAPSHOT UTF-8 1.8 org.springframework.boot spring-boot-starter-parent 1.5.13.RELEASE org.springframework. org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.cloud spring-cloud-dependencies Edgware.SR3 pom import org.springframework.boot spring-boot-maven-plugin
application.properties
server.port=8761 #注意:這兩個配置eureka默認(rèn)為true,要改成false,否則會報錯,connot connect server #表示是否將自己注冊在EurekaServer上 eureka.client.register-with-eureka=false #表示是否從EurekaServer獲取注冊信息 eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
EurekaClient1:
pom.xml
4.0.0 com.cn eureka-ribbon-client 1.0-SNAPSHOT UTF-8 1.8 org.springframework.boot spring-boot-starter-parent 1.5.13.RELEASE org.springframework.cloud spring-cloud-starter-ribbon org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-dependencies Edgware.SR3 pom import org.springframework.boot spring-boot-maven-plugin
server.port=8762 spring.application.name=client-8762 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
而在啟動類中加入RestTemplate遠(yuǎn)程調(diào)用實(shí)例到容器中,并且 添加LoadBalanced注解,使RestTemplate具備負(fù)載均衡的能力 :
@SpringBootApplication @EnableDiscoveryClient public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } /** * @Description: 加入@LoadBalanced注解,就可以為RestTemplate加入負(fù)載均衡的能力 * @Param: * @return: * @Author: * @Date: 2018/6/15 */ @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
創(chuàng)建Controller,注入RestTemplate、LoadBalancerClient實(shí)例:
package com.cn.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; /** * @program: springcloud-example * @description: * @author: * @create: 2018-06-15 15:55 **/ @Controller public class RibbonController { @Autowired private LoadBalancerClient loadBalancerClient; @Autowired private RestTemplate restTemplate; @GetMapping("/loadInstance") @ResponseBody public String loadInstance() { ServiceInstance choose = this.loadBalancerClient.choose("client-87"); System.out.println(choose.getServiceId()+":"+choose.getHost()+":"+choose.getPort()); return choose.getServiceId() + ":" + choose.getHost() + ":" + choose.getPort(); } }
package com.cn; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * @program: springcloud-example * @description: * @author: 535504 * @create: 2018-06-15 16:05 **/ @SpringBootApplication @EnableDiscoveryClient public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } }
EurekaClient2:
pom.xml與EurekaClient1中一致
application.xml:
server.port=8763 spring.application.name=client-87 eureka.client.service-url.defaultZone=http://localhost:8761/eureka
ClientController.java:
package com.cn.contorller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @program: springcloud-example * @description: * @author: * @create: 2018-06-15 16:12 **/ @Controller public class ClientController { @GetMapping("/getUser") @ResponseBody public String getUser() { System.out.println("獲取用戶成功"); return "獲取用戶成功"; } }
2、啟動順序:
①、依次啟動EurekaServer =》 EurekaClient1 =》 EurekaClient2 ;
②、然后將EurekaClient2中的application.properties的server.port=8763改為server.port=8764,再次啟動該項目;
③、打開EurekaServer的配置頁面(http://localhost:8761/),如下:
④、我們在地址欄輸入http://localhost:8762/loadInstance,多刷新幾次,會發(fā)現(xiàn)每次調(diào)用的端口實(shí)例都不同,如下圖:
⑤、我們在看控制臺,如圖:
上述內(nèi)容就是怎么在SpringCloud客戶端中實(shí)現(xiàn)Ribbon負(fù)載均衡,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。