SpringCloudAlibaba整合Feign實(shí)現(xiàn)遠(yuǎn)程HTTP調(diào)用的簡(jiǎn)單示例
前言
Feign
是Netflix
開源的聲明式HTTP
客戶端,致力于讓編寫http client
更加簡(jiǎn)單,Feign
可以通過(guò)聲明接口自動(dòng)構(gòu)造請(qǐng)求的目標(biāo)地址完成請(qǐng)求
環(huán)境
Spring Cloud Hoxton.SR9 + Spring Cloud Alibaba 2.2.6.RELEASE
Feign
是Netflix
公司產(chǎn)品,目前已停止更新,文章中使用的是OpenFeign
,是Spring
社區(qū)開發(fā)的組件
簡(jiǎn)單示例
content-center pom.xml
<!-- openfeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
啟動(dòng)類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 要請(qǐng)求的微服務(wù)的名稱 */ @FeignClient(name = "user-center") public interface TestFeignClient{ /** * test接口被調(diào)用時(shí),feign會(huì)構(gòu)造出 url * http://user-center/test/{name} 完成請(qǐng)求 * @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("請(qǐng)求..."); return "hello " + name; } }
示例測(cè)試結(jié)果
…至此,已完成Feign
的整合
Feign 的組成和支持的配置項(xiàng)
Feign 的組成
接口 | 作用 | 默認(rèn)值 |
---|---|---|
Feign.Builder | Feign的入口 | Feign.Builder |
Client | Feign底層請(qǐng)求方式 | 和Ribbon配合時(shí) LoadBalancerFeignClient 不和Ribbon配合時(shí) feign.Client.Default |
Contract | 契約,注解支持 | SpringMvcContract |
Encoder | 編碼器,用于將對(duì)象轉(zhuǎn)換成HTTP請(qǐng)求消息體 | SpringEncoder |
Decoder | 解碼器,將響應(yīng)消息轉(zhuǎn)換成對(duì)象 | ResponseEntityDecoder |
Logger | 日志管理器 | Slf4jLogger |
RequestInterceptor | 用于為每個(gè)請(qǐng)求添加通用邏輯 | 無(wú) |
Feign 支持的配置項(xiàng)
配置項(xiàng) | 作用 |
---|---|
Logger.Level | 指定日志級(jí)別 |
Retryer | 指定重試策略 |
ErrorDecoder | 指定錯(cuò)誤解碼器 |
Request.Options | 超時(shí)時(shí)間 |
Collection< RequestInterceptor> | 攔截器 |
SetterFactory | 用于設(shè)置Hystrix的配置屬性,整合Hystrix才會(huì)生效 |
配置屬性支持的配置項(xiàng)
feign.client.config: <feignName>: connectTimeout: 5000 # 連接超時(shí)時(shí)間 readTimeout: 5000 # 讀取超時(shí)時(shí)間 loggerLevel: full # 日志級(jí)別 errorDecoder: com.example.SimpleErrorDecoder # 錯(cuò)誤解碼器 retryer: com.example.SimpleRetryer # 重試策略 requestInterceptors: com.example.FooRequestInterceptor # 攔截器 decode404: false # 是否對(duì)404錯(cuò)誤碼解碼 encoder: com.example.SimpleEncoder # 編碼器 decoder: com.example.SimpleDecoder # 解碼器 contract: com.example.SimpleContract # 契約
Feign 的日志
Feign 的日志級(jí)別
feign
默認(rèn)不打印任何日志
級(jí)別 | 打印內(nèi)容 |
---|---|
NONE(默認(rèn)值) | 不記錄任何日志 |
BASIC | 僅記錄請(qǐng)求方法、URL、響應(yīng)狀態(tài)代碼以及執(zhí)行時(shí)間 |
HEADERS | BASIC級(jí)別的基礎(chǔ)上,記錄請(qǐng)求和響應(yīng)的header |
FULL | 記錄請(qǐng)求和響應(yīng)的header、body和元數(shù)據(jù) |
自定義配置 Feign 的日志級(jí)別
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 的日志級(jí)別是建立在接口日志級(jí)別基礎(chǔ)上的 com.coisini.contentcenter.feignclient.UserCenterFeignClient: debug
訪問(wèn)接口查看feign日志
yml 屬性配置方式
application.yml,實(shí)現(xiàn)效果同上
logging: level: com.coisini.contentcenter.feignclient.UserCenterFeignClient: debug # 自定義配置 feign 日志級(jí)別 feign: client: config: # 調(diào)用的微服務(wù)名稱 user-center: loggerLevel: full
全局配置 Feign 的日志級(jí)別
Java 代碼配置方式 GlobalFeignConfiguration.java
import feign.Logger; import org.springframework.context.annotation.Bean; /** * @Description Feign 全局配置類 */ public class GlobalFeignConfiguration { @Bean public Logger.Level level() { // feign 日志級(jí)別 FULL return Logger.Level.FULL; } }
啟動(dòng)類ContentCenterApplication.java
@EnableFeignClients(defaultConfiguration = GlobalFeignConfiguration.class) @SpringBootApplication public class ContentCenterApplication { ... }
application.yml
logging: level: com.coisini.contentcenter.feignclient.UserCenterFeignClient: debug
接口日志打印
yml 屬性配置方式 application.yml
# 自定義配置 feign 日志級(jí)別 feign: client: config: # 全局配置 default: loggerLevel: full
實(shí)現(xiàn)效果同上
Feign 日志級(jí)別配置方式總結(jié)
- 配置方式優(yōu)先級(jí):全局代碼配置 < 全局屬性配置 < 自定義代碼配置(細(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloudAlibaba微服務(wù)調(diào)用組件OpenFeign的方法
- Alibaba?SpringCloud集成Nacos、openFeign實(shí)現(xiàn)負(fù)載均衡的解決方案
- 淺談SpringCloud?Alibaba和SpringCloud的區(qū)別
- 聊聊SpringCloud和SpringCloudAlibaba的區(qū)別
- SpringCloudAlibaba分布式組件詳解
- SpringCloud Alibaba項(xiàng)目實(shí)戰(zhàn)之nacos-server服務(wù)搭建過(guò)程
- SpringCloud Alibaba 基本開發(fā)框架搭建過(guò)程
- Springcloud Alibaba超詳細(xì)使用詳解
相關(guān)文章
Java使用synchronized修飾方法來(lái)同步線程的實(shí)例演示
synchronized下的方法控制多線程程序中的線程同步非常方便,這里就來(lái)看一下Java使用synchronized修飾方法來(lái)同步線程的實(shí)例演示,需要的朋友可以參考下2016-06-06Feign如何解決服務(wù)之間調(diào)用傳遞token
這篇文章主要介紹了Feign如何解決服務(wù)之間調(diào)用傳遞token,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java發(fā)送http請(qǐng)求時(shí)如何處理異步回調(diào)結(jié)果
這篇文章主要介紹了java發(fā)送http請(qǐng)求時(shí)如何處理異步回調(diào)結(jié)果問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06Spring Cloud Hystrix實(shí)現(xiàn)服務(wù)容錯(cuò)的方法
Hystrix是SpringCloud中重要的熔斷保護(hù)組件,由Netflix開源,主要提供延遲和容錯(cuò)管理,以保障分布式系統(tǒng)的高可用性和魯棒性,通過(guò)封裝依賴項(xiàng)實(shí)現(xiàn)服務(wù)間隔離,引入回退邏輯應(yīng)對(duì)依賴服務(wù)故障,有效防止系統(tǒng)崩潰和服務(wù)級(jí)聯(lián)故障2024-10-10