前言
Feign
是Netflix
開源的聲明式HTTP
客戶端,致力于讓編寫http client
更加簡單,Feign
可以通過聲明接口自動構(gòu)造請求的目標(biāo)地址完成請求
環(huán)境
Spring Cloud Hoxton.SR9 + Spring Cloud Alibaba 2.2.6.RELEASE
Feign
是Netflix
公司產(chǎn)品,目前已停止更新,文章中使用的是OpenFeign
,是Spring
社區(qū)開發(fā)的組件
簡單示例
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 要請求的微服務(wù)的名稱 */ @FeignClient(name = "user-center") public interface TestFeignClient{ /** * test接口被調(diào)用時(shí),feign會構(gòu)造出 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; } }
示例測試結(jié)果
…至此,已完成Feign
的整合
Feign 的組成和支持的配置項(xiàng)
Feign 的組成
接口 | 作用 | 默認(rèn)值 |
---|---|---|
Feign.Builder | Feign的入口 | Feign.Builder |
Client | Feign底層請求方式 |
和Ribbon配合時(shí) LoadBalancerFeignClient 不和Ribbon配合時(shí) feign.Client.Default |
Contract | 契約,注解支持 | SpringMvcContract |
Encoder | 編碼器,用于將對象轉(zhuǎn)換成HTTP請求消息體 | SpringEncoder |
Decoder | 解碼器,將響應(yīng)消息轉(zhuǎn)換成對象 | ResponseEntityDecoder |
Logger | 日志管理器 | Slf4jLogger |
RequestInterceptor | 用于為每個(gè)請求添加通用邏輯 | 無 |
Feign 支持的配置項(xiàng)
配置項(xiàng) | 作用 |
---|---|
Logger.Level | 指定日志級別 |
Retryer | 指定重試策略 |
ErrorDecoder | 指定錯(cuò)誤解碼器 |
Request.Options | 超時(shí)時(shí)間 |
Collection< RequestInterceptor> | 攔截器 |
SetterFactory | 用于設(shè)置Hystrix的配置屬性,整合Hystrix才會生效 |
配置屬性支持的配置項(xiàng)
feign.client.config: <feignName>: connectTimeout: 5000 # 連接超時(shí)時(shí)間 readTimeout: 5000 # 讀取超時(shí)時(shí)間 loggerLevel: full # 日志級別 errorDecoder: com.example.SimpleErrorDecoder # 錯(cuò)誤解碼器 retryer: com.example.SimpleRetryer # 重試策略 requestInterceptors: com.example.FooRequestInterceptor # 攔截器 decode404: false # 是否對404錯(cuò)誤碼解碼 encoder: com.example.SimpleEncoder # 編碼器 decoder: com.example.SimpleDecoder # 解碼器 contract: com.example.SimpleContract # 契約
Feign 的日志
Feign 的日志級別
feign
默認(rèn)不打印任何日志
級別 | 打印內(nèi)容 |
---|---|
NONE(默認(rèn)值) | 不記錄任何日志 |
BASIC | 僅記錄請求方法、URL、響應(yīng)狀態(tài)代碼以及執(zhí)行時(shí)間 |
HEADERS | BASIC級別的基礎(chǔ)上,記錄請求和響應(yīng)的header |
FULL | 記錄請求和響應(yīng)的header、body和元數(shù)據(jù) |
自定義配置 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 的日志級別是建立在接口日志級別基礎(chǔ)上的 com.coisini.contentcenter.feignclient.UserCenterFeignClient: debug
訪問接口查看feign日志
yml 屬性配置方式
application.yml,實(shí)現(xiàn)效果同上
logging: level: com.coisini.contentcenter.feignclient.UserCenterFeignClient: debug # 自定義配置 feign 日志級別 feign: client: config: # 調(diào)用的微服務(wù)名稱 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
實(shí)現(xiàn)效果同上
Feign 日志級別配置方式總結(jié)
- 配置方式優(yōu)先級:全局代碼配置 < 全局屬性配置 < 自定義代碼配置(細(xì)粒度) < 自定義屬性配置(細(xì)粒度)
- 建議盡量使用屬性配置
項(xiàng)目源碼
GitHub: https://github.com/Maggieq8324/coisini-cloud-alibaba
Gitee: https://gitee.com/maggieq8324/coisini-cloud-alibaba
到此這篇關(guān)于SpringCloudAlibaba 整合 Feign 實(shí)現(xiàn)遠(yuǎn)程 HTTP 調(diào)用的文章就介紹到這了,更多相關(guān)SpringCloudAlibaba遠(yuǎn)程 HTTP 調(diào)用內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/weixin_41182727/article/details/120434890