代码之家  ›  专栏  ›  技术社区  ›  Stéphane GRILLON

将springboot1.x中的netflix-feign迁移到springboot2.x中的openfeign

  •  1
  • Stéphane GRILLON  · 技术社区  · 6 年前

    我尝试将springboot1.x.y(布鲁塞尔SR12)迁移到2.x.y。我用 FeignClients

    我更改Maven配置:

    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    

    所以

    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-openfeign</artifactId>
    <version>2.0.0.RELEASE</version>
    

    import org.springframework.cloud.netflix.feign.EnableFeignClients;
    
    import org.springframework.cloud.netflix.feign.FeignClient;
    

    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    import org.springframework.cloud.openfeign.FeignClient;
    

    我使用这个界面:

    @FeignClient(value = "COMPANY", fallbackFactory = CompanyClientFallbackFactory.class, configuration = FeignConfiguration.class)
    public interface CompanyClient extends CompanyApi {
    }
    

    当我运行JUnit测试(使用spring上下文)时,我现在有这个错误(不在Springboot 1.x.y和旧的netflix包中):

    The bean 'COMPANY.FeignClientSpecification', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.
    

    完整跟踪:

    java.lang.IllegalStateException: Failed to load ApplicationContext
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
        at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
        at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.postProcessFields(MockitoTestExecutionListener.java:99)
        at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.injectFields(MockitoTestExecutionListener.java:79)
        at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.prepareTestInstance(MockitoTestExecutionListener.java:54)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    ...
    Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'COMPANY.FeignClientSpecification' defined in null: Cannot register bean definition [Generic bean: class [org.springframework.cloud.openfeign.FeignClientSpecification]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean 'COMPANY.FeignClientSpecification': There is already [Generic bean: class [org.springframework.cloud.openfeign.FeignClientSpecification]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:896)
        at org.springframework.cloud.openfeign.FeignClientsRegistrar.registerClientConfiguration(FeignClientsRegistrar.java:355)
        at org.springframework.cloud.openfeign.FeignClientsRegistrar.registerFeignClients(FeignClientsRegistrar.java:155)
        at org.springframework.cloud.openfeign.FeignClientsRegistrar.registerBeanDefinitions(FeignClientsRegistrar.java:83)
    
    3 回复  |  直到 6 年前
        1
  •  6
  •   Alessandro Dionisi    6 年前

    可能你有多个 @FeignClient 具有相同名称属性的定义。

        2
  •  18
  •   Ilya Serbis    4 年前

    这就是我们的解决方案 Feign documentation :

    如果我们要创建多个具有相同名称或url的外部客户端 contextId 的属性 @FeignClient 豆。

    @FeignClient(contextId = "fooClient", name = "stores", configuration = FooConfiguration.class)
    public interface FooClient {
        //..
    }
    
    @FeignClient(contextId = "barClient", name = "stores", configuration = BarConfiguration.class)
    public interface BarClient {
        //..
    }
    

    https://github.com/spring-cloud/spring-cloud-openfeign/pull/90/commits/82fa5181fdd2e23e7414521f468ecea88e17d157

        3
  •  8
  •   James Gawron    5 年前

    如果意外地使用@EnableFeignClients注释了多个@Configuration类,也会发生这种情况

        4
  •  0
  •   Stéphane GRILLON    5 年前

    变通办法 @Alessandro Dionisi response

    应用程序.yml:

    spring:
      main:
        allow-bean-definition-overriding: true
    
        5
  •  0
  •   Akhil    4 年前

    mvn clean package为我修复了这个错误

        6
  •  -3
  •   CmdSmith    5 年前