[Calling Third-party API - GitHub OpenFeign] Feign makes writing java http clients easier

Feign is a Java to HTTP client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign's first goal was reducing the complexity of binding Denominator uniformly to HTTP APIs regardless of ReSTfulness...

Posted by Mr.Vincent on 2020-11-18
Estimated Reading Time 4 Minutes
Words 968 In Total
Viewed Times

OpenFeign 简介

Feign 是一个声明式的 Web Service 客户端。它的出现使开发 Web Service 客户端变得很简单。使用 Feign 只需要创建一个接口加上对应的注解,比如:FeignClient 注解。Feign 有可插拔的注解,包括 Feign 注解和 JAX-RS 注解。Feign 也支持编码器和解码器,Spring Cloud Open Feign 对 Feign 进行增强支持 Spring MVC 注解,可以像 Spring Web 一样使用 HttpMessageConverters 等。Feign 是一种声明式、模板化的 HTTP 客户端。在 Spring Cloud 中使用 Feign,可以做到使用 HTTP 请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问 HTTP 请求。

OpenFeign 官网:https://github.com/OpenFeign/feign

这是一张由 feign 提供的当前主要功能的图:

feign

OpenFeign 的功能特点

  • 可插拔的注解支持,包括 Feign 注解和 JAX-RS 注解。
  • 支持可插拔的 HTTP 编码器和解码器(Gson,Jackson,Sax,JAXB,JAX-RS,SOAP)。
  • 支持 Hystrix 和它的 Fallback。
  • 支持 Ribbon 的负载均衡。
  • 支持 HTTP 请求和响应的压缩。
  • 灵活的配置:基于 name 粒度进行配置
  • 支持多种客户端:JDK URLConnection、apache httpclient、okhttp,ribbon)
  • 支持日志
  • 支持错误重试
  • url支持占位符
  • 可以不依赖注册中心独立运行

OpenFeign 的主要依赖

OpenFeign 下还有一些子项目:feign-formfeign-jacksonfeign-gsonfeign-slf4j 等。

  • io.github.openfeign.feign-core:核心包。
  • io.github.openfeign.feign-jackson:该模块增加了对通过 Jackson 编码和解码的 JSON 支持。
  • io.github.openfeign.feign-gson:该模块增加了对通过 Gson 库编码和解码的 JSON 支持。
  • io.github.openfeign.feign-form:该模块增加了对 application/x-www-form-urlencodedmultipart/form-data 表单进行编码的支持。
  • io.github.openfeign.feign-slf4j:该模块允许将 Feign 的日志记录定向到 SLF4J,从而使您可以轻松地使用自己选择的日志记录后端(Logback,Log4J等)。

OpenFeign 接口注解

Feign 注释定义关联接口与基础客户端之间应如何工作。Feign 的默认定义了以下注释:

Annotation Interface Target Usage
@RequestLine Method Defines the HttpMethod and UriTemplate for request. Expressions, values wrapped in curly-braces {expression} are resolved using their corresponding @Param annotated parameters.
@Param Parameter Defines a template variable, whose value will be used to resolve the corresponding template Expression, by name.
@Headers Method, Type Defines a HeaderTemplate; a variation on a UriTemplate. that uses @Param annotated values to resolve the corresponding Expressions. When used on a Type, the template will be applied to every request. When used on a Method, the template will apply only to the annotated method.
@QueryMap Parameter Defines a Map of name-value pairs, or POJO, to expand into a query string.
@HeaderMap Parameter Defines a Map of name-value pairs, to expand into Http Headers
@Body Method Defines a Template, similar to a UriTemplate and HeaderTemplate, that uses @Param annotated values to resolve the corresponding Expressions.

OpenFeign 使用实例

POM

pom.xml 配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- feign-core -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>${feign.version}</version>
</dependency>
<!-- feign-jackson -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
<version>${feign.version}</version>
</dependency>
<!-- feign-form -->
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>${feign-form.version}</version>
</dependency>

官网案例

官网案例:https://github.com/OpenFeign/feign/blob/master/README.md#basics

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);

@RequestLine("POST /repos/{owner}/{repo}/issues")
void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);

}

public static class Contributor {
String login;
int contributions;
}

public static class Issue {
String title;
String body;
List<String> assignees;
int milestone;
List<String> labels;
}

public class MyApp {
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 + ")");
}
}
}

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !