Spring Cloud使用Feign進行遠程調用的操作指南
一、Feign簡介
Feign官網(wǎng):https://github.com/OpenFeign/feign
Fegin是聲明式、模塊化的Http客戶端,可以幫助我們快捷優(yōu)雅的調用HTTP接口。在SpringCloud中可以很方便的創(chuàng)建一個Feign客戶端,只需聲明一個接口,并加上對應的注解就能完成對HTTP接口的調用。

二、RestTemplate方式調用存在問題
可讀性差,參數(shù)復雜難以維護
//2遠程查詢用戶信息 String url="http://provider-server/provider/"+order.getUserId(); //3. 發(fā)起調用 User user = restTemplate.getForObject(url, User.class);
三、Feign的使用步驟
在服務消費者端添加如下
3.1 引入依賴
<!-- 加入feign的依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3.2 添加@EnableFeignClients注解
在啟動類或者配置類添加
@EnableFeignClients
3.3 編寫FeignClient接口
@FeignClient("provider-server")
public interface ProviderServeClient {
/**
* 根據(jù)id查詢用戶信息
*/
@GetMapping("/provider/{id}")
User queryById(@PathVariable("id") Long id);
}
- 服務名稱:
provider-server - 請求路徑:
/provider/{id} - 請求方式:
GET - 請求參數(shù):
Long id - 返回值類型:
User
3.4 使用Feign客戶端代用RestTemplate
@Autowired
private ProviderServeClient providerServeClient;
/**
* 根據(jù)id查詢訂單并返回
*/
@Override
public Order queryOrderById(Long orderId) {
// 1.查詢訂單
Order order = orderMapper.findById(orderId);
//2遠程查詢用戶信息
// String url="http://provider-server/provider/"+order.getUserId();
//2. 發(fā)起調用
// User user = restTemplate.getForObject(url, User.class);
User user = providerServeClient.queryById(order.getUserId());
//3. 存入order
order.setUser(user);
// 4.返回
return order;
}
啟動調用成功

四、日志配置
4.1 日志級別介紹

1. NONE: 不記錄任何日志信息,默認值
2. BASIC: 僅記錄請求的方法,URL以及響應狀態(tài)碼和執(zhí)行時間
3. HEADERS: 在BASIC的基礎上,增加了請求和響應頭信息
4. FULL: 記錄所有請求和響應的明細,包括頭信息,請求體,元數(shù)據(jù)
4.2 配置日志
方式一: 配置文件方式
全局配置
feign:
client:
config:
default: # 全局的配置
loggerLevel: BASIC
局部配置
feign:
client:
config:
provider-server: # 寫服務名稱,則針對某個微服務的配置
loggerLevel: FULL
方式二: java代碼方式
public class FeignClientConfiguration {
public Logger.Level feignLogLevel(){
return Logger.Level.BASIC;
}
}
全局配置,添加到@EnableFeignClients注解中
@EnableFeignClients(defaultConfiguration = FeignClientConfiguration.class)
局部配置,添加到服務對應的@FeignClient注解中
@FeignClient(value = "provider-server",configuration = FeignClientConfiguration.class)
五、Feign的性能優(yōu)化
Feign底層的客戶端實現(xiàn):
- URLConnection :默認實現(xiàn),不支持連接池
- Apache HttpClient:支持連接池
- OKHttp : 支持鏈接池
- 使用連接池代替默認的URLConnection
- 日志級別最好使用
BASIC或者NONE
引入依賴
<!--httpClient的依賴 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
配置連接池
httpclient:
enabled: true # 開啟feign對HttpClient的支持
max-connections: 200 # 最大的連接數(shù)
max-connections-per-route: 30 # 每個路徑的最大連接數(shù)
六、Feign的實際應用
繼承方式,給消費者的FeginClient和提供者的controller定義統(tǒng)一的父接口(不推薦)
缺點:
耦合度高參數(shù)無法繼承
模塊抽-----取將FeignClient抽取為獨立模塊,并把所有接口有關的配置都放到這個模塊中,提供給所有消費者使用,這樣就不用每個消費者都定義自己的Feign客戶端,避免重復開發(fā)
抽取啟動可能會報錯如下:ProviderServeClient沒有找到

Description: Field providerServeClient in com.xing.service.impl.ConsumerServiceImpl required a bean of type 'com.api.clients.ProviderServeClient' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
原因:
- 啟動類所在包:
package com.xing - fegin模塊客戶端所在包為:
package com.api; - Spring Boot啟動默認掃描啟動類所在包及其所有子包,而fegin客戶端所在包沒有掃描到,所以報錯
ProviderServeClient沒有找到
解決方案:
指定FeignClient所在包
@EnableFeignClients(basePackages = "com.api")
指定FeignClient字節(jié)碼
@EnableFeignClients(clients = {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->ProviderServeClient.class})啟動成功

測試接口調用成功

以上就是Spring Cloud使用Feign進行遠程調用的操作指南的詳細內容,更多關于Spring Cloud Feign遠程調用的資料請關注腳本之家其它相關文章!
相關文章
SpringBoot+Redis+Lua防止IP重復防刷攻擊的方法
本文主要介紹了SpringBoot+Redis+Lua防止IP重復防刷攻擊的方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
IDEA快速搭建spring?boot項目教程(Spring?initializr)
這篇文章主要介紹了IDEA快速搭建spring?boot項目教程(Spring?initializr),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
JAVA基于靜態(tài)數(shù)組實現(xiàn)棧的基本原理與用法詳解
這篇文章主要介紹了JAVA基于靜態(tài)數(shù)組實現(xiàn)棧的基本原理與用法,結合實例形式詳細分析了JAVA基于靜態(tài)數(shù)組實現(xiàn)棧相關原理、用法與操作注意事項,需要的朋友可以參考下2020-03-03

