Spring Cloud Feign常见问题

1. FeignClient接口,不能使用@GettingMapping 之类的组合注解

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
  public User findById(@PathVariable("id") Long id);
  ...
}

这边的@RequestMapping(value = “/simple/{id}”, method = RequestMethod.GET) 不能写成@GetMapping(“/simple/{id}”) 。

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

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
  public User findById(@PathVariable("id") Long id);
  ...
}

这边的@PathVariable(“id”) 中的”id”,不能省略,必须指定。

3. FeignClient多参数的构造

例如:

http ://microservice-provider-user/query-by?id=1&username=张三

错误写法:使用对象

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping(value = "/query-by", method = RequestMethod.GET)
  public User queryBy(User user);
  ...
}

写法1:

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping(value = "/query-by", method = RequestMethod.GET)
  public User queryBy(@RequestParam("id")Long id, @RequestParam("username")String username);
}

写法2:

@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping(value = "/query-by", method = RequestMethod.GET)
  public List<User> queryBy(@RequestParam Map<String, Object> param);
}

4. feign第一次请求会出现time-out,fallback异常

原因:

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

解决:

方法一

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

该配置是让Hystrix的超时时间改为5秒

方法二

hystrix.command.default.execution.timeout.enabled: false

该配置,用于禁用Hystrix的超时时间
ps:我自己使用这种方法才生效

方法三

feign.hystrix.enabled: false

该配置,用于索性禁用feign的hystrix。该做法除非一些特殊场景,不推荐使用。

5. 当feign返回较为复杂的对象时,会出现类型转换异常

项目中,feign调用url返回一个名为ResponseResult的对象,结果会出现 以下异常:

LinkedHashMap can not be cast to ResponseResult

目前出现原因还未得出,当返回List和简单对象时则不会出现异常

参考:http://itmuch.com/spring-cloud-sum-feign/

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值