OpenFeign是什么

随着业务的增多,我们的单体应用越来越复杂,单机已经难以满足性能的需求,这时候出现了分布式。分布式通讯除了RPC, REST HTTP请求是最简单的一种方式。OpenFeign是Netflix开源的参照Retrofit, JAXRS-2.0, and WebSocket的一个http client客户端,致力于减少http client客户端构建的复杂性。

官方用法

github提供了一个简单的demo,很容易理解。

interface GitHub {@RequestLine("GET /repos/{owner}/{repo}/contributors")List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}static class Contributor {String login;int contributions;
}public static void main(String... args) {GitHub github = Feign.builder().decoder(new GsonDecoder()).target(GitHub.class, "https://api.github.com");// Fetch and print a list of the contributors to this library.List<Contributor> contributors = github.contributors("OpenFeign", "feign");for (Contributor contributor : contributors) {System.out.println(contributor.login + " (" + contributor.contributions + ")");}
}

简单的说,这么用没问题。但如果想要集成到系统中,关于Hystrix的配置还需要自己指定。为此,我单独把配置方案提炼了一下。

项目地址: https://github.com/Ryan-Miao/springboot-starter-feign

本项目提供了一个开箱即用的spring boot feign starter, 基于默认的约定配置
来简化和优化OpenFeign的使用流程.

How to use

引入repo

<repositories><repository><id>jitpack.io</id><url>https://jitpack.io</url></repository>
</repositories>

引入依赖

<dependency><groupId>com.github.Ryan-Miao</groupId><artifactId>springboot-starter-feign</artifactId><version>1.1</version>
</dependency>

在springboot 项目中添加Configuration

@Autowired
private Environment environment;@Bean
public FeignFactory feignFactory() {return new FeignFactory(environment, hystrixConfigurationProperties());
}@Bean
public HystrixConfigurationProperties hystrixConfigurationProperties() {return new HystrixConfigurationProperties();
}

然后就可以使用了。

使用和配置

约定了一些配置,大概如下

