SpringCloud客戶端的負(fù)載均衡Ribbon的實(shí)現(xiàn)
前言:微服務(wù)架構(gòu),不可避免的存在單個(gè)微服務(wù)有多個(gè)實(shí)例,那么客戶端如何將請(qǐng)求分?jǐn)偟蕉鄠€(gè)微服務(wù)的實(shí)例上呢?這里我們就需要使用負(fù)載均衡了
一、Ribbon簡(jiǎn)介
Ribbon是Netflix發(fā)布的負(fù)載均衡器,它有助于控制HTTP和TCP客戶端的行為。為Ribbon配置服務(wù)提供者地址列表后,Ribbon就可基于某種負(fù)載均衡算法,自動(dòng)地幫助服務(wù)消費(fèi)者去請(qǐng)求。Ribbon默認(rèn)為我們提供了很多的負(fù)載均衡算法,例如:輪詢,隨機(jī)等,也可自定義;
Ribbon的GitHub: https://github.com/Netflix/ribbon
而在SpringCloud中使用Ribbon和Eureka時(shí),Ribbon會(huì)自動(dòng)從EurekaServer中獲取服務(wù)提供者地址列表,并基于負(fù)載均衡算法。
二、Ribbon實(shí)戰(zhàn)
1、創(chuàng)建EurekaServer,EurekaClient1,EurekaClient2,之前已經(jīng)說(shuō)過(guò)了Eureka的使用,這里直接上代碼:
EurekaServer:
ServerApplication.java
@SpringBootApplication @EnableEurekaServer public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class,args); } }
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cn</groupId> <artifactId>eureka-ribbon-server</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.</groupId> <artifactId></artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
server.port=8761 #注意:這兩個(gè)配置eureka默認(rèn)為true,要改成false,否則會(huì)報(bào)錯(cuò),connot connect server #表示是否將自己注冊(cè)在EurekaServer上 eureka.client.register-with-eureka=false #表示是否從EurekaServer獲取注冊(cè)信息 eureka.client.fetch-registry=false eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
EurekaClient1:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cn</groupId> <artifactId>eureka-ribbon-client</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
server.port=8762 spring.application.name=client-8762 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
而在啟動(dòng)類中加入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、啟動(dòng)順序:
①、依次啟動(dòng)EurekaServer =》 EurekaClient1 =》 EurekaClient2 ;
②、然后將EurekaClient2中的application.properties的server.port=8763改為server.port=8764,再次啟動(dòng)該項(xiàng)目;
③、打開(kāi)EurekaServer的配置頁(yè)面(http://localhost:8761/),如下:
④、我們?cè)诘刂窓谳斎雋ttp://localhost:8762/loadInstance,多刷新幾次,會(huì)發(fā)現(xiàn)每次調(diào)用的端口實(shí)例都不同,如下圖:
⑤、我們?cè)诳纯刂婆_(tái),如圖:
至此,Ribbon已經(jīng)入門,是不是很簡(jiǎn)單,但是這只是最簡(jiǎn)單的應(yīng)用,九牛一毛爾...學(xué)無(wú)止境乎!
相關(guān)文章
mybatis中數(shù)據(jù)加密與解密的實(shí)現(xiàn)
數(shù)據(jù)加解密的實(shí)現(xiàn)方式多種多樣,在mybatis環(huán)境中數(shù)據(jù)加解密變得非常簡(jiǎn)單易用,本文主要介紹了mybatis中數(shù)據(jù)加密與解密的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Intelli IDEA安裝Scala插件并安裝Scala軟件和配置環(huán)境變量的詳細(xì)教程
這篇文章主要介紹了Intelli IDEA安裝Scala插件并安裝Scala軟件和配置環(huán)境變量的詳細(xì)教程,需要的朋友可以參考下2020-10-10Java?EasyExcel利用填充模版動(dòng)態(tài)生成多個(gè)sheet頁(yè)
這篇文章主要為大家詳細(xì)介紹了Java?EasyExcel如何利用填充模版動(dòng)態(tài)生成多個(gè)sheet頁(yè),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12SpringMVC?RESTFul實(shí)現(xiàn)列表功能
這篇文章主要為大家介紹了SpringMVC?RESTFul實(shí)現(xiàn)列表功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Java多線程局域網(wǎng)聊天室的實(shí)現(xiàn)
在學(xué)習(xí)了一個(gè)學(xué)期的java以后,搞了一個(gè)多線程的聊天室,熟悉了一下服務(wù)器和客戶機(jī)的操作。感興趣的小伙伴們可以參考一下2021-06-06java_時(shí)間戳與Date_相互轉(zhuǎn)化的實(shí)現(xiàn)代碼
本篇文章是對(duì)java_時(shí)間戳與Date_相互轉(zhuǎn)化的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下如下。2016-11-11