当前位置:网站首页>[microservice openfeign] @feignclient detailed explanation
[microservice openfeign] @feignclient detailed explanation
2022-07-04 04:21:00 【Bulst】
List of articles
Last one , We use sentinel Integrate feign, Have a preliminary understanding of both , This article , More focused on feign Use and details of .
The steps of integration are roughly the same as the former , Let's take a look first @FeignClient.

value/name
value and name It does the same thing , If not configured url Then the configured value will be the service name , For service discovery .
On the contrary, it's just a name .
If you also specify url and name/value , With url Subject to , name/value Will be the name of the service ,url Address for service .
serviceId
serviceId It's abandoned , Use it directly name that will do .
contextId
If I have two feignclient All have the same name , By specifying different contextId To distinguish between , Otherwise, there are two at the same time bean It's a mistake .
Of course , You can also configure spring.main.allow-bean-definition-overriding=true, The function is to allow beanName Same BeanDefinition.
Let's take a look at the source code :
org.springframework.cloud.openfeign.FeignClientsRegistrar#getClientName
private String getClientName(Map<String, Object> client) {
if (client == null) {
return null;
}
String value = (String) client.get("contextId");
if (!StringUtils.hasText(value)) {
value = (String) client.get("value");
}
if (!StringUtils.hasText(value)) {
value = (String) client.get("name");
}
if (!StringUtils.hasText(value)) {
value = (String) client.get("serviceId");
}
if (StringUtils.hasText(value)) {
return value;
}
throw new IllegalStateException("Either 'name' or 'value' must be provided in @"
+ FeignClient.class.getSimpleName());
}
You can see that if it is configured contextId Will use contextId, If it's not configured, it will go value And then there was name And finally serviceId. No configuration by default , When a service has more than one Feign Client When it's time to report something wrong .
The second role is to register FeignClient in ,contextId Will act as Client Part of the alias , If the qualifier Give priority to qualifier As an alias .
private void registerFeignClient(BeanDefinitionRegistry registry,
AnnotationMetadata annotationMetadata, Map<String, Object> attributes) {
String className = annotationMetadata.getClassName();
BeanDefinitionBuilder definition = BeanDefinitionBuilder
.genericBeanDefinition(FeignClientFactoryBean.class);
validate(attributes);
definition.addPropertyValue("url", getUrl(attributes));
definition.addPropertyValue("path", getPath(attributes));
String name = getName(attributes);
definition.addPropertyValue("name", name);
String contextId = getContextId(attributes);
definition.addPropertyValue("contextId", contextId);
definition.addPropertyValue("type", className);
definition.addPropertyValue("decode404", attributes.get("decode404"));
definition.addPropertyValue("fallback", attributes.get("fallback"));
definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory"));
definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
String alias = contextId + "FeignClient";
AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
beanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, className);
// has a default, won't be null
boolean primary = (Boolean) attributes.get("primary");
beanDefinition.setPrimary(primary);
String qualifier = getQualifier(attributes);
if (StringUtils.hasText(qualifier)) {
alias = qualifier;
}
BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className,
new String[] {
alias });
BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
}
url
Used to configure the address of the specified service , It's equivalent to requesting the service directly , Service selection without load balancer .
decode404
When a call request occurs 404 When it's wrong ,decode404 The value of is true, Then it will execute decoder decode , Otherwise, throw an exception .
configuration
configuration It's configuration Feign Configuration class , In the configuration class, you can customize Feign Of Encoder、Decoder、LogLevel、Contract etc. .
fallback
Defines fault-tolerant handling classes , That is, fallback logic ,fallback Must implement Feign Client The interface of , It is impossible to know the abnormal information of fusing .
fallbackFactory
It's also fault-tolerant processing , We can know the abnormal information of the fuse .
path
path Define the current FeignClient Uniform prefixes for interfaces , For example, the interface address is /user/get, If you define the prefix as user, Then the path of the specific method only needs to write /get that will do , When we configure server.context-path,server.servlet-path When using .
primary
primary The corresponding is @Primary annotation , The default is true, There's also a reason for the official setting . When our Feign Realized fallback after , That means Feign Client There are several identical Bean stay Spring In the container , When we are using @Autowired When it comes to injection , I don't know which one to inject , So we need to set a high priority ,@Primary Annotation is what we do .
qualifier
qualifier The corresponding is @Qualifier annotation , General scene direct @Autowired Just inject it directly .
If our Feign Client Yes fallback Realization , Default @FeignClient Annotated primary=true, It means that we use @Autowired There is no problem with injection , Will give priority to your Feign Client.
If you are a ghost axe and a god sent one primary Set to false 了 , Direct use @Autowired The injection place will report an error , I don't know which object to inject .
The solution is obvious , You can take primary Set to true that will do , If for some special reason , You have to get rid of primary=true Set up , How do we inject in this case , We can configure a qualifier, And then use @Qualifier Annotations are injected .
边栏推荐
- [csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
- STM32 external DHT11 display temperature and humidity
- (指针)自己写一个比较字符串大小的函数,功能与strcmp类似。
- [webrtc] M98 Ninja build and compile instructions
- Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
- Pytest multi process / multi thread execution test case
- Rhcsa-- day one
- Understand the principle of bytecode enhancement technology through the jvm-sandbox source code
- 96% of the collected traffic is prevented by bubble mart of cloud hosting
- 透过JVM-SANDBOX源码,了解字节码增强技术原理
猜你喜欢

量子力学习题

指针数组和数组指针

软件测试是干什么的 发现缺陷错误,提高软件的质量
![Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure](/img/ba/c1d40de154344ccc9f2fd1dd4cb12f.png)
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure

Restore the subtlety of window position

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

My opinion on how to effectively telecommute | community essay solicitation

Katalon framework test web (XXVI) automatic email

Flink learning 8: data consistency

10 reasons for not choosing to use free virtual hosts
随机推荐
JS realizes the effect of text scrolling marquee
Flink learning 7: application structure
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
User defined path and file name of Baidu editor in laravel admin
Redis cluster uses Lua script. Lua script can also be used for different slots
图解网络:什么是热备份路由器协议HSRP?
Mitsubishi M70 macro variable reading Mitsubishi M80 public variable acquisition Mitsubishi CNC variable reading acquisition Mitsubishi CNC remote tool compensation Mitsubishi machine tool online tool
如何远程办公更有效率 | 社区征文
leetcode刷题:二叉树04(二叉树的层序遍历)
Why is the probability of pod increasing after IPtable
[webrtc] M98 Ninja build and compile instructions
Katalon使用script实现查询List大小
The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
Activiti7 task service - process variables (setvariable and setvariablelocal)
(pointer) write a function to compare the size of strings by yourself, which is similar to StrCmp.
RHCSA 08 - automount配置
[microservice openfeign] use openfeign to remotely call the file upload interface
(pointeur) Écrivez - vous une fonction qui compare la taille de la chaîne et fonctionne comme strcmp.
还原窗口位置的微妙之处
ctf-pikachu-XSS