天天看点

SpringCloud使用feign过程中遇到问题----持续更新中

1、首次访问超时问题

原因: Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码。而首次请求往往会比较慢(因为Spring的懒加载机制,要实例化一些类),这个响应时间可能就大于1秒了。

解决方法:

  • 配置Hystrix的超时时间改为5秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
           
  • 禁用Hystrix的超时时间
  • 禁用feign的hystrix 功能

注: 个人推荐 第一 或者第二种 方法

2、FeignClient接口中,如果使用到@PathVariable ,必须指定其value

错误写法:

@RequestMapping(value = "hi/{id}", method = RequestMethod.GET)
String hiService(@PathVariable Integer id);
           

正确写法:

@RequestMapping(value = "hi/{id}", method = RequestMethod.GET)
String hiService(@PathVariable(value = "id") Integer id);
           
3、FeignClient接口中,如果使用到@RequestParam ,必须指定其name

错误写法:

@RequestMapping(value = "hi", method = RequestMethod.GET)
String hiService(@RequestParam String name);
           

正确写法:

@RequestMapping(value = "hi", method = RequestMethod.GET)
String hiService(@RequestParam(name = "name") String name);
           

继续阅读