Feign报错‘xx.FeignClientSpecification‘, defined in null, could not be registered.

错误描述:

版本使用的是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对象,可以看到这里实际注入的是FeignClientFactoryBean
BeanDefinitionHolder中传递了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")


 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用Feign进行远程调用时,有时会遇到.lang.reflect.InvocationTargetException异常。这个异常是由Java的反射机制引起的,表示在调用方法时发生了异常。 java.lang.reflect.InvocationTargetException是一个包装异常,它将原始异常封装在内部。通常,我们需要查看InvocationTargetException的getCause()方法来获取实际的异常信息。 出现java.lang.reflect.InvocationTargetException异常的原因可能有很多,以下是一些常见的情况: 1. 远程服务不可用:如果远程服务没有启动或者无法访问,Feign调用就会失败,并抛出InvocationTargetException异常。 2. 请求参数错误:如果Feign调用的请求参数不正确,例如参数类型不匹配或者缺少必要的参数,远程服务可能会抛出异常,并导致InvocationTargetException异常。 3. 远程服务异常:如果远程服务在处理请求时发生了异常,它可能会将异常信息返回给Feign客户端,并导致InvocationTargetException异常。 为了解决这个问题,我们可以通过以下步骤来排查: 1. 检查远程服务是否正常运行,并确保可以正常访问。 2. 检查Feign调用的请求参数是否正确,并与远程服务的接口定义进行对比。 3. 查看InvocationTargetException的getCause()方法返回的异常信息,以确定实际的异常原因。 4. 根据具体的异常原因采取相应的措施,例如修复远程服务的bug或者调整Feign调用的参数。 希望以上信息对您有帮助!如果您还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值