feign:hystrixConfig:"hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds": 8000"hystrix.command.GithubConnector#getRepos.execution.isolation.thread.timeoutInMilliseconds": 15000endpointConfig:GithubConnector:default:url: https://api.github.comreadTimeoutMillis: 8000connectTimeoutMillis: 5000getRepos:url: https://api.github.comreadTimeoutMillis: 15000connectTimeoutMillis: 10000
  • feign是配置的第一个索引
  • hystrixConfig是hystrix的配置,更多配置见Hystrix
  • endpointConfig是我们远程请求的host和超时配置,其中,第一个节点为Connector class
    的名称,下一个是具体到某个请求的key,整个Connector class的默认配置是default
    节点,如果该Connector里的某个请求的超时比较长,需要单独设置,则会覆盖默认节点。
    另外,hystrix的超时配置commankey为[connectorClassName][#][methodName]

定义一个GithubConnector,继承com.miao.connect.Connector

public interface GithubConnector extends Connector {@RequestLine("GET /users/{username}")@Headers({"Content-Type: application/json"})GithubUser getGithubUser(@Param("username") String username);@RequestLine("GET /users/{username}/repos")@Headers({"Content-Type: application/json"})Observable<String> getRepos(@Param("username") String username);
}

调用

@Autowired
private FeignFactory feignFactory;@GetMapping("/profile/{username}")
public GithubUser getProfile(@PathVariable String username) {//采用Jackson作为编码和解码类库,url和超时配置按照default,即读取feign.endpointConfig.GithubConnector.defaultfinal GithubConnector connector = feignFactory.builder().getConnector(GithubConnector.class);return connector.getGithubUser(username);
}@GetMapping("/repos/{username}")
public String getUserRepos(@PathVariable String username) {//用String来接收返回值, url和超时单独指定配置,因为请求时间较长//采用connector的method来当做获取配置的key,即读取feign.endpointConfig.GithubConnector.getReposfinal GithubConnector connector = feignFactory.builder().connectorMethod("getRepos").stringDecoder()  //默认使用jackson作为序列化工具,这里接收string,使用StringDecoder.getConnector(GithubConnector.class);return connector.getRepos(username).onErrorReturn(e -> {LOGGER.error("请求出错", e);Throwable cause = e.getCause();if (cause instanceof FeignErrorException) {throw (FeignErrorException) cause;}throw new RuntimeException("请求失败", e);}).toBlocking().first();
}

具体见使用示例example

相比原生有什么区别?

最大的区别是hystrix配置的内容,原生并没有提供hystrix相关配置,需要自己额外
准备。这里集成hystrix的约定,只要按照hystrix官方参数配置即可。

然后是缓存,在使用原生OpenFeign的过程中发现每次请求都要创建一个Connector,
而且Connector的创建又依赖一大堆别的class。对于我们远程调用比较频繁的应用来说,
增大了垃圾收集器的开销,我们其实不想回收。所以对Connector做了缓存。

其他用法同OpenFeign。

OpenFeign封装为springboot starter相关推荐

  1. 深入理解springboot starter

    定义:Spring Boot Starter 是在 SpringBoot 组件中被提出来的一种概念,官网概念 Starter POMs are a set of convenient dependen ...

  2. Spring Boot学习总结(22)——如何定制自己的 springboot starter 组件呢?

    引言 我们日常项目中都会用到springboot,只要我们用到springboot,一定会用到各种spring-boot-starter.下面我们通过一个springboot starter 的dem ...

  3. 简述SpringBoot Starter原理及自定义实现

    简述SpringBoot Starter原理及自定义实现 一.简述 二.结合SpringBoot启动原理看容器如何实现自动装配 三.解析mybatis-spring-boot-starter包看myb ...

  4. SpringSecurity Oauth2 - 自定义 SpringBoot Starter 远程访问受限资源

    文章目录 1. 自定义 SpringBoot Starter 1. 统一的dependency管理 2. 对外暴露 properties 3. 实现自动装配 4. 指定自动配置类的路径 META-IN ...

  5. Springboot starter开发之traceId请求日志链路追踪

    一.请求链路追踪是什么? 能标识一次请求的完整流程,包括日志打印.响应标识等,以便于出现问题可以快速定位并解决问题. 二.使用步骤 1. 相关知识点 ThreadLocal:一种保证一种规避多线程访问 ...

  6. 自定义SpringBoot Starter实现

    文章目录 自定义stater pom文件 配置文件类properties 使用配置类 创建AutoConfiguration 项目结构 自定义stater pom文件 引入自动配置类spring-bo ...

  7. springboot starter工作原理_98,谈谈SpringBoot的工作原理

    对技术的探索,一切源于好奇心,保持好奇心,才能让人更年轻. 至今,我们已经有了很多创建SpringBoot项目的经验,比如我们要创建一个支持web开发的项目,我们只需要引入web-starter模块即 ...

  8. java公司自己封装的框架_SpringBoot封装自己的Starter的实现方法

    一.说明 我们在使用SpringBoot的时候常常要引入一些Starter,例如spring-boot-starter-web,官方为我们提供了几乎所有的默认配置,很好的降低了使用框架时的复杂度,所以 ...

  9. SpringBoot Starter介绍以及实例

    一.Spring Boot Starter简介 Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件 ...

最新文章

  1. 软件开发者的“比天之翼”
  2. 如何复制带格式的Notepad++文本?
  3. Angular元素属性绑定的一个例子
  4. Power Automate Desktop概览
  5. 威马汽车否认接盘ST众泰:没有任何兴趣参与
  6. 18岁男子吸电子烟一年肺如70岁老人,怒诉电子烟公司
  7. 聊聊spring for kafka对consumer的封装与集成
  8. 【渝粤教育】国家开放大学2018年秋季 1356T高级英语听说(2) 参考试题
  9. Mybatis日志实现
  10. 雷达系统与信号处理概述(一)
  11. 全栈开发实战(二)——简易博客社区前端搭建教程(附源码)
  12. dataframe保存为txt_Python读写txt文本(示例说明)
  13. MySQL获取汉字的拼音首字母并过滤字母符号数字
  14. excel多列合并关联数据
  15. Task watchdog got triggered错误
  16. CAD打印线条太粗、线条颜色设置
  17. 【题目泛做】哲学题(DFS序)(Splay)
  18. 机械革命无线网消失解决办法
  19. wangeditor支持图片和视频上传
  20. axure能做剪切蒙版吗_UI设计师扔掉PS,使用Axure是怎样一种体验?

热门文章

  1. 提高你css技能的css开发技巧
  2. IOS Window窗口使用
  3. 学习日常笔记day12jsp基础
  4. 利用SQL语句自动生成序号的两种方式
  5. vs2010 C#链接 ACCESS数据库
  6. 大数据之-Hadoop之HDFS_HDFS的优缺点---大数据之hadoop工作笔记0049
  7. Configuration property name ‘fdfs.thumbImage‘ is not valid---springcloud工作笔记163
  8. OAuth2.0_授权服务配置_授权码模式_Spring Security OAuth2.0认证授权---springcloud工作笔记144
  9. 即时通讯学习笔记002---xmpp基本概念
  10. C#.NET验证码智能识别学习笔记---01C#.NET验证码识别介绍