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。

最新文章

  1. 有了jsRender,妈妈再也不用担心我用jq拼接DOM拼接的一团糟了、页面整齐了、其他伙伴读代码也不那么费劲了
  2. 【jQuery】【转】jQuery中的trigger和triggerHandler区别
  3. 纯代码自定义不等高cell
  4. MySQL数据库优化技术之SQL语句慢查询定位
  5. badge ionic tab
  6. 【sgu282】Isomorphism
  7. php的curl获取https加密协议请求返回json数据进行信息获取
  8. WebDriver使用IE浏览器
  9. CodeForces 510B DFS水题
  10. Problem 2. number题解
  11. windows微信双开
  12. ssm项目导入activiti依赖后jsp页面el表达式报错
  13. visual studio 2017下载地址
  14. ACM10.14题解
  15. ubuntu14.04 anaconda tensorflow spyder(python3.5) + opencv3
  16. python 获取subprocess进程执行后返回值
  17. python至winreg模块
  18. 201621123001 《Java程序设计》第6周学习总结
  19. vue 下实现 echarts 全国到省份的地图下钻
  20. 关于bfs时间轴

热门文章

  1. 放大镜Demo
  2. 7、Redis中对ZSet类型的操作命令
  3. Oracle 取整函数
  4. Java对象池
  5. Redis源代码分析(三十五)--- redis.c服务端的实现分析(2)
  6. Delphi开发环境中应用层网络协议的实现
  7. I/O会一直占用CPU吗?【转载】
  8. IOS开发之——objective-c与javascript交互
  9. SharePoint 获取详细Log信息
  10. 嗜血法医第八季/全集Dexter 8迅雷下载