广告
返回顶部
首页 > 资讯 > 后端开发 > Python >@FeignClient注解中属性contextId的使用说明
  • 695
分享到

@FeignClient注解中属性contextId的使用说明

2024-04-02 19:04:59 695人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录一、概述二、解决方案2.1 方案12.2 方案2三、源代码分析相关代码1相关代码2一、概述 如果我们使用Feign定义了两个接口,但是目标服务是同一个,那么在SpringBoot

一、概述

如果我们使用Feign定义了两个接口,但是目标服务是同一个,那么在SpringBoot启动时就会遇到一个问题:

Description:
The bean 'xxxxxxxx.FeignClientSpecification', defined in null, could not be reGIStered. A bean with that name has already been defined in null and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

二、解决方案

2.1 方案1

修改yml配置:spring.main.allow-bean-definition-overriding=true

spring:
  main:
    allow-bean-definition-overriding: true

2.2 方案2

在每个Feign的接口中,在注解上加 contextId属性

contextId在Feign Client的作用是在注册Feign Client Configuration的时候需要一个名称,名称是通过getClientName方法获取的

@FeignClient(name = "sale-service",contextId= "saleservice1")
 
public interface saleClient{
 
    @RequestMapping(value = "/sale/add", method = RequestMethod.GET)
 
    String add(@RequestParam("saleNum") String queryStr);
 
}

备注:contextId= "名称" 中的名称,不能用“_”会报错,可以用“-”

三、源代码分析

  • 包名:spring-cloud-openfeign-core-2.2.5.RELEASE.jar
  • 类路径:org.springframework.cloud.openfeign.FeignClientsRegistrar

相关代码1

 private void registerFeignClient(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, Map<String, Object> attributes) {
        String className = annotationMetadata.getClassName();
        BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(FeignClientFactoryBean.class);
        this.validate(attributes);
        definition.addPropertyValue("url", this.getUrl(attributes));
        definition.addPropertyValue("path", this.getPath(attributes));
        String name = this.getName(attributes);
        definition.addPropertyValue("name", name);
        String contextId = this.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(2);
        String alias = contextId + "FeignClient";
        AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
        beanDefinition.setAttribute("factoryBeanObjectType", className);
        boolean primary = (Boolean)attributes.get("primary");
        beanDefinition.setPrimary(primary);
        String qualifier = this.getQualifier(attributes);
        if (StringUtils.hasText(qualifier)) {
            alias = qualifier;
        }
 
        BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[]{alias});
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
    }

代码截图:

相关代码2

可以看到, name应该是从注解中的属性取值来的, 再看看getClientName()方法.

 private String getClientName(Map<String, Object> client) {
        if (client == null) {
            return null;
        } else {
            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;
            } else {
                throw new IllegalStateException("Either 'name' or 'value' must be provided in @" + FeignClient.class.getSimpleName());
            }
        }
    }

代码截图:

一目了然了, 我们声明@FeignClient注解时, 只使用了value属性, 所以产生了冲突, 只要加上contextId就好了.

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: @FeignClient注解中属性contextId的使用说明

本文链接: https://www.lsjlt.com/news/151890.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作