一、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}")

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

        <div class="art_xg">

您可能感兴趣的文章:

Spring Cloud中关于Feign的常见问题总结的更多相关文章

  1. Spring Cloud系列之Feign的常见问题总结

    一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...

  2. spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?

    Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢? 造成该问题的原因 Hystrix默认的超时时间是1秒,如果超过这个时间 ...

  3. Spring Cloud中Hystrix、Ribbon及Feign的熔断关系是什么?

    导读 今天和大家聊一聊在Spring Cloud微服务框架实践中,比较核心但是又很容易把人搞得稀里糊涂的一个问题,那就是在Spring Cloud中Hystrix.Ribbon以及Feign它们三者之 ...

  4. 解决Spring Cloud中Feign第一次请求失败的问题

    在Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题 com.netflix.hystrix.exception.HystrixTimeoutE ...

  5. Spring Cloud中,如何解决Feign/Ribbon第一次请求失败的问题?

    Spring Cloud中,如何解决Feign/Ribbon第一次请求失败的问题? Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解 ...

  6. Spring Cloud中,如何解决Feign整合Hystrix第一次请求失败的问题

    Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢? 造成该问题的原因 Hystrix默认的超时时间是1秒,如果超过这个时间 ...

  7. Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端

    Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...

  8. Spring Cloud中Feign如何统一设置验证token

    代码地址:https://github.com/hbbliyong/springcloud.git 原理是通过每个微服务请求之前都从认证服务获取认证之后的token,然后将token放入到请求头中带过 ...

  9. Spring Cloud中,Eureka常见问题总结

    Spring Cloud中,Eureka常见问题总结. 1 eureka.environment: 指定环境 参考文档: 1 eureka.datacenter: 指定数据中心 参考文档: 使用配置项 ...

随机推荐

  1. Cannot read property 'validate' of undefined

    在使用element-UI表单验证中一直报错,'Error in event handler for “click”: “TypeError: Cannot read property ‘valida ...

  2. 怪事年年有,今天特别多!org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'empno' not found. Available parameters are [emp, deptno, param1, param

    错误: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.Binding ...

  3. python+selenium运行时,提示元素不可见

    python+selenium运行多次新增项目脚本(出错的元素通过by_id的方式定位),当第三次新增时报Message: element not visible的错误,加入等待时间,等页面加载完成, ...

  4. 基于Linux下catalog方式的 Oracle 备份策略(RMAN)

    --********************************** -- 基于Linux下 Oracle 备份策略(RMAN) --******************************* ...

  5. 好程序员web前端分享HTML基础篇

    好程序员web前端分享HTML基础篇,最近遇到很多新手,都会问,如果要学web前端开发,需要学什么?难不难学啊?多久能入门之类的问题?那么今天好程序员就先来给大家分享一下web前端学习路线:HTML基 ...

  6. Troubleshooting 'library cache: mutex X' Waits. (Doc ID 1357946.1)

    In this Document   Purpose   Troubleshooting Steps   What is a 'library cache: mutex X' wait?   What ...

  7. 第一章 初识 MyBatis

    概念:优秀持久层框架:实体类和SQL语句之间建立映射关系 与hibernate区别    :自动生成sql语句,并且建立实体类和数据表的映射. MyBatis基本要素:核心对象   核心配置文件  S ...

  8. XML的几种转换

    package com.qbskj.project.util; import java.io.ByteArrayOutputStream; import java.util.ArrayList; im ...

  9. Vue 实现左边导航栏且右边显示具体内容(element-ui)

    最终效果图: 现在开始进入正题: 1.安装element-ui npm i element-ui -S CDN 目前可以通过 unpkg.com/element-ui 获取到最新版本的资源,在页面上引 ...

  10. h5手机查看

    1.装个node:2.全局装个anywhere的npm包.(npm i -g anywhere)3.大功告成,现在到任意目录下用命令行执行anywhere就可以:(-p 参数可以设置启动端口) 补充: ...