Ribbon 是一個基於 HTTP 和 TCP 客戶端的負載均衡器java
它能夠在客戶端配置 ribbonServerList(服務端列表),而後輪詢請求以實現均衡負載git
它在聯合 Eureka 使用時github
ribbonServerList 會被 DiscoveryEnabledNIWSServerList 重寫,擴展成從 Eureka 註冊中心獲取服務端列表web
同時它也會用 NIWSDiscoveryPing 來取代 IPing,它將職責委託給 Eureka 來肯定服務端是否已經啓動spring
Spring Cloud Netflix 的微服務都是以 HTTP 接口的形式暴露的,因此能夠用 Apache 的 HttpClient 或 Spring 的 RestTemplate 去調用apache
而 Feign 是一個使用起來更加方便的 HTTP 客戶端,它用起來就好像調用本地方法一樣,徹底感覺不到是調用的遠程方法瀏覽器
總結起來就是:發佈到註冊中心的服務方接口,是 HTTP 的,也能夠不用 Ribbon 或者 Feign,直接瀏覽器同樣可以訪問app
只不過 Ribbon 或者 Feign 調用起來要方便一些,最重要的是:它倆都支持軟負載均衡負載均衡
注意:spring-cloud-starter-feign 裏面已經包含了 spring-cloud-starter-ribbon(Feign 中也使用了 Ribbon)maven
示例代碼以下(也能夠直接從 Github 下載:https://github.com/v5java/demo-cloud-05-ribbon-feign)
它是由五個模塊組成的 Maven 工程,其中包含一個註冊中心、兩個服務提供者、兩個服務消費者(分別由Ribbon和Feign實現)
這是公共的 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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jadyer.demo</groupId> <artifactId>demo-cloud-05</artifactId> <version>1.1</version> <packaging>pom</packaging> <modules> <module>service-client-01</module> <module>service-client-02</module> <module>service-discovery</module> <module>service-server-01</module> <module>service-server-02</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.5.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
這是註冊中心的 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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.jadyer.demo</groupId> <artifactId>demo-cloud-05</artifactId> <version>1.1</version> </parent> <artifactId>service-discovery</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
這是註冊中心的配置文件 /src/main/resources/application.yml
server: port: 1100 eureka: server: enable-self-preservation: false # 關閉自我保護模式(缺省爲打開) eviction-interval-timer-in-ms: 1000 # 續期時間,即掃描失效服務的間隔時間(缺省爲60*1000ms) client: # 設置是否從註冊中心獲取註冊信息(缺省true) # 由於這是一個單點的EurekaServer,不須要同步其它EurekaServer節點的數據,故設爲false fetch-registry: false # 設置是否將本身做爲客戶端註冊到註冊中心(缺省true) # 這裏爲不須要(查看@EnableEurekaServer註解的源碼,會發現它間接用到了@EnableDiscoveryClient) register-with-eureka: false # 在未設置defaultZone的狀況下,註冊中心在本例中的默認地址就是http://127.0.0.1:1100/eureka/ # 但奇怪的是,啓動註冊中心時,控制檯仍是會打印這個地址的節點:http://localhost:8761/eureka/ # 而實際服務端註冊時,要使用1100端口的才能註冊成功,8761端口的會註冊失敗並報告異常 serviceUrl: # 實際測試:若修改尾部的eureka爲其它的,好比/myeureka,註冊中心啓動沒問題,但服務端在註冊時會失敗 # 報告異常:com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server defaultZone: http://127.0.0.1:${server.port}/eureka/
這是註冊中心的 SpringBoot 啓動類 ServiceDiscoveryBootStrap.java
package com.jadyer.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; //建立服務註冊中心 @EnableEurekaServer @SpringBootApplication public class ServiceDiscoveryBootStrap { public static void main(String[] args) { SpringApplication.run(ServiceDiscoveryBootStrap.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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.jadyer.demo</groupId> <artifactId>demo-cloud-05</artifactId> <version>1.1</version> </parent> <artifactId>service-server-01</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> </project>
這是第一個服務提供方的配置文件 /src/main/resources/application.yml
server: port: 2100 spring: application: name: CalculatorServer # 指定發佈的微服務名(之後調用時,只需該名稱便可訪問該服務) eureka: instance: instance-id: ${spring.application.name}:${server.port} prefer-ip-address: true # 設置微服務調用地址爲IP優先(缺省爲false) lease-renewal-interval-in-seconds: 5 # 心跳時間,即服務續約間隔時間(缺省爲30s) lease-expiration-duration-in-seconds: 15 # 發呆時間,即服務續約到期時間(缺省爲90s) client: healthcheck: enabled: true # 開啓健康檢查(依賴spring-boot-starter-actuator) serviceUrl: defaultZone: http://127.0.0.1:1100/eureka/ # 指定服務註冊中心的地址
這是第一個服務提供方的 SpringBoot 啓動類 ServiceServer01BootStarp.java
package com.jadyer.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * 經過 @EnableEurekaClient 註解,爲服務提供方賦予註冊和發現服務的能力 * ------------------------------------------------------------------------------------------------------------------ * 也可使用org.springframework.cloud.client.discovery.@EnableDiscoveryClient註解 * 詳見如下兩篇文章的介紹 * http://cloud.spring.io/spring-cloud-static/Camden.SR3/#_registering_with_eureka * https://spring.io/blog/2015/01/20/microservice-registration-and-discovery-with-spring-cloud-and-netflix-s-eureka * ------------------------------------------------------------------------------------------------------------------ * Created by 玄玉<https://jadyer.cn/> on 2017/1/9 16:00. */ @EnableEurekaClient @SpringBootApplication public class ServiceServer01BootStarp { public static void main(String[] args) { SpringApplication.run(ServiceServer01BootStarp.class, args); } }
這是第一個服務提供方暴露的數學運算服務 CalculatorController.java
package com.jadyer.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * 服務提供方暴露的數學運算服務 * Created by 玄玉<https://jadyer.cn/> on 2017/1/9 16:00. */ @RestController public class CalculatorController { private final Logger logger = LoggerFactory.getLogger(getClass()); @Resource private DiscoveryClient client; @RequestMapping("/add") public int add(int a, int b){ //加運算 int result = a + b; //輸出服務信息 ServiceInstance instance = client.getLocalServiceInstance(); logger.info("uri={},serviceId={},result={}", instance.getUri(), instance.getServiceId(), result); //返回結果 return result; } }
除了啓動端口爲2200外,其代碼與服務提供方01的徹底相同
這是服務消費方Ribbon的 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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.jadyer.demo</groupId> <artifactId>demo-cloud-05</artifactId> <version>1.1</version> </parent> <artifactId>service-client-01</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies> </project>
這是服務消費方Ribbon的配置文件 /src/main/resources/application.yml
server: port: 3100 spring: application: name: client-consumer-ribbon eureka: instance: instance-id: ${spring.application.name}:${server.port} prefer-ip-address: true lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 15 client: healthcheck: enabled: true serviceUrl: defaultZone: http://127.0.0.1:1100/eureka/
這是服務消費方Ribbon的 SpringBoot 啓動類 ServiceClient01BootStarp.java
package com.jadyer.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; 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; @EnableEurekaClient @SpringBootApplication public class ServiceClient01BootStarp { //開啓軟均衡負載 @LoadBalanced @Bean RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ServiceClient01BootStarp.class, args); } }
這是服務消費方Ribbon的遠程服務調用實現 CalculatorService.java
package com.jadyer.demo.ribbon; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @Service class CalculatorService { @Resource private RestTemplate restTemplate; int addService(int a, int b){ String reqURL = "http://CalculatorServer/add?a=" + a + "&b=" + b; return restTemplate.getForEntity(reqURL, Integer.class).getBody(); } }
這是服務消費方Ribbon的調用示例 ConsumerController.java
package com.jadyer.demo.ribbon; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * 服務調用方 * Created by 玄玉<https://jadyer.cn/> on 2017/1/10 18:23. */ @RestController @RequestMapping("/demo/ribbon") public class ConsumerController { @Resource private CalculatorService calculatorService; @RequestMapping("/toadd") int toadd(int a, int b){ return calculatorService.addService(a, b); } }
這是服務消費方Feign的 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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.jadyer.demo</groupId> <artifactId>demo-cloud-05</artifactId> <version>1.1</version> </parent> <artifactId>service-client-02</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies> </project>
這是服務消費方Feign的配置文件 /src/main/resources/application.yml
server: port: 3200 spring: application: name: client-consumer-feign eureka: instance: instance-id: ${spring.application.name}:${server.port} prefer-ip-address: true lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 15 client: healthcheck: enabled: true serviceUrl: defaultZone: http://127.0.0.1:1100/eureka/
這是服務消費方Feign的 SpringBoot 啓動類 ServiceClient02BootStarp.java
package com.jadyer.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; //開啓Feign功能 @EnableFeignClients @EnableEurekaClient @SpringBootApplication public class ServiceClient02BootStarp { public static void main(String[] args) { SpringApplication.run(ServiceClient02BootStarp.class, args); } }
這是服務消費方Feign的遠程服務調用實現 CalculatorService.java
package com.jadyer.demo.feign; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; //綁定該接口到CalculatorServer服務,並通知Feign組件對該接口進行代理(不須要編寫接口實現) @FeignClient(value="CalculatorServer") interface CalculatorService { ////@PathVariable這種也是支持的 //@RequestMapping(value="/add/{a}", method=RequestMethod.GET) //int myadd(@PathVariable("a") int a, @RequestParam("b") int b); //經過SpringMVC的註解來配置所綁定的服務下的具體實現 @RequestMapping(value="/add", method=RequestMethod.GET) int myadd(@RequestParam("a") int a, @RequestParam("b") int b); }
這是服務消費方Feign的調用示例 ConsumerController.java
package com.jadyer.demo.feign; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * 服務調用方 * Created by 玄玉<https://jadyer.cn/> on 2017/1/10 18:23. */ @RestController @RequestMapping("/demo/feign") public class ConsumerController { @Resource private CalculatorService calculatorService; @RequestMapping("/toadd") int toadd(int a, int b){ return calculatorService.myadd(a, b); } }
本文中的示例(Ribbon和Feign)都會將請求走軟負載均衡後,打到服務端
驗證方式就是:分別屢次訪問下面的兩個服務消費URL,兩個服務端打印的請求日誌是均等輸出的
http://10.16.64.133:3100/demo/ribbon/toadd?a=11&b=22
http://10.16.64.133:3200/demo/feign/toadd?a=11&b=22
若沒有走 Ribbon 或者 Feigin,而是直接瀏覽器訪問服務方的接口
以下兩個地址,也能夠訪問成功,只不過沒法享受到軟負載均衡
http://10.16.64.133:2100/add?a=3&b=13
http://10.16.64.133:2200/add?a=3&b=13
最後,補充一張註冊中心的頁面截圖: