当前位置:网站首页>[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 .
边栏推荐
- Epidemic strikes -- Thinking about telecommuting | community essay solicitation
- Unity draws the trajectory of pinball and billiards
- Flink学习7:应用程序结构
- STM32 external DHT11 display temperature and humidity
- Getting started with the go language is simple: go implements the Caesar password
- Flink学习8:数据的一致性
- Smart subway | cloud computing injects wisdom into urban subway transportation
- Global exposure and roller shutter exposure of industrial cameras
- 01 QEMU starts the compiled image vfs: unable to mount root FS on unknown block (0,0)
- Activiti7 task service - process variables (setvariable and setvariablelocal)
猜你喜欢
PostgreSQL users cannot create table configurations by themselves
The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
图解网络:什么是热备份路由器协议HSRP?
软件测试是干什么的 发现缺陷错误,提高软件的质量
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
Brief explanation of depth first search (with basic questions)
Idea configuration 360zip open by default -- external tools
随机推荐
【罗技】m720
如何远程办公更有效率 | 社区征文
一位毕业生的自我分享
I Build a simple microservice project
2020 Bioinformatics | TransformerCPI
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
Myslq delete followed by limit
JDBC 进阶
Introduction to asynchronous task capability of function calculation - task trigger de duplication
还原窗口位置的微妙之处
Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
Go 语言入门很简单:Go 实现凯撒密码
RHCSA 07 - 用户与群组管理
ROS2中CMake编译选项的设置
程序员远程办公喜忧参半| 社区征文
Unity draws the trajectory of pinball and billiards
Graduation summary
10 reasons for not choosing to use free virtual hosts
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
96% of the collected traffic is prevented by bubble mart of cloud hosting