头图

背景

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

现在的环境

依赖版本
Spring Boot2.2.5.RELEASE
Spring CloudHoxton.SR3
Spring Cloud Alibaba2.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
  3. 暂时先不使用Hoxton,等Spring Cloud Alibaba v2.2.1.RELEASE发布之后再进行版本升级

在这里插入图片描述

修改方法

  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的后台动态权限管理脚手架


码匠君
41 声望0 粉丝

我们所耳闻的一切都只是观点而不是事实。我们所看见的都是一个角度而不是真相。——马库斯.奥利利乌斯