springboot FeignClient注解及參數(shù)
一、FeignClient注解
FeignClient注解被@Target(ElementType.TYPE)修飾,表示FeignClient注解的作用目標(biāo)在接口上
@FeignClient(name = "github-client", url = "https://api.github.com", configuration = GitHubExampleConfig.class)
public interface GitHubClient {
@RequestMapping(value = "/search/repositories", method = RequestMethod.GET)
String searchRepo(@RequestParam("q") String queryStr);
}
聲明接口之后,在代碼中通過(guò)@Resource注入之后即可使用。@FeignClient標(biāo)簽的常用屬性如下:
- name:指定FeignClient的名稱(chēng),如果項(xiàng)目使用了Ribbon,name屬性會(huì)作為微服務(wù)的名稱(chēng),用于服務(wù)發(fā)現(xiàn)
- url: url一般用于調(diào)試,可以手動(dòng)指定@FeignClient調(diào)用的地址
- decode404:當(dāng)發(fā)生http 404錯(cuò)誤時(shí),如果該字段位true,會(huì)調(diào)用decoder進(jìn)行解碼,否則拋出FeignException
- configuration: Feign配置類(lèi),可以自定義Feign的Encoder、Decoder、LogLevel、Contract
- fallback: 定義容錯(cuò)的處理類(lèi),當(dāng)調(diào)用遠(yuǎn)程接口失敗或超時(shí)時(shí),會(huì)調(diào)用對(duì)應(yīng)接口的容錯(cuò)邏輯,fallback指定的類(lèi)必須實(shí)現(xiàn)@FeignClient標(biāo)記的接口
- fallbackFactory: 工廠類(lèi),用于生成fallback類(lèi)示例,通過(guò)這個(gè)屬性我們可以實(shí)現(xiàn)每個(gè)接口通用的容錯(cuò)邏輯,減少重復(fù)的代碼
- path: 定義當(dāng)前FeignClient的統(tǒng)一前綴
@FeignClient(name = "github-client",
url = "https://api.github.com",
configuration = GitHubExampleConfig.class,
fallback = GitHubClient.DefaultFallback.class)
public interface GitHubClient {
@RequestMapping(value = "/search/repositories", method = RequestMethod.GET)
String searchRepo(@RequestParam("q") String queryStr);
/**
* 容錯(cuò)處理類(lèi),當(dāng)調(diào)用失敗時(shí),簡(jiǎn)單返回空字符串
*/
@Component
public class DefaultFallback implements GitHubClient {
@Override
public String searchRepo(@RequestParam("q") String queryStr) {
return "";
}
}
}
在使用fallback屬性時(shí),需要使用@Component注解,保證fallback類(lèi)被Spring容器掃描到,GitHubExampleConfig內(nèi)容如下:
@Configuration
public class GitHubExampleConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
在使用FeignClient時(shí),Spring會(huì)按name創(chuàng)建不同的ApplicationContext,通過(guò)不同的Context來(lái)隔離FeignClient的配置信息,在使用配置類(lèi)時(shí),不能把配置類(lèi)放到Spring App Component scan的路徑下,否則,配置類(lèi)會(huì)對(duì)所有FeignClient生效.
二、Feign Client 和@RequestMapping
當(dāng)前工程中有和Feign Client中一樣的Endpoint時(shí),F(xiàn)eign Client的類(lèi)上不能用@RequestMapping注解否則,當(dāng)前工程該endpoint http請(qǐng)求且使用accpet時(shí)會(huì)報(bào)404
Controller:
@RestController
@RequestMapping("/v1/card")
public class IndexApi {
@PostMapping("balance")
@ResponseBody
public Info index() {
Info.Builder builder = new Info.Builder();
builder.withDetail("x", 2);
builder.withDetail("y", 2);
return builder.build();
}
}
Feign Client
@FeignClient(
name = "card",
url = "http://localhost:7913",
fallback = CardFeignClientFallback.class,
configuration = FeignClientConfiguration.class
)
@RequestMapping(value = "/v1/card")
public interface CardFeignClient {
@RequestMapping(value = "/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
Info info();
}
if @RequestMapping is used on class, when invoke http /v1/card/balance, like this :
如果 @RequestMapping注解被用在FeignClient類(lèi)上,當(dāng)像如下代碼請(qǐng)求/v1/card/balance時(shí),注意有Accept header:
Content-Type:application/json Accept:application/json POST http://localhost:7913/v1/card/balance
那么會(huì)返回 404。
如果不包含Accept header時(shí)請(qǐng)求,則是OK:
Content-Type:application/json POST http://localhost:7913/v1/card/balance
或者像下面不在Feign Client上使用@RequestMapping注解,請(qǐng)求也是ok,無(wú)論是否包含Accept:
@FeignClient(
name = "card",
url = "http://localhost:7913",
fallback = CardFeignClientFallback.class,
configuration = FeignClientConfiguration.class
)
public interface CardFeignClient {
@RequestMapping(value = "/v1/card/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
Info info();
}
三、Feign請(qǐng)求超時(shí)問(wèn)題
Hystrix默認(rèn)的超時(shí)時(shí)間是1秒,如果超過(guò)這個(gè)時(shí)間尚未響應(yīng),將會(huì)進(jìn)入fallback代碼。而首次請(qǐng)求往往會(huì)比較慢(因?yàn)镾pring的懶加載機(jī)制,要實(shí)例化一些類(lèi)),這個(gè)響應(yīng)時(shí)間可能就大于1秒了
解決方案有三種,以feign為例。
方法一
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
該配置是讓Hystrix的超時(shí)時(shí)間改為5秒
方法二
hystrix.command.default.execution.timeout.enabled: false
該配置,用于禁用Hystrix的超時(shí)時(shí)間
方法三
feign.hystrix.enabled: false
該配置,用于索性禁用feign的hystrix。該做法除非一些特殊場(chǎng)景,不推薦使用。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot @ConditionalOnMissingBean注解的作用詳解
- SpringBoot 注解事務(wù)聲明式事務(wù)的方式
- springboot2中使用@JsonFormat注解不生效的解決
- SpringBoot中@Pattern注解對(duì)時(shí)間格式校驗(yàn)方式
- SpringBoot中@ConfigurationProperties注解的使用與源碼詳解
- SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案
- 親測(cè)SpringBoot參數(shù)傳遞及@RequestBody注解---踩過(guò)的坑及解決
- 詳解SpringBoot 快速整合Mybatis(去XML化+注解進(jìn)階)
- SpringBoot單元測(cè)試中@SpyBean使用小結(jié)
相關(guān)文章
使用JPA雙向多對(duì)多關(guān)聯(lián)關(guān)系@ManyToMany
這篇文章主要介紹了使用JPA雙向多對(duì)多關(guān)聯(lián)關(guān)系@ManyToMany,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
基于SpringBoot服務(wù)端表單數(shù)據(jù)校驗(yàn)的實(shí)現(xiàn)方式
這篇文章主要介紹了基于SpringBoot服務(wù)端表單數(shù)據(jù)校驗(yàn)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
關(guān)于Arrays.sort()使用的注意事項(xiàng)
這篇文章主要介紹了關(guān)于Arrays.sort()使用的注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
解決Maven項(xiàng)目中 Invalid bound statement 無(wú)效的綁定問(wèn)題
這篇文章主要介紹了解決Maven項(xiàng)目中 Invalid bound statement 無(wú)效的綁定問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
IDEA無(wú)法創(chuàng)建JDK1.8版本的Springboot項(xiàng)目問(wèn)題解決(2種方法)
本文主要介紹了IDEA無(wú)法創(chuàng)建JDK1.8版本的Springboot項(xiàng)目問(wèn)題解決,包含兩種解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
SSH框架網(wǎng)上商城項(xiàng)目第7戰(zhàn)之整合Struts2和Json
SSH框架網(wǎng)上商城項(xiàng)目第7戰(zhàn)之整合Struts2和Json,打通EasyUI和Struts2之間的交互,感興趣的小伙伴們可以參考一下2016-05-05

