错误描述:

版本使用的是SpringBoot: 2.1.0.RELEASE,SpringCloud: Greenwich.M1,OpenFeign: 2.1.0.M2 报错:

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

Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: 
Invalid bean definition with name xxxx.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 'xxxx.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.

分析:

多个接口上的@FeignClient(“相同服务名”)会报错,overriding is disabled,即出现了相同的Bean名。

在启动类中添加的@EnableFeignClients中,可看到@import引入FeignClientsRegistrar实现注入。在这里插入图片描述 核心是BeanDefinitionRegistry在这里插入图片描述 注册FeignClients

    public void registerFeignClients(AnnotationMetadata metadata,
			BeanDefinitionRegistry registry) {
		ClassPathScanningCandidateComponentProvider scanner = getScanner();
		scanner.setResourceLoader(this.resourceLoader);

		Set<String> basePackages;

		Map<String, Object> attrs = metadata
				.getAnnotationAttributes(EnableFeignClients.class.getName());
		AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(
				FeignClient.class);
		final Class<?>[] clients = attrs == null ? null
				: (Class<?>[]) attrs.get("clients");
		if (clients == null || clients.length == 0) {
			scanner.addIncludeFilter(annotationTypeFilter);
			basePackages = getBasePackages(metadata);
		}
		else {
			final Set<String> clientClasses = new HashSet<>();
			basePackages = new HashSet<>();
			for (Class<?> clazz : clients) {
				basePackages.add(ClassUtils.getPackageName(clazz));
				clientClasses.add(clazz.getCanonicalName());
			}
			AbstractClassTestingTypeFilter filter = new AbstractClassTestingTypeFilter() {
				@Override
				protected boolean match(ClassMetadata metadata) {
					String cleaned = metadata.getClassName().replaceAll("\\$", ".");
					return clientClasses.contains(cleaned);
				}
			};
			scanner.addIncludeFilter(
					new AllTypeFilter(Arrays.asList(filter, annotationTypeFilter)));
		}

		for (String basePackage : basePackages) {
			Set<BeanDefinition> candidateComponents = scanner
					.findCandidateComponents(basePackage);
			for (BeanDefinition candidateComponent : candidateComponents) {
				if (candidateComponent instanceof AnnotatedBeanDefinition) {
					// verify annotated class is an interface
					AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) candidateComponent;
					AnnotationMetadata annotationMetadata = beanDefinition.getMetadata();
					Assert.isTrue(annotationMetadata.isInterface(),
							"@FeignClient can only be specified on an interface");

					Map<String, Object> attributes = annotationMetadata
							.getAnnotationAttributes(
									FeignClient.class.getCanonicalName());

					String name = getClientName(attributes);
					registerClientConfiguration(registry, name,
							attributes.get("configuration"));

					registerFeignClient(registry, annotationMetadata, attributes);
				}
			}
		}
	}

其中registerFeignClient方法需要装配BeanDefinitionBuilder对象,可以看到这里实际注入的是FeignClientFactoryBeanBeanDefinitionHolder中传递了beanName,而它是等于annotationMetadata.getClassName();,即被FeignClient注解的接口的全限定类名。 那么它的Bean名不会重复,错误原因并不是它引起的。

在这里插入图片描述

而注册configuration实际注入的是FeignClientSpecification,这就是我们所报错的already存在的Bean。 可看到 beanName:name + "." + FeignClientSpecification.class.getSimpleName()在这里插入图片描述 而其中的name参数,就是在registerFeignClients方法中通过getClientName()获得。

String name = getClientName(attributes);
registerClientConfiguration(registry, name, attributes.get("configuration"));

getClientName()源码中,可以看到name就是@FeignClient注解中的serviceId,name,value。 beanName:name + "." + FeignClientSpecification.class.getSimpleName() 故而我们在使用相同名称的FeigClient注解时,注入到Ioc的是相同Bean名。 所以错误是由FeignClientSpecification类引起的。在这里插入图片描述

解决:

在application.yml中配置:

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

在SpringBoot 2.1之前,这个配置默认就是true,而在2.1做了更改。 设置为true后,因为FeignClientSpecification的原因,FeignClient注解的configuration参数会被覆盖。

解决配置覆盖参考: https://blog.csdn.net/neosmith/article/details/82349449

个人觉得Stackoverflow中有位大佬说的不错: https://stackoverflow.com/questions/53103991/cant-stat-app-after-updating-springboot-from-2-0-6-release-to-2-1-0-release

别的情况下也可能会因为Bean名重复引起该错误,更佳的解决方案是尽可能避免去定义相同的Bean名。 而Feign远程调用服务因为业务需求出现在不同在接口上使用相同的服务名,将allow-bean-definition-overriding设置为true,如果在其他情况下没有重复Bean名,那么受影响的应该就只是Feign的configuration(然而configuration我都没另外做配置,覆盖也不影响- -)。

在这里插入图片描述


评论里大佬推荐在注解中添加 contextId 来区分,解决该问题。

@FeignClient(name="common-service", contextId = "example")