在上一篇Spring Cloud構(gòu)建微服務架構(gòu)之服務消費(一)中我們已經(jīng)學會如何使用LoadBalanceClient去訪問微服務提供的接口了。但是在使用的過程中我們發(fā)現(xiàn),需要我們手動的去指定serviceInstance,并根據(jù)serviceInstance去拼接請求url,這對于我們開發(fā)者來說是非常不友好的。本節(jié)我將演示如何使用Spring Cloud Ribbon 負載均衡去消費微服務提供的服務接口。
使用Ribbon
- 1在Services工程下新建新的Module工程,命名biz-customer-service
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">
<parent>
<artifactId>services</artifactId>
<groupId>spring-cloud-demos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>Spring cloud 案例 消費者</name>
<artifactId>biz-customer-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.7.RELEASE</version>
</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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在pom中引入spring-cloud-starter-ribbon依賴
- 2創(chuàng)建主類
package com.snow.spring.cloud.customer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* 微服務之消費者
*
*/
@EnableDiscoveryClient
@SpringBootApplication
public class BizCustomerServiceApp {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args){
SpringApplication.run(BizCustomerServiceApp.class,args);
}
}
對比上一篇文章的主類我們發(fā)現(xiàn)在RestTemplate上多了一個@LoadBalance注解,該注解指定RestTemplate可以通過客戶端負載均衡來去請求微服務接口。
- 3修改Bootstrap.yml
spring:
application:
name: biz-customer-serice
server:
port: 9000
eureka:
instance:
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://localhost:8762/eureka/
services:
urls:
userInfoUrl: http://biz-provider-service1/
修改消費者端口為9000
- 4 增加消費者訪問控制器CustomerController,在Controller包下新建類CustomerController
package com.snow.spring.cloud.customer.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class CustomerController extends RestTemplate{
private static final Logger logger = LoggerFactory.getLogger(CustomerController.class);
@Value("${services.urls.userInfoUrl}")
private String userInfoUrl;
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getUserInfo")
public String getUserInfo(@RequestParam String userName){
logger.info("userInfoUrl:" + userInfoUrl);
return restTemplate.postForObject(userInfoUrl+"/getUserInfo",userName,String.class);
}
}
對比上一小節(jié)Controller里移除了LoadBalanceClient選擇ServiceInstance實例的相關(guān)邏輯,RestTemplate請求接口的拼接也簡化了 ,而是由RestTemplate直接請求微服務的別名。那么這樣的請求為什么可以調(diào)用成功呢?因為Spring Cloud Ribbon有一個攔截器,它能夠在這里進行實際調(diào)用的時候,自動的去選取服務實例,并將實際要請求的IP地址和端口替換這里的服務名,從而完成服務接口的調(diào)用。
- 5 啟動項目在瀏覽器中訪問http://localhost:9000/getUserInfo?userName=snow
請求首先到達消費者的CustomerController的getUserInfo,然后又由RestTemplate調(diào)用postForObject請求微服務名下的getUserInfo接口,從而消費微服務提供的接口。多次刷新瀏覽器,發(fā)現(xiàn)請求分別會被均衡分發(fā)到8801端口和8802端口,也實現(xiàn)的負載均衡。
2018-07-02 11:10:14.939 INFO 11925 --- [nio-8802-exec-1] c.s.s.c.s.controller.UserInfoController : request snow
2018-07-02 11:10:16.822 INFO 11918 --- [nio-8801-exec-8] c.s.s.c.s.controller.UserInfoController : request snow
我們發(fā)現(xiàn)使用Ribbon能夠比使用LoadBalanceClient更少的代碼實現(xiàn)相同的功能,使我們的開發(fā)變得簡單。
至此使用Ribbon消費微服務演示完畢,相關(guān)代碼可以參考
碼市:spring-cloud-demos
本人水平有限,寫的不好,主要是為了記錄自己對SpringCloud的學習之路。
感謝閱讀。