com.alibaba.cloud.sentinel.feign.SentinelContractHolder.parseAndValidateMetadata(Ljava/lang/Class;)L

背景

随着Spring Cloud Alibaba 2.2.0.RELEASE的发布,终于可以使用最新的Spring Boot和Spring Cloud。

现在的环境

依赖 版本
Spring Boot 2.2.5.RELEASE
Spring Cloud Hoxton.SR3
Spring Cloud Alibaba 2.2.0.RELEASE

问题

已有工程升级了依赖版本之后,一启动就抛出以下错误:

Caused by: java.lang.AbstractMethodError: com.alibaba.cloud.sentinel.feign.SentinelContractHolder.parseAndValidateMetadata(Ljava/lang/Class;)Ljava/util/List;
	at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:151) ~[feign-core-10.7.4.jar:na]
	at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:49) ~[feign-core-10.7.4.jar:na]
	at feign.Feign$Builder.target(Feign.java:252) ~[feign-core-10.7.4.jar:na]
	at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:38) ~[spring-cloud-openfeign-core-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:253) ~[spring-cloud-openfeign-core-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:282) ~[spring-cloud-openfeign-core-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:262) ~[spring-cloud-openfeign-core-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	... 67 common frames omitted

解决

看了一下代码,问题的表现是从Sentinel抛出,本质是由于feign核心接口方法命名纠正拼写错误导致

Hoxton.SR1 中,fegin.context接口方法的定义为parseAndValidatateMetadata
Hoxton.SR3 中,fegin.context接口方法的定义为parseAndValidateMetadata

很明显是为了纠正拼写错误。

解决办法

  1. 将Spring Cloud Hoxton.SR3 降级为 Spring Cloud Hoxton.SR1
  2. 暂时自己修改SentinelContractHolder

修改方法

  1. 在自己工程合适的地方,新建package:com.alibaba.cloud.sentinel.feign
  2. 拷贝com.alibaba.cloud.sentinel.feign.SentinelContractHolder至该目录
  3. 修改对应错误方法名

改后代码如下:

public class SentinelContractHolder implements Contract {

	private final Contract delegate;

	/**
	 * map key is constructed by ClassFullName + configKey. configKey is constructed by
	 * {@link feign.Feign#configKey}
	 */
	public final static Map<String, MethodMetadata> METADATA_MAP = new HashMap<>();

	public SentinelContractHolder(Contract delegate) {
		this.delegate = delegate;
	}

	@Override
	public List<MethodMetadata> parseAndValidateMetadata(Class<?> targetType) {
		List<MethodMetadata> metadatas = delegate.parseAndValidateMetadata(targetType);
		metadatas.forEach(metadata -> METADATA_MAP
				.put(targetType.getName() + metadata.configKey(), metadata));
		return metadatas;
	}
}

同步在Sentiel Github增加了说明:地址

个人首个开源项目:Spring Boot,Shiro,Redis,Ehcache,Thymeleaf,Postgresql的后台动态权限管理脚手架

发布了27 篇原创文章 · 获赞 8 · 访问量 14万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览