当前位置:网站首页>@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/
边栏推荐
- Emlog用户注册插件 价值80元
- 微信公众号无限回调授权系统源码
- Select function variable column name in dplyr of R language
- Leetcode skimming: binary tree 04 (sequence traversal of binary tree)
- Statistical genetics: Chapter 3, population genetics
- Unity Resource path
- 陪驾注意事项 这23点要注意!
- Architecture training graduation design + summary
- [wechat applet] good looking carousel map component
- tdk-lambda电源主要应用
猜你喜欢

How do good test / development programmers practice? Where to go

Leetcode skimming: binary tree 09 (minimum depth of binary tree)

两万字带你掌握多线程

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

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

6-4漏洞利用-SSH Banner信息获取

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

Architecture training graduation design + summary

The "functional art" jointly created by Bolang and Virgil abloh in 2021 to commemorate the 100th anniversary of Bolang brand will debut during the exhibition of abloh's works in the museum

Distributed cap theory
随机推荐
戳气球和布尔运算问题(巨难)
Kivy教程之 更改背景颜色(教程含源码)
两万字带你掌握多线程
What is the difference between Western Digital Green disk, blue disk, black disk, red disk and purple disk
[security attack and Defense] how much do you know about serialization and deserialization?
2020 Bioinformatics | TransformerCPI
20000 words will take you to master multithreading
架构训练毕业设计+总结
[cloud native] those lines of code that look awesome but have a very simple principle
5张图告诉你:同样是职场人,差距怎么这么大?
Intersection traffic priority, illustration of intersection traffic rules
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Asahi Kasei participated in the 5th China International Import Expo (5th ciie) for the first time
Pytest基础自学系列(一)
Precautions for accompanying driving these 23 points should be paid attention to!
MySQL 索引和事务
牛客小白月赛49
更优雅地远程操作服务器:Paramiko库的实践
Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
Dry goods | detailed explanation of webshell Foundation