介紹
Feign
OpenFeign是聲明式、模板化的HTTP請求客戶端。可以更加便捷、優雅地調用http api。
OpenFeign會根據帶有注解的函數信息構建出網絡請求的模板,在發送網絡請求之前,OpenFeign會將函數的參數值設置到這些請求模板中。
主要是用來構建微服務消費端。只要使用OpenFeign提供的注解修飾定義網絡請求的接口類,就可以使用該接口的實例發送RESTful的網絡請求。還可以集成Ribbon和Hystrix,提供負載均衡和斷路器。
Feign和OpenFeign的關系
- Feign本身不支持Spring MVC的注解,它有一套自己的注解
- OpenFeign是Spring Cloud 在Feign的基礎上支持了Spring MVC的注解,如@RequesMapping等等。 OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口, 並通過動態代理的方式產生實現類,實現類中做負載均衡並調用其他服務。
踩坑說明:
通過Feign發送Get請求,一定要在接口的參數前面加上@RequestParam
注解,否則會報如下的錯誤:
feign.FeignException$MethodNotAllowed: [405] during [GET] to [http://api-02-application/getObjParam] [PersonClient#getObjParam(String)]: [{"timestamp":"2020-09-16T05:35:22.853+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/getObjParam"}]
准備服務提供者
構建一個新的項目API-02-APPLICATION,提供訪問的API,並注冊到注冊中心(這里使用Eureka做注冊中心)上
(OPENFEIGN-APP是等下要構建的項目,這里先不用管)
API-02-APPLICATION的pom文件以及配置如下:
<?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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-client002</artifactId>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.qiankai</groupId>
<artifactId>client-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件:
server:
port: 8001
eureka:
client:
# 設置服務注冊中心的url
service-url:
defaultZone: http://localhost:7900/eureka
instance:
instance-id: qiankai-client-002 #顯示此名字(默認是當前項目http://localhost:8001)
prefer-ip-address: true #訪問路徑可以顯示ip地址
spring:
application:
name: api-02-application
在API-02-APPLICATION中的Controller中提供兩個接口,以便等下在其他項目中演示調用該接口:
@GetMapping("/getObjParam")
public Person getObjParam(String name) {
Person person = new Person();
person.setId(100);
person.setName(name);
return person;
}
@PostMapping("/postParam")
public Person postParam(@RequestBody String name) {
System.out.println("name:" + name);
Person person = new Person();
person.setId(100);
person.setName("xiaoming" + name);
return person;
}
OpenFeign 基本使用
構建項目OPENFEIGN-APP
- 引入OpenFeign依賴
<!-- openFeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>client-open-feign</artifactId>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencies>
<!-- 導入自定義公共模塊中的Person類 -->
<dependency>
<groupId>com.qiankai</groupId>
<artifactId>client-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- openFeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 用於上報節點信息 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 配置文件
server:
port: 8020
eureka:
client:
# 設置服務注冊中心的url
service-url:
defaultZone: http://localhost:7900/eureka
instance:
instance-id: qiankai-client-8020 #顯示此名字(默認是當前項目http://localhost:8001)
prefer-ip-address: true #訪問路徑可以顯示ip地址
spring:
application:
name: openfeign-app
- 啟動類上添加注解 @EnableFeignClients
package com.qiankai.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* OpenFeign 客戶端調用
*
* @author kai_qian
* @version v1.0
* @since 2020/09/16 09:15
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients // 激活Feign
public class AppOpenFeign {
public static void main(String[] args) {
SpringApplication.run(AppOpenFeign.class, args);
}
}
- 編寫客戶端調用接口
package com.qiankai.feign.client;
import com.qiankai.common.dto.Person;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
/**
* OpenFeign 客戶端調用演示
*
* @author kai_qian
* @version v1.0
* @since 2020/09/16 09:24
*/
@FeignClient(value = "API-02-APPLICATION") // value為 Eureka上的服務應用名,會自動去注冊中心中查找對應的服務地址
public interface PersonClient {
@GetMapping("/getObjParam")
Person getObjParam(@RequestParam("name") String name); // GET 請求的參數一定要加上 @RequestParam注解,否則會報錯
@PostMapping("/postParam")
Person postParam(@RequestBody String name);
}
- 通過Feign客戶端調用其他服務的接口
package com.qiankai.feign.controller;
import com.qiankai.common.dto.Person;
import com.qiankai.feign.client.PersonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* openFeign 客戶端調用演示
*
* @author kai_qian
* @version v1.0
* @since 2020/09/16 09:27
*/
@RestController
public class PersonController {
@Autowired
private PersonClient personClient;
@GetMapping("/testGetPerson")
public void getPerson() {
Person person = personClient.getObjParam("aaa");
System.out.println(person.toString());
}
@GetMapping("/testPostPerson")
public void postPerson() {
Person person = personClient.postParam("bbb");
System.out.println(person.toString());
}
}
- 結果展示
依次訪問接口 /testGetPerson
, /testPostPerson
,控制台打印返回的Person信息
便捷的使用方式
上面的方式調用其他客戶端接口,每次都需要將API接口手動寫一遍,比較繁瑣,可以通過將API寫成接口,單獨抽成一個項目模塊,然后在原項目中引入API模塊,Controller繼承這些接口。
其他客戶端想要調用這些接口的時候,只需要引入 API接口模塊,然后通過接口直接繼承即可。
例如,有項目中有兩個模塊,User-Module
用戶模塊,Article-Module
文章模塊,在Article-Module
中想通過Feign調用User-Module
的接口。
在構建 User-Module
的時候,可以先構建一個 User-Module-Api
模塊,在User-Module-Api
模塊中定義 User-Module 中的所有接口:
// 代碼在 User-Module-Api 模塊中
public interface UserApi {
@PutMapping("/v1/user")
Result update(@RequestBody UserReqDto userReqDto);
@GetMapping("/v1/user/{id}")
Result<UserRespDto> findUserById(@PathVariable("id") Long id);
@PostMapping("/v1/user/subscribe")
Result<SubscribeRespDto> follow(@RequestParam("id") Long id);
@DeleteMapping("/v1/user/subscribe")
Result unFollow(@RequestParam("id") Long id);
}
然后在 User-Module 模塊中 引入 User-Module-Api 的代碼(pom文件中添加User-Module-Api的依賴),編寫Controller
// 代碼在 User-Modul 模塊中
@RestController
public class UserController implements UserApi {
private final UserService userService;
@Override
public Result update(UserReqDto userReqDto) {
// doSomething
return Result.newSuccess();
}
@Override
public Result<UserRespDto> findUserById(Long id) {
// doSomething
return Result.newSuccess(userRespDto);
}
@Override
public Result follow(Long id) {
// doSomething
return Result.newSuccess(subscribeRespDto);
}
@Authentication
@Override
public Result unFollow(Long id) {
// doSomething
return Result.newSuccess();
}
}
上面將User模塊的 接口路徑參數 和 具體實現邏輯 進行了分離,分為了兩個模塊
在 Article-Module 項目中,如果想調用 User-Module 的接口,僅需要引入 User-Module-Api 的依賴,然后通過繼承該接口即可
// 代碼在 Article-Modul 模塊中
@FeignClient("User-Module")
public interface UserClient extends UserApi {
// 無需編寫內容
}
Feign自定義配置
為了演示配置,對服務提供者的接口加上權限校驗
API-02-APPLICATION添加權限
修改前面的 API-02-APPLICATION應用,添加權限校驗:
pom中添加依賴:
<!-- 安全認證 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
新建權限認證配置類:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 關閉csrf
http.csrf().disable();
// 表示所有的訪問都必須認證,認證處理后才可以正常進行
http.httpBasic().and().authorizeRequests().anyRequest().fullyAuthenticated();
// 所有的rest服務一定要設置為無狀態,以提升操作效率和性能
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
配置文件中添加:
spring:
security:
user:
name: root
password: root
此時再訪問 API-02-APPLICATION應用的接口,會彈窗要求登錄,只有通過驗證才能繼續訪問:
如果不對 Feign進行配置就直接調用接口會報錯
OPENFEIGN-APP的OpenFeign配置
在 OPENFEIGN-APP 中有兩種方式進行配置
- 自定義配置類
- 增加攔截器
方式一:自定義配置類
- 編寫配置類(不用加 @Component):
package com.qiankai.api.config;
import feign.auth.BasicAuthRequestInterceptor;
import org.springframework.context.annotation.Bean;
/**
* OpenFeign 配置
*
* @author kai_qian
* @version v1.0
* @since 2020/09/16 14:44
*/
public class FeignAuthConfiguration {
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("root", "root");
}
}
- 在feign上加配置
@FeignClient(name = "service-valuation",configuration = FeignAuthConfiguration.class)
package com.qiankai.feign.client;
import com.qiankai.common.dto.Person;
import com.qiankai.feign.config.FeignAuthConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
/**
* OpenFeign 客戶端調用演示
*
* @author kai_qian
* @version v1.0
* @since 2020/09/16 09:24
*/
@FeignClient(name = "API-02-APPLICATION", configuration = FeignAuthConfiguration.class) // 添加了配置
public interface PersonClient {
@GetMapping("/getObjParam")
Person getObjParam(@RequestParam("name") String name);
@PostMapping("/postParam")
Person postParam(@RequestBody String name);
}
這樣接口就又可以正常訪問了。
小結:
如果在配置類上添加了@Configuration注解,並且該類在@ComponentScan所掃描的包中,那么該類中的配置信息就會被所有的@FeignClient共享。
最佳實踐是:不指定@Configuration注解(或者指定configuration,用注解忽略),而是手動:@FeignClient(name = "service-valuation",configuration = FeignAuthConfiguration.class)
方式二:添加攔截器
取消上面的方式一所做的操作,訪問報錯,然后:
1.新建一個攔截器
package com.qiankai.feign.config;
import feign.RequestInterceptor;
import feign.RequestTemplate;
/**
* 攔截器
*
* @author kai_qian
* @version v1.0
* @since 2020/09/16 14:56
*/
public class MyBasicAuthRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
// TODO Auto-generated method stub
template.header("Authorization", "Basic cm9vdDpyb290");
}
}
說明:
cm9vdDpyb290 表示通過 BASE64 加密過的賬號密碼
- 配置文件中添加 feign攔截器的配置
server:
port: 8020
eureka:
client:
# 設置服務注冊中心的url
service-url:
defaultZone: http://localhost:7900/eureka
instance:
instance-id: qiankai-client-8020 #顯示此名字(默認是當前項目http://localhost:8001)
prefer-ip-address: true #訪問路徑可以顯示ip地址
spring:
application:
name: openfeign-app
feign:
client:
config:
API-02-APPLICATION:
requestInterceptors:
- com.qiankai.feign.config.MyBasicAuthRequestInterceptor
重新啟動OPENFEIGN-APP服務,又可以正常的調用接口了
說明
Feign服務配置
指定服務名稱的配置:
feign:
client:
config:
api-02-application:
connect-timeout: 5000
read-timeout: 5000
logger-level: full
通用配置:
feign:
client:
config:
default:
connect-timeout: 5000
read-timeout: 5000
logger-level: full
屬性配置比Java代碼優先級高。也可通過配置設置java代碼優先級高。
feign:
client:
default-to-properties: false
原理
- 主程序入口添加@EnableFeignClients注解開啟對Feign Client掃描加載處理。根據Feign Client的開發規范,定義接口並加@FeignClient注解。
- 當程序啟動時,會進行包掃描,掃描所有@FeignClient注解的類,並將這些信息注入Spring IoC容器中。當定義的Feign接口中的方法被調用時,通過JDK的代理方式,來生成具體的RequestTemplate。當生成代理時,Feign會為每個接口方法創建一個RequestTemplate對象,該對象封裝了HTTP請求需要的全部信息,如請求參數名、請求方法等信息都在這個過程中確定。
- 然后由RequestTemplate生成Request,然后把這個Request交給client處理,這里指的Client可以是JDK原生的URLConnection、Apache的Http Client,也可以是Okhttp。最后Client被封裝到LoadBalanceClient類,這個類結合Ribbon負載均衡發起服務之間的調用。
壓縮
服務端provider配置
# 服務端開啟壓縮
server.compression.enabled=true
調用方consumer配置
# 配置請求GZIP壓縮
feign.compression.request.enabled=true
# 配置響應GZIP壓縮
feign.compression.response.enabled=true
# 單位是B
feign.compression.request.min-request-size=100