javascript
Spring Cloud构建微服务架构(二)服务消费者
Ribbon
Ribbon是一個基于HTTP和TCP客戶端的負載均衡器。Feign中也使用Ribbon,后續會介紹Feign的使用。
Ribbon可以在通過客戶端中配置的ribbonServerList服務端列表去輪詢訪問以達到均衡負載的作用。
當Ribbon與Eureka聯合使用時,ribbonServerList會被DiscoveryEnabledNIWSServerList重寫,擴展成從Eureka注冊中心中獲取服務端列表。同時它也會用NIWSDiscoveryPing來取代IPing,它將職責委托給Eureka來確定服務端是否已經啟動。
下面我們通過實例看看如何使用Ribbon來調用服務,并實現客戶端的均衡負載。
準備工作
- 啟動Chapter-9-1-1中的服務注冊中心:eureka-server
- 啟動Chapter-9-1-1中的服務提供方:compute-service
- 修改compute-service中的server-port為2223,再啟動一個服務提供方:compute-service
此時訪問:http://localhost:1111/
alt
可以看到COMPUTE-SERVICE服務有兩個單元正在運行:
- 192.168.21.101:compute-service:2222
- 192.168.21.101:compute-service:2223
使用Ribbon實現客戶端負載均衡的消費者
構建一個基本Spring Boot項目,并在pom.xml中加入如下內容:
|
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
? <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </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>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
在應用主類中,通過@EnableDiscoveryClient注解來添加發現服務能力。創建RestTemplate實例,并通過@LoadBalanced注解開啟均衡負載能力。
|
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
? @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的值。
|
? 1 2 3 4 5 6 7 8 9 10 11 12 |
? @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服務注冊中心
|
? 1 2 3 4 5 |
? spring.application.name=ribbon-consumer server.port=3333 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ |
啟動該應用,并訪問兩次:http://localhost:3333/add
然后,打開compute-service的兩個服務提供方,分別輸出了類似下面的日志內容:
- 端口為2222服務提供端的日志:
|
? 1 |
? 2016-06-02 11:16:26.787 INFO 90014 --- [io-2222-exec-10] com.didispace.web.ComputeController : /add, host:192.168.21.101, service_id:compute-service, result:30 |
- 端口為2223服務提供端的日志:
|
? 1 |
? 2016-06-02 11:19:41.241 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在客戶端已經實現了對服務調用的均衡負載。
完整示例可參考:Chapter9-1-2/eureka-ribbon
Feign
Feign是一個聲明式的Web Service客戶端,它使得編寫Web Serivce客戶端變得更加簡單。我們只需要使用Feign來創建一個接口并用注解來配置它既可完成。它具備可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的編碼器和解碼器。Spring Cloud為Feign增加了對Spring MVC注解的支持,還整合了Ribbon和Eureka來提供均衡負載的HTTP客戶端實現。
下面,通過一個例子來展現Feign如何方便的聲明對上述computer-service服務的定義和調用。
創建一個Spring Boot工程,配置pom.xml,將上述的配置中的ribbon依賴替換成feign的依賴即可,具體如下:
|
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
? <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </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>Brixton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
在應用主類中通過@EnableFeignClients注解開啟Feign功能,具體如下:
|
? 1 2 3 4 5 6 7 8 9 10 |
? @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class FeignApplication { public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); } } |
定義compute-service服務的接口,具體如下:
|
? 1 2 3 4 5 6 7 |
? @FeignClient("compute-service") public interface ComputeClient { @RequestMapping(method = RequestMethod.GET, value = "/add") Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b); } |
- 使用@FeignClient("compute-service")注解來綁定該接口對應compute-service服務
- 通過Spring MVC的注解來配置compute-service服務下的具體實現。
在web層中調用上面定義的ComputeClient,具體如下:
|
? 1 2 3 4 5 6 7 8 9 10 11 12 |
? @RestController public class ConsumerController { @Autowired ComputeClient computeClient; @RequestMapping(value = "/add", method = RequestMethod.GET) public Integer add() { return computeClient.add(10, 20); } } |
application.properties中不用變,指定eureka服務注冊中心即可,如:
|
? 1 2 3 4 |
? spring.application.name=feign-consumer server.port=3333 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ |
啟動該應用,訪問幾次:http://localhost:3333/add
再觀察日志,可以得到之前使用Ribbon時一樣的結果,對服務提供方實現了均衡負載。
這一節我們通過Feign以接口和注解配置的方式,輕松實現了對compute-service服務的綁定,這樣我們就可以在本地應用中像本地服務一下的調用它,并且做到了客戶端均衡負載。
完整示例可參考:Chapter9-1-2/eureka-feign
【轉載請注明出處】:http://blog.didispace.com/springcloud2/
總結
以上是生活随笔為你收集整理的Spring Cloud构建微服务架构(二)服务消费者的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cloud构建微服务架构(
- 下一篇: Spring Cloud构建微服务架构(