好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring cloud踩坑记录之使用feignclient远程调用服务404的方法

前言

公司项目进行微服务改造,由之前的dubbo改用springcloud,微服务之间通过feignclient进行调用,今天在测试的时候,eureka注册中心有相应的服务,但feignclient就是无法调通,一直报404错误,排查过程如下:

一、问题:

服务提供方定义的接口如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/**

  * 黑白名单查询接口

  *

  * @author lijunjun

  * @since 2018/10/18

  */

@component (value = "blackandwhitelistfeignclient" )

@feignclient (value = "pear-cache-service" , path = "v1/cache/limitlist" )

public interface iblackandwhitelistfeignclient {

 

  /**

   * 获取黑白名单手机号分组编号

   *

   * @param trace 请求流水

   * @param phonenum 电话号码

   * @return 电话号码所在分组

   */

  @requestmapping (value = "/blackandwhitelist" , method = requestmethod.post, produces = mediatype.application_json_utf8_value)

  resultdata<string> blackandwhitelist( @requestheader (name = "trace" ) string trace, @requestparam ( "phonenum" ) string phonenum);

}

接口实现类如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

/**

  * 黑白名单controller

  *

  * @author lijunjun

  * @since 2018/10/18

  */

@protectedldapi

@restcontroller

@requestmapping (value = "v1/cache/limitlist" )

@api (value = "黑白名单缓存" , description = "黑白名单缓存相关接口" )

public class blacklandwhitelistcontroller extends abstractcontroller implements iblackandwhitelistfeignclient {

 

  /**

   * 日志记录器

   */

  private final static log logger = new log(blacklandwhitelistcontroller. class );

 

  /**

   * 注入tedis

   */

  @autowired

  private jedissentinelpoolext jedissentinelpool;

 

  /**

   * 获取黑白名单手机号分组编号

   *

   * @param trace 请求流水

   * @param phonenum 电话号码

   * @return 电话号码所在分组

   */

  @override

  @apioperation (value = "获取黑白名单手机号分组编号" , notes = "根据电话号码从缓存中获取黑白名单分组" )

  @requestmapping (value = "/blackandwhitelist" , method = requestmethod.post, consumes = mediatype.application_json_utf8_value, produces = mediatype.application_json_utf8_value)

  public resultdata<string> blackandwhitelist( @requestheader (name = "trace" ) string trace, @requestparam ( "phonenum" ) string phonenum) {

   do something...

  }

}

调用方如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

public class messagelistcontroller {

 

  private static final log logger = new log(messagelistcontroller. class );

 

  @autowired

  private iblackandwhitelistfeignclient blackandwhitelistfeignclient;

 

  @requestmapping (value = "/testblackandwhitelist" , method = requestmethod.post, produces = mediatype.application_json_utf8_value)

  public resultdata<string> testblackandwhitelist() {

 

   logger.info( "开始调用缓存接口" );

 

   resultdata<string> res = blackandwhitelistfeignclient.blackandwhitelist( "asdqwezxxc" , "b18037903086" );

 

   logger.info( "调用结果:" + res.getresultdata());

 

   return res;

  }

调用结果:

 华丽丽的404了,很头疼,经过各种度娘,发现导致这个问题有两个原因,以下是解决方法:

二、问题分析

经过百度,说将springboot配置文件里面 server.servlet.context-path 注释掉即可,抱着试一哈的态度,注释了,重启,调用,结果惊喜的发现,依旧报错了,但仔细一看,错误代码已经不是404,变成了415,这就相当于调通了,但是,content-type的类型不对,于是,返回去看代码(此时已经肯定,今天能把feignclient接口调通),

仔细一看发现,接口上定义的@requestmapping中,只定义了 produces = mediatype.application_json_utf8_value ,而实现类中,@requestmapping定义了consumes、produces均为 "application/json;charset=utf-8"

我们知道,consumes定义了方法接受的http的请求类型,produces则定义了http请求返回的类型;

然后我们说下feignclient,它的底层实现,就是根据定义的feignclient接口,来组装http请求进行 远程调用 ,而http默认的content-type是x-www-form-urlencoded类型化的,到这儿,问题就呼之欲出了:

再来回顾上面我们定义的接口,并没有指定请求类型(consumes),那么feignclient组装的http请求的类型就是默认的x-www-form-urlencoded类型,但我们的实现类上,却定义了 consumes=mediatype.application_json_utf8_value ,也就是说,仅接受json类型的请求,这就是为什么415的原因了;

三、解决方法

知道了问题的原因,解决起来就很简单了,我们可以在feignclient的接口定义上,指定consumes,这样,feignclient在组装http请求的时候,就会在header里面设置请求类型为application/json,这样,问题就完美解决;

再来看调用结果:

完美返回!!!

四、总结

feignclient接口定义是一个模板化的,其组装的http请求完全按照你定义的接口去组装,如你在参数中,用@requestheader去接收一个参数,其组装请求时,就会将你传入的参数放至header中,你指定的consumes为json,其组装的请求content-type就是 application/json类型的,完全不需要调用方感知,就像调用普通方法一样,不得不说,很强大,只要生成的http请求正确,服务提供方提供的rest接口能和feignclient组装的http请求,就能够完成远程调用。

五、遗留问题

为什么需要将服务提供方的 server.servlet.context-path 去掉才能实现调用,今天暂时没有研究,但一定有解决方案,springcloud不会这么low的,解决方案研究出来会补上。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:https://HdhCmsTestcnblogs测试数据/ft535535/p/9898147.html

查看更多关于Spring cloud踩坑记录之使用feignclient远程调用服务404的方法的详细内容...

  阅读:28次