概述
最近結(jié)合一些別人的開源項(xiàng)目來學(xué)習(xí)Spring Cloud茴恰,其中關(guān)于服務(wù)消費(fèi)這方面的一個(gè)很便利的工具 Feign讓我記憶頗深。雖然網(wǎng)上的Demo和例子不勝枚舉澡为,但大多比較分散升筏,本文就來集中記錄一下聲明式客戶端 Feign的一些使用姿勢(shì)。
注: 本文首發(fā)于 博客 CodeSheep · 程序羊帆吻,歡迎光臨 小站!
下文就結(jié)合例子來記錄這一過程蜕依,代碼在文尾處桅锄。
創(chuàng)建基于 Eureka的服務(wù)注冊(cè)中心
三個(gè)步驟即可搞定:
- 建工程
創(chuàng)建一個(gè)名為 eureka_server
的 SpringBoot工程琉雳,并在pom.xml中添加好對(duì)應(yīng)依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
- 改主類
修改應(yīng)用主類样眠,添加 @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
#默認(rèn)設(shè)置下,服務(wù)注冊(cè)中心自己也會(huì)將自己作為客戶端來嘗試注冊(cè)它自己翠肘,所以我們需要禁用它的客戶端注冊(cè)行為
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
- 啟動(dòng)服務(wù)注冊(cè)中心
瀏覽器訪問之:
此時(shí)還沒有任何服務(wù)注冊(cè)上來檐束。
創(chuàng)建服務(wù)提供者
- 建工程
創(chuàng)建一個(gè)名為 service_provider
的 SpringBoot工程,并在pom.xml中添加好對(duì)應(yīng)依賴:
<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
提供一個(gè)Restful接口而已束倍,該接口的作用是獲取服務(wù)器上的時(shí)間并返回
@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/
- 啟動(dòng)工程
瀏覽器訪問服務(wù)注冊(cè)中心被丧,我們發(fā)現(xiàn)服務(wù)提供者 service_provider
已經(jīng)注冊(cè)到 eureka_server
上:
同時(shí)瀏覽器訪問:http://localhost:1112/test?param=www.codesheep.cn
盟戏,可以測(cè)試服務(wù)提供 service_provider
提供的接口工作正常
接下來我們創(chuàng)建服務(wù)消費(fèi)者,是 Feign該登場(chǎng)的時(shí)候了甥桂!
創(chuàng)建基于 Feign的服務(wù)消費(fèi)者
- 創(chuàng)建一個(gè)名為
service_consumer
的 SpringBoot工程柿究,并在pom.xml中添加好對(duì)應(yīng)依賴
<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>
- 修改應(yīng)用主類
主要是添加有關(guān) Feign客戶端的一些注解而已
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
}
- 創(chuàng)建一個(gè) Feign客戶端的接口:
DateServiceFeignClientInterface
很明顯其內(nèi)部用 @FeignClient( value = "service-provider" )
聲明的方式指向了 服務(wù)提供者,而接口方法則實(shí)現(xiàn)了對(duì) 服務(wù)提供者接口的實(shí)際調(diào)用
@FeignClient( value = "service-provider" )
public interface DateServiceFeignClientInterface {
@GetMapping("/test")
String consumer( @RequestParam("param") String param );
}
- 創(chuàng)建控制器:
DateServiceFeignController
注意黄选,這是服務(wù)消費(fèi)者提供的 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/
- 啟動(dòng)服務(wù)消費(fèi)者
我們先去服務(wù)注冊(cè)中心上看看蝇摸,發(fā)現(xiàn) 服務(wù)消費(fèi)者也注冊(cè)上來了:
然后我們?yōu)g覽器訪問 服務(wù)消費(fèi)者提供的Rest接口: http://localhost:1113/consumer/date?param=www.codesheep.cn
這樣我們就通過 服務(wù)消費(fèi)者的 Feign客戶端 取到了服務(wù)提供者 給予的接口數(shù)據(jù)。
上面這就是聲明式客戶端 Feign的第一種使用姿勢(shì)办陷,也是常用的手法貌夕,常見于很多Demo
下面我們來實(shí)踐一下關(guān)于 Feign的繼承與實(shí)現(xiàn)機(jī)制,發(fā)現(xiàn)其使用更加靈活( Feign支持接口繼承方式快速生成客戶端民镜,頗有點(diǎn)RPC的意思(關(guān)于RPC的實(shí)踐可以參考我的文章:《RPC框架實(shí)踐之:Google gRPC》啡专、《RPC框架實(shí)踐之:Apache Thrift》) )
抽象出一個(gè)公共的 API服務(wù)
創(chuàng)建一個(gè)普通 Maven項(xiàng)目:
service_provider_api
創(chuàng)建一個(gè)公共接口:
DateService
public interface DateService {
@GetMapping("/api/test")
String consumer( @RequestParam("param") String param );
}
改造之前的 服務(wù)提供者 / 消費(fèi)者項(xiàng)目
- 在服務(wù)消費(fèi)者
service_consumer
項(xiàng)目中添加一個(gè)新的Feign的客戶端接口
@FeignClient( value = "service-provider" )
public interface DateServiceFeignClientInterface2 extends DateService {
}
- 并且在
service_consumer
項(xiàng)目中添加一個(gè)新的控制器DateServiceFeignController2
@RestController
@RequestMapping("/consumer2")
public class DateServiceFeignController2 {
@Resource
DateServiceFeignClientInterface2 dateServiceFeignClientInterface2;
@GetMapping("/date")
public String getDate( @RequestParam String param ) {
return dateServiceFeignClientInterface2.consumer( param );
}
}
- 在服務(wù)提供者
service_provider
項(xiàng)目中來實(shí)現(xiàn)我們?cè)诠瞐pi項(xiàng)目service_provider_api
中的DateService
接口,賦予實(shí)際邏輯
@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點(diǎn)mm分" );
String nowTime = simpleDateFormat.format( now );
return "hello again " + param + ", " + nowTime;
}
}
- 依次將
eureka_server
制圈、service_provider
们童、service_consumer
三個(gè)項(xiàng)目分別啟動(dòng)
瀏覽器訪問:http://localhost:1113/consumer2/date?param=www.codesheep.cn
使用 feign的繼承特性時(shí),可以將服務(wù)接口的定義從服務(wù)消費(fèi)者中剝離出去离唐,形成獨(dú)立的api項(xiàng)目從而可以很方便的實(shí)現(xiàn)接口定義和依賴的共享病附,不用再復(fù)制粘貼接口進(jìn)行綁定,當(dāng)然這種做法存在的問題就是可能會(huì)導(dǎo)致服務(wù)提供者和服務(wù)消費(fèi)者間的耦合度增高亥鬓,此時(shí)如果服務(wù)提供者修改了一個(gè)接口定義完沪,服務(wù)消費(fèi)者可能也得跟著變,進(jìn)而帶來一些坑嵌戈。
后 記
由于能力有限覆积,若有錯(cuò)誤或者不當(dāng)之處,還請(qǐng)大家批評(píng)指正熟呛,一起學(xué)習(xí)交流宽档!
- My Personal Blog:CodeSheep 程序羊
- 我的半年技術(shù)博客之路