https://mp.weixin.qq.com/s/fViea_Q2R8D6-CBECXO8gQjava
經過上一篇《Spring Cloud構建微服務架構:服務註冊與發現》,咱們已經成功地將服務提供者:compute-service服務註冊到Eureka服務註冊中心或Consul服務端上了,那麼咱們要如何去消費服務提供者的接口呢?web
Spring Cloud Ribbon是基於Netflix Ribbon實現的一套客戶端負載均衡的工具。它是一個基於HTTP和TCP的客戶端負載均衡器。它能夠經過在客戶端中配置ribbonServerList來設置服務端列表去輪詢訪問以達到均衡負載的做用。spring
當Ribbon與Eureka聯合使用時,ribbonServerList會被DiscoveryEnabledNIWSServerList重寫,擴展成從Eureka註冊中心中獲取服務實例列表。同時它也會用NIWSDiscoveryPing來取代IPing,它將職責委託給Eureka來肯定服務端是否已經啓動。微信
而當Ribbon與Consul聯合使用時,ribbonServerList會被ConsulServerList來擴展成從Consul獲取服務實例列表。同時由ConsulPing來做爲IPing接口的實現。架構
咱們在使用Spring Cloud Ribbon的時候,不管是與Eureka仍是Consul結合,都會在引入Spring Cloud Eureka或Spring Cloud Consul依賴的時候經過自動化配置來加載上述所說的配置內容,因此咱們能夠快速在Spring Cloud中實現服務間調用的負載均衡。app
下面咱們經過具體的例子來看看如何使用Spring Cloud Ribbon來實現服務的調用以及客戶端均衡負載。負載均衡
啓動上一篇《Spring Cloud構建微服務架構:服務註冊與發現》中構建的服務註冊中心和服務提供方:eureka-server(或consul)、compute-service,其中compute-service須要啓動多個實例。ide
在本地調試的時候,因爲端口不能衝突,因此同一個服務咱們須要使用不一樣的端口來啓動。啓動的方法有不少,好比能夠經過以下命令的方式:spring-boot
java -jar compute-service.jar --server.port=8081 java -jar compute-service.jar --server.port=8082
在啓動完成以後,咱們能夠經過訪問eureka-service的ui頁面看到compute-service的多個實例被註冊到了eureka-server上。
使用Ribbon實現客戶端負載均衡的消費者微服務
構建一個基本Spring Boot項目,並在pom.xml中加入以下內容(如使用Consul,將spring-cloud-starter-eureka替換成spring-cloud-starter-consul-discovery便可):
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> </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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在應用主類中,經過@EnableDiscoveryClient註解來添加發現服務能力。建立RestTemplate實例,並經過@LoadBalanced註解開啓均衡負載能力。
@SpringBootApplication @EnableDiscoveryClient public class RibbonApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(RibbonApplication.class, args); } }
建立ConsumerController來消費COMPUTE-SERVICE的add服務。經過直接RestTemplate來調用服務,計算10 + 20的值。
@RestController public class ConsumerController { @Autowired RestTemplate restTemplate; @RequestMapping(value = "/add", method = RequestMethod.GET) public String add() { return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody(); } }
在application.properties中配置eureka服務註冊中心
spring.application.name=ribbon-consumer server.port=3333 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
啓動該應用,並訪問兩次:http://localhost:3333/add。而後,打開compute-service的兩個服務提供方,分別輸出了相似下面的日誌內容:
-- 端口爲2222服務提供端的日誌: INFO 90014 --- [io-2222-exec-10] com.didispace.web.ComputeController : /add, host:192.168.21.101, service_id:compute-service, result:30 -- 端口爲2223服務提供端的日誌: INFO 90122 --- [nio-2223-exec-1] com.didispace.web.ComputeController : /add, host:192.168.21.101, service_id:compute-service, result:30
能夠看到,以前啓動的兩個compute-service服務端分別被調用了一次。到這裏,咱們已經經過Ribbon在客戶端已經實現了對服務調用的均衡負載。
Spring Cloud Feign是一套基於Netflix Feign實現的聲明式服務調用客戶端。它使得編寫Web服務客戶端變得更加簡單。咱們只須要經過建立接口並用註解來配置它既可完成對Web服務接口的綁定。它具有可插拔的註解支持,包括Feign註解、JAX-RS註解。它也支持可插拔的編碼器和解碼器。Spring Cloud Feign還擴展了對Spring MVC註解的支持,同時還整合了Ribbon和Eureka來提供均衡負載的HTTP客戶端實現。
下面,咱們經過一個例子來展示Feign如何方便的聲明對上述computer-service服務的定義和調用。
建立一個Spring Boot工程,配置pom.xml,引入eureka和feign的依賴,具體以下(如使用Consul,將spring-cloud-starter-eureka替換成spring-cloud-starter-consul-discovery便可):
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在應用主類中經過@EnableFeignClients註解開啓Feign功能,具體以下:
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class FeignApplication { public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); } }
定義compute-service服務的接口,具體以下:
@FeignClient("compute-service") public interface ComputeClient { @RequestMapping(method = RequestMethod.GET, value = "/add") Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b); }
在web層中調用上面定義的ComputeClient,具體以下:
@RestController public class ConsumerController { @Autowired ComputeClient computeClient; @RequestMapping(value = "/add", method = RequestMethod.GET) public Integer add() { return computeClient.add(10, 20); } }
application.properties中不用變,指定eureka服務註冊中心便可,如:
spring.application.name=feign-consumer server.port=3333 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
啓動該應用,訪問幾回:http://localhost:3333/add
再觀察日誌,能夠獲得以前使用Ribbon時同樣的結果,對服務提供方實現了均衡負載。
本節咱們經過Feign以接口和註解配置的方式,輕鬆實現了對compute-service服務的綁定,這樣咱們就能夠在本地應用中像本地服務一下的調用它,而且作到了客戶端均衡負載。
更多關於Spring Cloud的內容可掃以下二維碼關注個人公衆號。關於新書《Spring Cloud實戰》最新動態將第一時間在公衆號中推送。
版權聲明
本文采用 CC BY 3.0 CN協議 進行許可。 可自由轉載、引用,但需署名做者且註明文章出處。如轉載至微信公衆號,請在文末添加做者公衆號二維碼。
友情推薦
最具顏值的瓦泥匠SAMA