Spring Cloud 之OpenFeign
Spring Cloud 之OpenFeign
一:简介
Feign是一个声明式(对比RestTemplate编程式)的服务客户端,即通过@FeignClient注解即可声明一个接口(interface)。还支持多种插拔式的配置如encoders/decoders(加解码),clients(不同的链接框架)...... 。Spring Cloud 集成了 Eureka、Spring Cloud Breaker 以及 Spring Cloud LoadBalancer,便可实现 Feign 负载平衡的 http 客户端。

二: 使用方式一(单独使用)
2.1:引入依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>??feign.version??</version>
</dependency>
2.2:定义接口
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}
public static class Contributor {
String login;
int contributions;
}
2.3 : 构造Feign的 http 客户端 示例
public class MyApp {
public static void main(String... args) {
GitHub github = Feign.builder()
.decoder(new GsonDecoder())
.target(GitHub.class, "https://api.github.com");
List<Contributor> contributors = github.contributors("OpenFeign", "feign");
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
}
}
示例解释:
2.3.1 :接口地址‘ https://api.github.com/repos/OpenFeign/feign/contributors’是一个获取github上OpenFeign项目的贡献者名单信息。
2.3.2 :Feign.builder(构建者模式(23种设计模式之一)).decoder指定解析器(返回的是json数组).target指定指定目标对象(接口和url)
2.3.3 :自定义其他参数
.encoder(加析器),errorDecoder(异常解析器).client(指定使用的客户端)....再次不一一介绍。
小结: 通过简介以及一个简单的示例,我们已经明了了Feign用法和架构,下面就看springcloud如何集成Feign
三:springcloud集成Feign
3.1:启动类加@EnableFeignClients注解开启声明式Feign,
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.2: 通过interface定义接口地址(复用2.2:定义接口)
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}
3.3:通过@FeignClient注解(相当于 Feign.builder()) 定义远程服务地址和其他组件
@FeignClient(url = "服务名地址(https://api.github.com)" ,value = "服务名(springboot项目的项目名与url二选一)")
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}
四:集成feign后的常见需求
4.1 Feign调用失败后的错误分析
fallbackFactory :错误后的返回信息 推荐使用相比fallback可辨知错误原因)
fallback:错误后的返回信息
#配置文件中需开启错误分析
feign:
hystrix: # 老版本
enabled: true
circuitbreaker: #新版本
enabled: true
4.1.2 fallbackFactory 示例
@Component
public class FallbackFactoryGitHub implements FallbackFactory<GitHub> {
private static final Logger logger = LoggerFactory.getLogger(GitHub.class);
@Override
public GitHub create(Throwable throwable) {
return new GitHub() {
@Override
public String contributors( String owner, String repo) {
logger.error("contributors", throwable);
return throwable.getMessage();
}
.....
};
}
}
4.2 服务间通过Feign调用,会丢失请求头的信息
4.2.1 步骤一:定义RequestInterceptor传的请求头信息:
@Bean
public RequestInterceptor headerInterceptor() {
return template -> {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (null != attributes) {
attributes.getRequest();
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
// 跳过 content-length
if (name.equals("content-length")){
continue;
}
template.header(name, values);
}
}
}
};
}
4.2.2 指定配置
@FeignClient(.... , configuration = XXX.class)
五:参考资料
5.1 https://docs.spring.io/spring-cloud-openfeign/docs/3.1.8/reference/html
5.2 https://github.com/OpenFeign/feign
最后期待负载均衡客户段是如何实现的
Spring Cloud 之OpenFeign的更多相关文章
- Spring cloud简单学习总结
微服务简介 一.spring boot和spring cloud 的关系 spring boot来写各个拆分出来的微服务,spring cloud把各个微服务联系起来,比如各个微服务通过eurke找 ...
- [Spring Cloud实战 | 第六篇:Spring Cloud Gateway+Spring Security OAuth2+JWT实现微服务统一认证授权
一. 前言 本篇实战案例基于 youlai-mall 项目.项目使用的是当前主流和最新版本的技术和解决方案,自己不会太多华丽的言辞去描述,只希望能勾起大家对编程的一点喜欢.所以有兴趣的朋友可以进入 g ...
- Spring Cloud实战 | 最终篇:Spring Cloud Gateway+Spring Security OAuth2集成统一认证授权平台下实现注销使JWT失效方案
一. 前言 在上一篇文章介绍 youlai-mall 项目中,通过整合Spring Cloud Gateway.Spring Security OAuth2.JWT等技术实现了微服务下统一认证授权平台 ...
- 1.入门篇十分钟了解Spring Cloud
文章目录 Spring Cloud入门系列汇总 为什么需要学习Spring Cloud 什么是Spring Cloud 设计目标与优缺点 设计目标 优缺点 Spring Cloud发展前景 整体架构 ...
- vue+uni-app商城实战 | 第一篇:【有来小店】微信小程序快速开发接入Spring Cloud OAuth2认证中心完成授权登录
一. 前言 本篇通过实战来讲述如何使用uni-app快速进行商城微信小程序的开发以及小程序如何接入后台Spring Cloud微服务. 有来商城 youlai-mall 项目是一套全栈商城系统,技术栈 ...
- 【Spring Cloud + Vue 有来商城】研发小组开发规范全方位梳理
项目演示 后端 Spring Cloud实战 | 第一篇:Windows搭建Nacos服务 Spring Cloud实战 | 第二篇:Spring Cloud整合Nacos实现注册中心 Spring ...
- Spring Cloud实战 | 最八篇:Spring Cloud +Spring Security OAuth2+ Axios前后端分离模式下无感刷新实现JWT续期
一. 前言 记得上一篇Spring Cloud的文章关于如何使JWT失效进行了理论结合代码实践的说明,想当然的以为那篇会是基于Spring Cloud统一认证架构系列的最终篇.但关于JWT另外还有一个 ...
- Spring Cloud实战 | 第九篇:Spring Cloud整合Spring Security OAuth2认证服务器统一认证自定义异常处理
本文完整代码下载点击 一. 前言 相信了解过我或者看过我之前的系列文章应该多少知道点我写这些文章包括创建 有来商城youlai-mall 这个项目的目的,想给那些真的想提升自己或者迷茫的人(包括自己- ...
- Spring Cloud实战 | 第十篇 :Spring Cloud + Seata 1.4.1 + Nacos1.4.0 整合实现微服务架构中逃不掉的话题分布式事务
Seata分布式事务在线体验地址:https://www.youlai.store 本篇完整源码地址:https://github.com/hxrui/youlai-mall 有想加入开源项目开发的童 ...
- Spring Cloud实战: 基于Spring Cloud Gateway + vue-element-admin 实现的RBAC权限管理系统,实现网关对RESTful接口方法权限和自定义Vue指令对按钮权限的细粒度控制
一. 前言 信我的哈,明天过年. 这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT ...
随机推荐
- MySQL(六)存储引擎
存储引擎 连接管理.查询缓存.解析器和执行器被归为MySQL service,而把真实存储数据的功能划分为存储引擎的功能.所以MySQL service经过查询优化后,只需按照生成的执行计划调用存 ...
- express获取登录服务器的IP地址
let ip = (req.headers['x-real-ip'] || req.connection.remoteAddress).slice(7);
- Go/Python gRPC实践
gRPC框架 & ProtoBuf 安装相关工具: pip3 install grpcio pip3 install grpcio-tools protobuf3有自己专门的定义的格式,基于此 ...
- 龙芯(Loongarch64),在Linux虚拟一个龙芯OS体验下
前言 想体验下龙芯OS,但是又没有龙芯开发板或者龙芯实体机.手头上只有一个X64环境的Linux发行版,应该怎么做呢? 概括 其实非常简单,可以通过Chroot命令和Qemu在X64的指令集系统上模拟 ...
- 【LeetCode动态规划#11】打家劫舍系列题(涉及环结构和树形DP的讨论)
打家劫舍 力扣题目链接(opens new window) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻 ...
- CISP_PTE学习
一.http协议的基础知识(请求方法.状态码.响应头信息.协议的URL) 1.请求方法: (1) http1.0请求包含 head.get.post (2)http1.1请求包含head.get.po ...
- #Python assign赋值,新增列操作
- 2020-09-01:mysql里什么是检查点、保存点和中间点?
福哥答案2020-09-01: 检查点checkpoint:批量刷盘.在一定程度上代表了刷到磁盘时日志所处的日志序列号(LSN)位置.标记重做日志中已经完成刷到磁盘的位置点,如果缓冲池中有很多重做日志 ...
- Jenkins - 页面汉化
Jenkins - 页面汉化 前言 对于不懂英文的人来讲,尤其是第一次使用Jenkins环境,看全英文的Jenkins页面是十分困难的: Jenkins对于不懂英文的用户有做汉化的插件包,但是汉化的不 ...
- C盘清理,移动node 依赖和缓存文件
由于先前安装的node 没有做任何配置,都是傻瓜式下一步,导致了我很多依赖都放置C盘,内存占用过多:也不太好管理所有觉得将它移动到node安装目录 一.新建文件夹 在原本安装的nodejs目录下新建 ...