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": 15000
endpointConfig:
GithubConnector:
default:
url: https://api.github.com
readTimeoutMillis: 8000
connectTimeoutMillis: 5000
getRepos:
url: https://api.github.com
readTimeoutMillis: 15000
connectTimeoutMillis: 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.default
final 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.getRepos
final 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

    我们知道 SpringBoot 提供了很多的 Starter 用于引用各种封装好的功能: 名称 功能 spring-boot-starter-web 支持 Web 开发,包括 Tomcat 和 spr ...

  2. 小D课堂 - 零基础入门SpringBoot2.X到实战_第7节 SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf_28..SpringBoot Starter讲解

    笔记 1.SpringBoot Starter讲解     简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-b ...

  3. SpringBoot Starter机制 - 自定义Starter

    目录 前言 1.起源 2.SpringBoot Starter 原理 3.自定义 Starter 3.1 创建 Starter 3.2 测试自定义 Starter 前言         最近在学习Sp ...

  4. 自定义springboot - starter 实现日志打印,并支持动态可插拔

    1. starter 命名规则: springboot项目有很多专一功能的starter组件,命名都是spring-boot-starter-xx,如spring-boot-starter-loggi ...

  5. SpringBoot starter 作用在什么地方?

    依赖管理是所有项目中至关重要的一部分.当一个项目变得相当复杂,管理依赖会成为一个噩梦,因为当中涉及太多 artifacts 了. 这时候 SpringBoot starter 就派上用处了.每一个 s ...

  6. SpringBoot Starter缘起

    SpringBoot通过SpringBoot Starter零配置自动加载第三方模块,只需要引入模块的jar包不需要任何配置就可以启用模块,遵循约定大于配置的思想. 那么如何编写一个SpringBoo ...

  7. 如何使用SpringBoot封装自己的Starter

    作者:Sans_ juejin.im/post/5cb880c2f265da03981fc031 一.说明 我们在使用SpringBoot的时候常常要引入一些Starter,例如spring-boot ...

  8. SpringBoot封装自己的Starter

    https://juejin.im/post/5cb880c2f265da03981fc031 一.说明 我们在使用SpringBoot的时候常常要引入一些Starter,例如spring-boot- ...

  9. SpringBoot - Starter

    If you work in a company that develops shared libraries, or if you work on an open-source or commerc ...

随机推荐

  1. 轻量级IOC框架:Ninject (上)

    前言 前段时间看Mvc最佳实践时,认识了一个轻量级的IOC框架:Ninject.通过google搜索发现它是一个开源项目,最新源代码地址是:http://github.com/enkari/ninje ...

  2. Java怎样处理EXCEL的读取

    须要包:poi-3.5.jar.poi-ooxml-3.5.jar 实例: [java] view plaincopy public class ProcessExcel { private Work ...

  3. LPC1800 and LPC4300 Boot/ISP/CRP

    MCU的启动方式有很多种:UART接口,扩展的静态存储单元(NOR Flash), SPI Flash,quad SPI Flash,高速USB0和USB1.另外可以通过对OTP存储单元的编程. 首先 ...

  4. windows和linux 下将tomcat注册为服务

    参考文献: tomcat注册成windows服务 背景 当前项目需要运行两个Tomcat,每次启动系统以后都要手动进入到tomcat目录执行startup.bat,非常烦,所以想将这两个tomcat直 ...

  5. Go语言中查询SqlServer数据库

    一.Go语言中查询MsSQL数据库: // main.go package main import ( "database/sql" "fmt" "l ...

  6. [Go] 单元测试/性能测试 (go test)

    特征 Golang 单元测试对文件名和方法名,参数都有很严格的要求.例如: 1.文件名必须以 _test.go 结尾 2.方法名必须是 Test 开头 3.方法参数必须是 t *testing.T 或 ...

  7. 在ASP.NET MVC中使用Castle Windsor

    平常用Inject比较多,今天接触到了Castle Windsor.本篇就来体验其在ASP.NET MVC中的应用过程. Visual Studio 2012创建一个ASP.NET MVC 4网站. ...

  8. 用DELPHI 开发压缩、解压、自解压、加密

    引 言:在日常中,我们一定使用过WINZIP.WINRAR这样的出名的压缩软件,就是我们开发软件过程中不免要遇到数据加密.数据压缩的问题!本文中就这一技术问题展开探讨,同时感谢各位网友的技巧,在我每次 ...

  9. Javascript 身份证号获得出生日期、获得性别、检查身份证号码

    //---------------------------------------------------------- // 功能:根据身份证号获得出生日期 // 参数:身份证号 psidno // ...

  10. 【IntelliJ IDEA】idea或者JetBrains公司所有编辑器,设置其软件的字体样式

    操作如下: 修改完成后的效果: 可以看到修改以后的ide的效果: