概述
最近結合一些別人的開源項目來學習Spring Cloud,其中關於服務消費這方面的一個很便利的工具 Feign讓我記憶頗深。雖然網上的Demo和例子不勝枚舉,但大多比較分散,本文就來集中記錄一下聲明式客戶端 Feign的一些使用姿勢。
注: 本文首發於 博客 CodeSheep · 程序羊,歡迎光臨 小站!
下文就結合例子來記錄這一過程,代碼在文尾處。
創建基於 Eureka的服務註冊中心
三個步驟即可搞定:
- 建工程
創建一個名爲 eureka_server
的 SpringBoot工程,並在pom.xml中添加好對應依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
- 改主類
修改應用主類,添加 @EnableEurekaServer
註解
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- 加配置
配置 application.properties
文件如下所示:
spring.application.name=eureka-server
server.port=1111
eureka.instance.hostname=localhost
#默認設置下,服務註冊中心自己也會將自己作爲客戶端來嘗試註冊它自己,所以我們需要禁用它的客戶端註冊行爲
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
- 啓動服務註冊中心
瀏覽器訪問之:
此時還沒有任何服務註冊上來。
創建服務提供者
- 建工程
創建一個名爲 service_provider
的 SpringBoot工程,並在pom.xml中添加好對應依賴:
<dependencies>
<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>
</dependencies>
- 改主類
添加 @EnableDiscoveryClient
註解
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
- 添加控制器
DateServiceController
提供一個Restful接口而已,該接口的作用是獲取服務器上的時間並返回
@RestController
public class DateServiceController {
@RequestMapping( value = "/test", method = RequestMethod.GET )
public String test( @RequestParam String param ){
return "hello " + param;
}
}
- 配置 application.properties文件
spring.application.name=service_provider
server.port=1112
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
- 啓動工程
瀏覽器訪問服務註冊中心,我們發現服務提供者 service_provider
已經註冊到 eureka_server
上:
同時瀏覽器訪問:http://localhost:1112/test?param=www.codesheep.cn
,可以測試服務提供 service_provider
提供的接口工作正常
接下來我們創建服務消費者,是 Feign該登場的時候了!
創建基於 Feign的服務消費者
- 創建一個名爲
service_consumer
的 SpringBoot工程,並在pom.xml中添加好對應依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 修改應用主類
主要是添加有關 Feign客戶端的一些註解而已
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
}
- 創建一個 Feign客戶端的接口:
DateServiceFeignClientInterface
很明顯其內部用 @FeignClient( value = "service-provider" )
聲明的方式指向了 服務提供者,而接口方法則實現了對 服務提供者接口的實際調用
@FeignClient( value = "service-provider" )
public interface DateServiceFeignClientInterface {
@GetMapping("/test")
String consumer( @RequestParam("param") String param );
}
- 創建控制器:
DateServiceFeignController
注意,這是服務消費者提供的 Rest接口
@RestController
@RequestMapping("/consumer")
public class DateServiceFeignController {
@Resource
DateServiceFeignClientInterface dateServiceFeignClientInterface;
@GetMapping("/date")
public String getDate( @RequestParam String param ) {
return dateServiceFeignClientInterface.consumer( param );
}
}
- 配置
application.properties
spring.application.name=service-consumer
server.port=1113
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
- 啓動服務消費者
我們先去服務註冊中心上看看,發現 服務消費者也註冊上來了:
然後我們瀏覽器訪問 服務消費者提供的Rest接口: http://localhost:1113/consumer/date?param=www.codesheep.cn
這樣我們就通過 服務消費者的 Feign客戶端 取到了服務提供者 給予的接口數據。
上面這就是聲明式客戶端 Feign的第一種使用姿勢,也是常用的手法,常見於很多Demo
下面我們來實踐一下關於 Feign的繼承與實現機制,發現其使用更加靈活( Feign支持接口繼承方式快速生成客戶端,頗有點RPC的意思(關於RPC的實踐可以參考我的文章:《RPC框架實踐之:Google gRPC》、《RPC框架實踐之:Apache Thrift》) )
抽象出一個公共的 API服務
-
創建一個普通 Maven項目:
service_provider_api
-
創建一個公共接口:
DateService
public interface DateService {
@GetMapping("/api/test")
String consumer( @RequestParam("param") String param );
}
改造之前的 服務提供者 / 消費者項目
- 在服務消費者
service_consumer
項目中添加一個新的Feign的客戶端接口
@FeignClient( value = "service-provider" )
public interface DateServiceFeignClientInterface2 extends DateService {
}
- 並且在
service_consumer
項目中添加一個新的控制器DateServiceFeignController2
@RestController
@RequestMapping("/consumer2")
public class DateServiceFeignController2 {
@Resource
DateServiceFeignClientInterface2 dateServiceFeignClientInterface2;
@GetMapping("/date")
public String getDate( @RequestParam String param ) {
return dateServiceFeignClientInterface2.consumer( param );
}
}
- 在服務提供者
service_provider
項目中來實現我們在公共api項目service_provider_api
中的DateService
接口,賦予實際邏輯
@RestController
public class DateServiceController2 implements DateService {
@Override
public String consumer( @RequestParam String param) {
Date now = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk點mm分" );
String nowTime = simpleDateFormat.format( now );
return "hello again " + param + ", " + nowTime;
}
}
- 依次將
eureka_server
、service_provider
、service_consumer
三個項目分別啓動
瀏覽器訪問:http://localhost:1113/consumer2/date?param=www.codesheep.cn
使用 feign的繼承特性時,可以將服務接口的定義從服務消費者中剝離出去,形成獨立的api項目從而可以很方便的實現接口定義和依賴的共享,不用再複製粘貼接口進行綁定,當然這種做法存在的問題就是可能會導致服務提供者和服務消費者間的耦合度增高,此時如果服務提供者修改了一個接口定義,服務消費者可能也得跟着變,進而帶來一些坑。
後 記
由於能力有限,若有錯誤或者不當之處,還請大家批評指正,一起學習交流!
- My Personal Blog:CodeSheep 程序羊
- 我的半年技術博客之路