scx12333
scx12333
采纳率100%
2019-08-18 11:22 阅读 3.5k
已采纳

openfeign里面配置httpclient出错。

因为现在需要使用get方式传递对象参数,所以尝试在openfeign里面配置httpclient。
配置代码如下:
在yml文件里面增加了配置信息

feign:
  httpclient:
    enabled: true

在pom.xml文件中引入的依赖:

       <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <!-- 使用Apache HttpClient替换Feign原生httpclient -->
        <dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-httpclient</artifactId>
            <version>8.16.1</version>
        </dependency>

配置好以后尝试使用get方式传递对象参数。
服务提供端代码:

 @GetMapping(value = "testAddRole1",consumes = "application/json")
    public JsonResult addTest1(@RequestBody Role role){
        roleService.addRole(role);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS);
    }

服务调用接口为:

@GetMapping(value = "/testAddRole1",consumes = "application/json")
    JsonResult testAddRole1(@RequestBody Role role);

现在在尝试调用接口后发现服务提供方可以接受到参数并且能够写到数据库中,但是在服务调用方拿不到正确的返回值并且会报错,错误如下:

Caused by: java.lang.NoSuchMethodError: feign.Response.create(ILjava/lang/String;Ljava/util/Map;Lfeign/Response$Body;)Lfeign/Response;

查看源码得知,openfeign在接受返回值时调用的不是httpclient的feign-core包的代码而是调用的本身的feign-core的代码,而本身的feign-core包中的Response类没有create方法。
得知原因后我将httpclient的依赖在pom文件中的位置上移,使openfeign优先调用httpclient的feign-core包的代码,结果在启动项目时就报错,报错信息为:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'test1Controller' defined in file [D:\workspace\basic\openfeign\target\classes\cn\cloudscope\openfeignTest\Test1\controller\Test1Controller.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.cloudscope.openfeignTest.Test1.service.Test1Service': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'feignRetryer' defined in org.springframework.cloud.openfeign.FeignClientsConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [feign.Retryer]: Factory method 'feignRetryer' threw exception; nested exception is java.lang.NoSuchFieldError: NEVER_RETRY

查看原因得知是两个feign-core包中的Retryer接口不一致导致的,求来个大神帮忙解决一下,或者怎么调整jar包的版本去解决这个问题。openfeign的feign-core版本为10.1.0 httpclient的版本为8.16.1

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    u011781096 c小小菜 2019-08-24 15:05

    我也遇到了这个问题,把你的feign-httpclient依赖换成这个

    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-httpclient</artifactId>
        <version>10.1.0</version>
    </dependency>
    
    点赞 9 评论 复制链接分享
  • Thinkingcao Thinkingcao 2020-10-19 16:41

    正是feign-httpclient依赖版本的不匹配导致的报异常,需要feign-core和feign-httpclient版本对应即可,参考下方:

    图片说明

    点赞 评论 复制链接分享

相关推荐