Feign簡單介紹
在開發 Spring Cloud 微服務的時候,我們知道,服務之間都是以 HTTP 接口的形式對外提供服務的,因此消費者在進行調用的時候,底層就是通過 HTTP Client 的這種方式進行訪問。當然我們可以使用JDK原生的 URLConnection、Apache 的 HTTP Client、Netty 異步 Http Client,Spring 的 RestTemplate 去實現服務間的調用。但是最方便、最優雅的方式是通過 Spring Cloud Open Feign 進行服務間的調用 Spring Cloud 對 Feign 進行了增強,使 Feign 支持 Spring Mvc 的註解,並整合了 Ribbon 等,從而讓 Feign 的使用更加方便。
Feign 是一個聲明式的 Web Service 客戶端。它的出現使開發 Web Service 客戶端變得很簡單。使用 Feign 只需要創建一個接口加上對應的註解,比如:@FeignClient 註解。 Feign 有可插拔的註解,包括 Feign 註解和 AX-RS 註解。Feign 也支持編碼器和解碼器,Spring Cloud Open Feign 對 Feign 進行增強支持 Spring Mvc 註解,可以像 Spring Web 一樣使用 HttpMessageConverters 等。
Feign 是一種聲明式、模板化的 HTTP 客戶端。在 Spring Cloud 中使用 Feign,可以做到使用 HTTP 請求訪問遠程服務,就像調用本地方法一樣的,開發者完全感知不到這是在調用遠程方法,更感知不到在訪問 HTTP 請求。接下來介紹一下 Feign 的特性,具體如下:
可插拔的註解支持,包括 Feign 註解和AX-RS註解。
支持可插拔的 HTTP 編碼器和解碼器。
支持 Hystrix 和它的 Fallback。
支持 Ribbon 的負載均衡。
支持 HTTP 請求和響應的壓縮。Feign 是一個聲明式的 WebService 客戶端,它的目的就是讓 Web Service 調用更加簡單。它整合了 Ribbon 和 Hystrix,從而不需要開發者針對 Feign 對其進行整合。Feign 還提供了 HTTP 請求的模板,通過編寫簡單的接口和註解,就可以定義好 HTTP 請求的參數、格式、地址等信息。Feign 會完全代理 HTTP 的請求,在使用過程中我們只需要依賴注入 Bean,然後調用對應的方法傳遞參數即可。
Feign小測試Demo
上一篇我們介紹了Ribbon,使用springboot爲我們提供的RestTemplate進行遠程服務的調用,Ribbon的缺點是代碼混亂,不清晰。下面我們就用Feign來實現我們的遠程服務調用吧。
完整的項目目錄如下,用到了1個pom,1個配置文件,1個啓動類,1個service,1個controller:
第一步構建一個空的springboot項目,引入feign的依賴
在springboot的pom裏面引入Fegin依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
完整的pom如下:
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>pers.cc</groupId>
<artifactId>springCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>feign</artifactId>
<name>feign</name>
<description>Feign消費者</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
第二步,修改配置文件
#配置服務及端口
spring.application.name=feign-consumer
server.port=4001
#註冊到註冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#從eureka服務器註冊表中獲取註冊信息的時間間隔(s),默認爲30秒
eureka.client.registry-fetch-interval-seconds=5
#fegin連接的超時時間 10s #服務名,填寫default爲所有服務
feign.client.config.EUREKA-CLIENT.connectTimeout=10000
#fegin讀取的超時時間 10s #服務名,填寫default爲所有服務
feign.client.config.EUREKA-CLIENT.readTimeout=10000
第三步,我們要加上支持feign的註解@EnableFeignClients
package pers.cc.springCloud.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
第四步,編寫服務類,EUREKA-CLIENT
package pers.cc.springCloud.feign.test.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
//根據服務名稱來指定服務提供方
@FeignClient("EUREKA-CLIENT")
public interface TestService {
// 通過註解指定訪問方式,訪問路徑,訪問參數
@RequestMapping(method = RequestMethod.GET, value = "/add")
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
@RequestMapping(method = RequestMethod.GET, value = "/dc")
String dc();
@RequestMapping(method = RequestMethod.GET, value = "/stop")
String stop();
}
Feign超時配置