前言:微服務架構,不可避免的存在單個微服務有多個實例,那麼客戶端如何將請求分攤到多個微服務的實例上呢?這裏咱們就須要使用負載均衡了html
Ribbon是Netflix發佈的負載均衡器,它有助於控制HTTP和TCP客戶端的行爲。爲Ribbon配置服務提供者地址列表後,Ribbon就可基於某種負載均衡算法,自動地幫助服務消費者去請求。Ribbon默認爲咱們提供了不少的負載均衡算法,例如:輪詢,隨機等,也可自定義;java
Ribbon的GitHub:https://github.com/Netflix/ribbongit
而在SpringCloud中使用Ribbon和Eureka時,Ribbon會自動從EurekaServer中獲取服務提供者地址列表,並基於負載均衡算法。github
一、建立EurekaServer,EurekaClient1,EurekaClient2,以前已經說過了Eureka的使用,這裏直接上代碼:web
EurekaServer:算法
@SpringBootApplication @EnableEurekaServer public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class,args); } }
<?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>
server.port=8761 #注意:這兩個配置eureka默認爲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:spring
<?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/
而在啓動類中加入RestTemplate遠程調用實例到容器中,而且添加LoadBalanced註解,使RestTemplate具有負載均衡的能力:apache
@SpringBootApplication @EnableDiscoveryClient public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } /** * @Description: 加入@LoadBalanced註解,就能夠爲RestTemplate加入負載均衡的能力 * @Param: * @return: * @Author: * @Date: 2018/6/15 */ @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
建立Controller,注入RestTemplate、LoadBalancerClient實例:架構
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(); } }
EurekaClient2:app
pom.xml與EurekaClient1中一致
application.xml:
server.port=8763 spring.application.name=client-87 eureka.client.service-url.defaultZone=http://localhost:8761/eureka
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); } }
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 "獲取用戶成功"; } }
二、啓動順序:
①、依次啓動EurekaServer =》 EurekaClient1 =》 EurekaClient2 ;
②、而後將EurekaClient2中的application.properties的server.port=8763改成server.port=8764,再次啓動該項目;
③、打開EurekaServer的配置頁面(http://localhost:8761/),以下:
④、咱們在地址欄輸入http://localhost:8762/loadInstance,多刷新幾回,會發現每次調用的端口實例都不一樣,以下圖:
⑤、咱們在看控制檯,如圖:
至此,Ribbon已經入門,是否是很簡單,可是這只是最簡單的應用,九牛一毛爾...學無止境乎!