前言
Feign
是Netflix
開源的聲明式HTTP
客戶端,致力于讓編寫http client
更加簡單,Feign
可以通過聲明接口自動構造請求的目標地址完成請求
環境
Spring Cloud Hoxton.SR9 + Spring Cloud Alibaba 2.2.6.RELEASE
Feign
是Netflix
公司產品,目前已停止更新,文章中使用的是OpenFeign
,是Spring
社區開發的組件
簡單示例
content-center pom.xml
<!-- openfeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
啟動類ContentCenterApplication.java
@EnableFeignClients public class ContentCenterApplication { }
TestController.java
import com.coisini.contentcenter.feignclient.TestFeignClient; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class TestController { private final TestFeignClient testFeignClient; /** * 整合Feign * @return */ @GetMapping("test4") public String test4() { return testFeignClient.test("Coisini"); } }
TestFeignClient.java
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** * @FeignClient(name = "user-center") * name 要請求的微服務的名稱 */ @FeignClient(name = "user-center") public interface TestFeignClient{ /** * test接口被調用時,feign會構造出 url * http://user-center/test/{name} 完成請求 * @param name * @return */ @GetMapping("/test/{name}") String test(@PathVariable String name); }
user-center TestController.java
@RestController @Slf4j public class TestController { @GetMapping("/test/{name}") public String test(@PathVariable String name) { log.info("請求..."); return "hello " + name; } }
示例測試結果
…至此,已完成Feign
的整合
Feign 的組成和支持的配置項
Feign 的組成
接口 | 作用 | 默認值 |
---|---|---|
Feign.Builder | Feign的入口 | Feign.Builder |
Client | Feign底層請求方式 |
和Ribbon配合時 LoadBalancerFeignClient 不和Ribbon配合時 feign.Client.Default |
Contract | 契約,注解支持 | SpringMvcContract |
Encoder | 編碼器,用于將對象轉換成HTTP請求消息體 | SpringEncoder |
Decoder | 解碼器,將響應消息轉換成對象 | ResponseEntityDecoder |
Logger | 日志管理器 | Slf4jLogger |
RequestInterceptor | 用于為每個請求添加通用邏輯 | 無 |
Feign 支持的配置項
配置項 | 作用 |
---|---|
Logger.Level | 指定日志級別 |
Retryer | 指定重試策略 |
ErrorDecoder | 指定錯誤解碼器 |
Request.Options | 超時時間 |
Collection< RequestInterceptor> | 攔截器 |
SetterFactory | 用于設置Hystrix的配置屬性,整合Hystrix才會生效 |
配置屬性支持的配置項
feign.client.config: <feignName>: connectTimeout: 5000 # 連接超時時間 readTimeout: 5000 # 讀取超時時間 loggerLevel: full # 日志級別 errorDecoder: com.example.SimpleErrorDecoder # 錯誤解碼器 retryer: com.example.SimpleRetryer # 重試策略 requestInterceptors: com.example.FooRequestInterceptor # 攔截器 decode404: false # 是否對404錯誤碼解碼 encoder: com.example.SimpleEncoder # 編碼器 decoder: com.example.SimpleDecoder # 解碼器 contract: com.example.SimpleContract # 契約
Feign 的日志
Feign 的日志級別
feign
默認不打印任何日志
級別 | 打印內容 |
---|---|
NONE(默認值) | 不記錄任何日志 |
BASIC | 僅記錄請求方法、URL、響應狀態代碼以及執行時間 |
HEADERS | BASIC級別的基礎上,記錄請求和響應的header |
FULL | 記錄請求和響應的header、body和元數據 |
自定義配置 Feign 的日志級別
Java 代碼配置方式 UserCenterFeignConfiguration.java
import feign.Logger; import org.springframework.context.annotation.Bean; /** * @Description 用戶中心 Feign 配置類 */ public class UserCenterFeignConfiguration { @Bean public Logger.Level level() { return Logger.Level.FULL; } }
UserCenterFeignClient.java
@FeignClient(name = "user-center", configuration = UserCenterFeignConfiguration.class) public interface UserCenterFeignClient { ... }
application.yml
logging: level: # feign 的日志級別是建立在接口日志級別基礎上的 com.coisini.contentcenter.feignclient.UserCenterFeignClient: debug
訪問接口查看feign日志
yml 屬性配置方式
application.yml,實現效果同上
logging: level: com.coisini.contentcenter.feignclient.UserCenterFeignClient: debug # 自定義配置 feign 日志級別 feign: client: config: # 調用的微服務名稱 user-center: loggerLevel: full
全局配置 Feign 的日志級別
Java 代碼配置方式 GlobalFeignConfiguration.java
import feign.Logger; import org.springframework.context.annotation.Bean; /** * @Description Feign 全局配置類 */ public class GlobalFeignConfiguration { @Bean public Logger.Level level() { // feign 日志級別 FULL return Logger.Level.FULL; } }
啟動類ContentCenterApplication.java
@EnableFeignClients(defaultConfiguration = GlobalFeignConfiguration.class) @SpringBootApplication public class ContentCenterApplication { ... }
application.yml
logging: level: com.coisini.contentcenter.feignclient.UserCenterFeignClient: debug
接口日志打印
yml 屬性配置方式 application.yml
# 自定義配置 feign 日志級別 feign: client: config: # 全局配置 default: loggerLevel: full
實現效果同上
Feign 日志級別配置方式總結
- 配置方式優先級:全局代碼配置 < 全局屬性配置 < 自定義代碼配置(細粒度) < 自定義屬性配置(細粒度)
- 建議盡量使用屬性配置
項目源碼
GitHub: https://github.com/Maggieq8324/coisini-cloud-alibaba
Gitee: https://gitee.com/maggieq8324/coisini-cloud-alibaba
到此這篇關于SpringCloudAlibaba 整合 Feign 實現遠程 HTTP 調用的文章就介紹到這了,更多相關SpringCloudAlibaba遠程 HTTP 調用內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_41182727/article/details/120434890