spring-cloud-alibaba-sentinel和feign配合使用,启动报Caused by: java.lang.AbstractMethodError: com.alibaba.clo

问题:

  • Spring Cloud Version:Hoxton.SR3
  • Spring Cloud Alibaba Version:2.2.0.RELEASE

项目中consumer端使用@FeignClient定义接口,启动报错:

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]

分析:

Spring Cloud从Hoxton.SR1版本开始,依赖的spring-cloud-starter-openfeign版本为2.2.2.RELEASE;
Spring Cloud Alibaba 2.2.0.RELEASE依赖的spring-cloud-openfeign-dependencies版本为2.2.0.RELEASE;

feign的Contract接口,
2.2.0.RELEASE:

// TODO: break this and correct spelling at some point
List parseAndValidatateMetadata(Class targetType);

2.2.2.RELEASE:

List parseAndValidateMetadata(Class targetType);

在2.2.0.RELEASE里有一行注释描述,接口方法名拼写错误,在2.2.2.RELEASE方法已修正了,即方法名发生了改变。

spring-cloud-alibaba-sentinel中的SentinelContractHolder类,用到了该接口的这个方法(feign2.2.0.RELEASE版本):

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

解决:

以下3中方式均可:

  • 修改Spring Cloud版本,Hoxton.SR3改为Hoxton.RELEASE

  • 在dependencyManagement中指定spring-cloud-openfeign-dependencies的版本为2.2.0.RELEASE


    
        
            org.springframework.cloud
            spring-cloud-openfeign-dependencies
            2.2.0.RELEASE
            pom
            import
        

        
            org.springframework.cloud
            spring-cloud-dependencies
            Hoxton.SR3
            pom
            import
        

        
            com.alibaba.cloud
            spring-cloud-alibaba-dependencies
            2.2.0.RELEASE
            pom
            import
        
    

注意spring-cloud-openfeign-dependencies写在spring-cloud-dependencies前面,因为dependency前面的优先生效。

  • 在项目中相同包路径下新建SentinelContractHolder类,方法实现和spring-cloud-alibaba-sentinel相同,修改Contract接口为新版版本的方法名即可。

你可能感兴趣的:(spring-cloud-alibaba-sentinel和feign配合使用,启动报Caused by: java.lang.AbstractMethodError: com.alibaba.clo)