Spring Cloud系列之Feign的常见问题总结
一、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}") 。
二、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”,不能省略,必须指定。
三、FeignClient多参数的构造
如果想要请求microservice-provider-user 服务,并且参数有多个例如: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);
...
}
该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。
正确的写法:
写法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);
}
四、Feign如果想要使用Hystrix Stream,需要做一些额外操作
我们知道Feign本身就是支持Hystrix的,可以直接使用@FeignClient(value = "microservice-provider-user", fallback = XXX.class) 来指定fallback的类,这个fallback类集成@FeignClient所标注的接口即可。
但是假设我们需要使用Hystrix Stream进行监控,默认情况下,访问http://IP:PORT/hystrix.stream 是个404。如何为Feign增加Hystrix Stream支持呢?
需要以下两步:
第一步:添加依赖,示例:
<!-- 整合hystrix,其实feign中自带了hystrix,引入该依赖主要是为了使用其中的hystrix-metrics-event-stream,用于dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
第二步:在启动类上添加@EnableCircuitBreaker 注解,示例:
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableCircuitBreaker
public class MovieFeignHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(MovieFeignHystrixApplication.class, args);
}
}
这样修改以后,访问任意的API后,再访问http://IP:PORT/hystrix.stream,就会展示出一大堆的API监控数据了。
五、如果需要自定义单个Feign配置,Feign的@Configuration 注解的类不能与@ComponentScan 的包重叠
如果包重叠,将会导致所有的Feign Client都会使用该配置。
六、首次请求失败
详见:解决Spring Cloud中Feign/Ribbon第一次请求失败的方法
七、@FeignClient 的属性注意点
(1) serviceId属性已经失效,尽量使用name属性。例如:
@FeignClient(serviceId = "microservice-provider-user")
这么写是不推荐的,应写为:
@FeignClient(name = "microservice-provider-user")
(2) 在使用url属性时,在老版本的Spring Cloud中,不需要提供name属性,但是在新版本(例如Brixton、Camden)@FeignClient必须提供name属性,并且name、url属性支持占位符。例如:
@FeignClient(name = "${feign.name}", url = "${feign.url}")
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持
Spring Cloud系列之Feign的常见问题总结的更多相关文章
- Spring Cloud中关于Feign的常见问题总结
一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...
- Spring Cloud系列文,Feign整合Ribbon和Hysrix
在本博客之前的Spring Cloud系列里,我们讲述了Feign的基本用法,这里我们将讲述下Feign整合Ribbon实现负载均衡以及整合Hystrix实现断路保护效果的方式. 1 准备Eureka ...
- Spring Cloud系列(二) 介绍
Spring Cloud系列(一) 介绍 Spring Cloud是基于Spring Boot实现的微服务架构开发工具.它为微服务架构中涉及的配置管理.服务治理.断路器.智能路由.微代理.控制总线.全 ...
- Spring Cloud 系列之 Apollo 配置中心(三)
本篇文章为系列文章,未读前几集的同学请猛戳这里: Spring Cloud 系列之 Apollo 配置中心(一) Spring Cloud 系列之 Apollo 配置中心(二) 本篇文章讲解 Apol ...
- Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端
Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...
- Spring Cloud 入门 之 Feign 篇(三)
原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...
- Spring Cloud 系列之 Spring Cloud Stream
Spring Cloud Stream 是消息中间件组件,它集成了 kafka 和 rabbitmq .本篇文章以 Rabbit MQ 为消息中间件系统为基础,介绍 Spring Cloud Stre ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例
既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...
- Spring Cloud 系列之 Consul 注册中心(二)
本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Consul 注册中心(一) 本篇文章讲解 Consul 集群环境的搭建. Consul 集群 上图是一个简单的 Co ...
随机推荐
- 每天一个小程序—0000题(python图像处理)
第 0000 题: 将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果. 类似于图中效果 python中的pillow库是专门用于处理图像的. from PIL ...
- Jquery中的DOM操作:
DOM是 Document Object Model的缩写,是一种与浏览器,平台,语言无关的接口,使用该接口可以访问页面中所有的标准组件,下面介绍一下常用的一些DOM操作: 选择节点: 将在下篇博客中 ...
- FILE,id不一致
当图片全部是新增的时候,id和FILE一一对应,后台可以匹配处理. array(1) { ["banner_img"] => array(5) { ["name&q ...
- python web.py实现简单的get和post请求
使用web.py框架,实现简单的get和post请求: py文件名:mytest.py import web urls = ( '/', 'hello' ) app = web.application ...
- Java String删除字符串中间的某部分
当你想删除字符串中的某部分时,java中并没有直接提供相关的方法,比如想删除 "cout<<\"Hello world\"<<endl" ...
- leecode第四题(寻找两个有序数组的中位数)
题解: class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<i ...
- MySQL学习(二)
1 增删改查是针对表来说的. 2 创建一个表 mysql> create table stu( -> id int primary key auto_increment, -> sn ...
- lua --- unpack
unpack 返回数组中的所有元素,包括 nil,注意是数组,对于 k-v 是不返回的!!! do , ,o = } print(unpack(tab)) --默认从索引 1 开始 )) --从索引 ...
- 注册表的作用、bat文件中REG ADD命令添加注册表项以及bat
注册表的用途与设置 注册表是windows的核心,里面储存着大量的系统信息,说白了就是一个庞大的数据库.如果你不懂什么是数据库,那没关系,不影响你了解注册表,不过最好对数据库有所了解.注册表里面所有的 ...
- python中函数与函数式编程(一)
在学习之前,我们先去区分面对对象.面对过程.函数式编程他们之间的区别,从改图可以看出,他们之间不是完全相同的,也不是没有任何相同点的 1.函数和过程的基本认识 def func1(): "& ...