feign的启动异常:The bean 'XXX.FeignClientSpecification', defined in null, could not be registered

feign的启动异常:The bean 'XXX.FeignClientSpecification', defined in null, could not be registerer

Description:

The bean 'produce-core.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

于是按照提示,设置为true,虽然可以启动,但是继续追根溯源。在项目的服务中对外提供了两个feign接口,当出现了多个服务名称的时候 就会报错。在启动类上可以看到:@EnableFeignClients这个注解。

 

可以看到FeignClientsRegistrar这个类,进去之后,可看到 方法

public void registerFeignClients(AnnotationMetadata metadata, BeanDefinitionRegistry registry)

而后可以看到

 

 

此代码可知:在注册配置client的时候,传入name,获取name的时候通过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());
    }

这个时候的name为feignClient的name,或者value,或者contextId
通过注释: This will be used as the bean name instead of name if present, but will not be used as a service id.也就是作为bean的名称会代替name(也是value,因为name和value为别名作用)
这个name在方法中和类名的简单名称进行拼接,注入到Ioc的是相同Bean名。
因为我们的value为微服务的名称。

所以,综上可知,只需要将contextId进行别名设计即可。
@FeignClient(value = “produce-core”, contextId = “userClient”)
@FeignClient(value = “produce-core”, contextId = “personClient”)
这个时候即便是不配置 allow-bean-definition-overriding: true
启动也是可以的

https://www.e-learn.cn/topic/3338227
https://blog.csdn.net/weixin_43368449/article/details/104028676

 

posted @ 2023-03-20 20:43  沧海一滴  阅读(892)  评论(0编辑  收藏  举报