当前位置:网站首页>@Feignclient comments and parameters
@Feignclient comments and parameters
2022-07-04 04:38:00 【Zhilan was born in a deep valley】
@FeignClient Notes and parameters
One 、FeignClient annotation
FeignClient The annotation is @Target(ElementType.TYPE) modification , Express FeignClient The purpose of annotation is on the interface
@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);
}
After declaring the interface , In the code through @Resource It can be used after injection [email protected] The common attributes of tags are as follows :
name: Appoint FeignClient The name of , If the project is used Ribbon,name The property will be used as the name of the microservice , For service discovery
url: url Generally used for debugging , Can be specified manually @FeignClient The address of the call
decode404: Happen when http 404 When it's wrong , If the word segment true, Would call decoder decode , Otherwise throw FeignException
configuration: Feign Configuration class , You can customize Feign Of Encoder、Decoder、LogLevel、Contract
fallback: Defines fault-tolerant handling classes , When the call to the remote interface fails or times out , The fault-tolerant logic of the corresponding interface is invoked ,fallback The specified class must be implemented @FeignClient Tagged interface
fallbackFactory: Factory , Used to generate fallback Class example , With this property we can implement fault tolerance logic common to each interface , Reduce duplicate code
path: Define the current FeignClient Unified prefix of
@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);
/** * Fault tolerant processing class , When the call fails , Simply return an empty string */
@Component
public class DefaultFallback implements GitHubClient {
@Override
public String searchRepo(@RequestParam("q") String queryStr) {
return "";
}
}
}
// In the use of fallback Attribute , Need to use @Component annotation , Guarantee fallback Class quilt Spring The container scans to ,GitHubExampleConfig The contents are as follows :
@Configuration
public class GitHubExampleConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
In the use of FeignClient when ,Spring Will press name Create different ApplicationContext, Through different Context To isolate FeignClient Configuration information , When using configuration classes , You can't put configuration classes in Spring App Component scan In the path of , otherwise , The configuration class will be for all FeignClient take effect .
Two 、Feign Client and @RequestMapping
There are and in the current project Feign Client In the same way Endpoint when ,Feign Client You can't use @RequestMapping Note otherwise , The current project should endpoint http Request and use accpet It will be reported in time 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 Annotations are used in FeignClient Class , When code requests like /v1/card/balance when , Pay attention to it Accept header:
Content-Type:application/json
Accept:application/json
POST http://localhost:7913/v1/card/balance
So it will return 404.
If not Accept header Ask for , It is OK:
Content-Type:application/json
POST http://localhost:7913/v1/card/balance
Or like it's not down here Feign Client Upper use @RequestMapping annotation , So is the request ok, Whether or not it contains 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();
}
3、 ... and 、Feign Request timeout problem
Hystrix The default timeout is 1 second , If there is no response beyond this time , Will enter fallback Code . The first request is often slow ( because Spring The lazy loading mechanism of , To instantiate some classes ), This response time may be greater than 1 The second
There are three solutions , With feign For example .
Method 1
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
The configuration is to let Hystrix The timeout for is changed to 5 second
Method 2
hystrix.command.default.execution.timeout.enabled: false
This configuration , Used to disable Hystrix Timeout for
Method 3
feign.hystrix.enabled: false
This configuration , It is used to disable feign Of hystrix. It's not possible to do this unless there are some special situations , It is not recommended to use .
See :http://www.itmuch.com/spring-cloud-sum-feign/
边栏推荐
- Instructions for LPC interrupt binding under ft2000+
- The five pictures tell you: why is there such a big gap between people in the workplace?
- 一个漂亮的API文档生成工具
- Virtual commodity account trading platform source code_ Support personal QR code collection
- 陪驾注意事项 这23点要注意!
- Balloon punching and Boolean operation problems (extremely difficult)
- EventBridge 在 SaaS 企业集成领域的探索与实践
- Apple CMS imitation watermelon video atmospheric response video template source code
- 浅谈JVM的那些事
- [wechat applet] good looking carousel map component
猜你喜欢

Modstartblog modern personal blog system v5.2.0 source code download

【微信小程序】好看的轮播图组件

【云原生】那些看起来很牛X,原理却很简单的一行代码

Select function variable column name in dplyr of R language

Emlog用户注册插件 价值80元

Intersection traffic priority, illustration of intersection traffic rules

GUI 应用:socket 网络聊天室

Many founders of technology companies provided enterpriser first with a round C financing of up to US $158million to help it invest in the next generation of global innovators

2021 RSC | Drug–target affinity prediction using graph neural network and contact maps

MySQL indexes and transactions
随机推荐
两万字带你掌握多线程
I.MX6U-ALPHA开发板(模仿STM32驱动开发实验)
浅谈JVM的那些事
Kivy教程之 自定义字体(教程含源码)
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
架构训练毕业设计+总结
Leetcode skimming: binary tree 04 (sequence traversal of binary tree)
苹果CMS仿西瓜视频大气响应式视频模板源码
Kivy tutorial custom fonts (tutorial with source code)
浅谈一篇优质的小红书文案需要具备什么
R语言中如何查看已安装的R包
【安全攻防】序列化与反序列,你了解多少?
Talking about what a high-quality little red book copy needs to have
C language one-way linked list exercise
Kivy教程之 格式化文本 (教程含源码)
[Yugong series] go teaching course 001 in July 2022 - Introduction to go language premise
LeetCode136+128+152+148
Select function variable column name in dplyr of R language
博朗与Virgil Abloh于2021年为纪念博朗品牌100周年而联合打造的“功能性艺术”将在博物馆展出Abloh作品期间首次亮相
Main applications of TDK lambda power supply