当前位置:网站首页>@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/
边栏推荐
- C language one-way linked list exercise
- RPC技术
- MySQL JDBC编程
- 仿《游戏鸟》源码 手游发号评测开服开测合集专区游戏下载网站模板
- [microservice openfeign] @feignclient detailed explanation
- The interactive solution of JS and app in the H5 page embedded in app (parameters can be transferred and callbacks can be made)
- How to view installed r packages in R language
- leetcode:1314. Matrix area and [2D prefix and template]
- Redis: hash type data operation command
- 更优雅地远程操作服务器:Paramiko库的实践
猜你喜欢
Instructions for LPC interrupt binding under ft2000+
Application scheme of Puyuan ds1000z series digital oscilloscope in communication principle experiment
资深开发人员告诉你,怎样编写出优秀的代码?
Boutique website navigation theme whole station source code WordPress template adaptive mobile terminal
Keysight n9320b RF spectrum analyzer solves tire pressure monitoring scheme
6-5漏洞利用-SSH弱口令破解利用
苹果CMS仿西瓜视频大气响应式视频模板源码
2020 Bioinformatics | TransformerCPI
Select function variable column name in dplyr of R language
旭化成首次参展第五届中国国际进口博览会(5th CIIE)
随机推荐
Niuke Xiaobai monthly race 49
C language one-way linked list exercise
什么是上下文?
Balloon punching and Boolean operation problems (extremely difficult)
西部数据绿盘、蓝盘、黑盘、红盘和紫盘有什么区别
Redis:有序集合zset类型数据操作命令
Ppt tutorial, how to save a presentation as a PDF file in PowerPoint?
2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x98 in position 1093: illegal multibyte sequence
[Yugong series] go teaching course 001 in July 2022 - Introduction to go language premise
How to view installed r packages in R language
Leetcode skimming: binary tree 04 (sequence traversal of binary tree)
How do good test / development programmers practice? Where to go
MIN_RTO 对话
EIG在智利推出可再生能源平台Grupo Cerro
Touch and take you to implement an EventEmitter
Rhcsa 04 - process management
Kivy教程之 07 组件和属性绑定实现按钮button点击修改label组件(教程含源码)
Leetcode skimming: binary tree 09 (minimum depth of binary tree)
R语言dplyr中的Select函数变量列名