[apache/dubbo]使用注解方式在多注册中心的问题

2024-04-11 481 views
8

使用注解方式在多注册中心中需要有两个RegistryConfig,但是会提示required a single bean, but 2 were found...Action: Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed 使用了@Primary注解,也不起作用

回答

1

请尝试指定为registry指定id,service中配置registry="id1,id2"

6

能否把详细的堆栈贴一下,便于我们排查问题

2

9785 [main] WARN o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addressController': Unsatisfied dependency expressed through field 'addressService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addressServiceImpl': Injection of @Reference dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.alibaba.dubbo.config.RegistryConfig' available: expected single matching bean but found 2: lobRegistry,mallRegistry 9789 [main] INFO c.a.d.c.s.b.f.a.ReferenceAnnotationBeanPostProcessor - class com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor was destroying! 9795 [main] INFO o.a.catalina.core.StandardService - Stopping service Tomcat 9829 [localhost-startStop-1] WARN o.a.c.loader.WebappClassLoaderBase - The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java.lang.Object.wait(Native Method) java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43) 9848 [main] WARN o.s.boot.SpringApplication - Error handling failed (Error creating bean with name 'com.alibaba.dubbo.config.spring.ServiceBean#0': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available) 9865 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter -


APPLICATION FAILED TO START


Description:

Field addressService in com.secoo.distribute.business.controller.AddressController required a single bean, but 2 were found:

  • lobRegistry: defined by method 'getLobRegistry' in class path resource [com/secoo/distribute/util/dubbo/DubboRegistry.class]
  • mallRegistry: defined by method 'getMallRegistry' in class path resource [com/secoo/distribute/util/dubbo/DubboRegistry.class]

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

9872 [DubboShutdownHook] INFO c.a.dubbo.config.AbstractConfig - [DUBBO] Run shutdown hook now., dubbo version: 2.5.7, current host: 127.0.0.1 9876 [DubboShutdownHook] INFO c.a.d.r.s.AbstractRegistryFactory - [DUBBO] Close all registries [], dubbo version: 2.5.7, current host: 127.0.0.1


这部分是Registry的代码 @Configuration public class DubboRegistry {

@ConfigurationProperties(prefix = "dubbo.registry.lob")
@Bean(name = "lobRegistry")
@Primary
public RegistryConfig getLobRegistry() {
    return new RegistryConfig();
}

@ConfigurationProperties(prefix = "dubbo.registry.mall")
@Bean(name = "mallRegistry")
public RegistryConfig getMallRegistry() {
    return new RegistryConfig();
}

}

1

在你的AddressController 中增加 @Qualifier 指定某个 Bean ID

5

@com.alibaba.dubbo.config.annotation.Reference(registry = "lobRegistry") private UserAddressExportService userAddressExportService; 这里是调用接口的地方。@Qualifier 应该放到哪里呢?

9

@BrightMi
你是写了类似这样的代码吗?如果是的话那是你Spring使用的问题,要明确指定一下

@Autowired
private RegistryConfig registryConfig;
6

我们判断基本是以上spring的使用问题,我先cose掉了