Spring cloud upgrade 2020.0.x version - 25. Introduction and use of openfeign

Code address of this series: https://github.com/JoJoTec/spring-cloud-parent

The origin and implementation of OpenFeign

In the microservice system, we often make RPC calls. In Spring Cloud system, RPC call is generally the call of HTTP protocol. For each call, the basic steps are as follows:

  • Find the list of microservice instances and select an instance
  • Call parameter serialization
  • Send the request using Http client
  • Response processing, deserialization, etc
    In addition to these common logics, you only need to define parameters, HTTP methods, HTTP URI s and responses in business, that is, you can define them using interfaces:
interface HttpBin {
    @Get(uri = "/get")
    String get(@Param("param") String param);

For example, the above interface defines an HTTP request. The HTTP method is get, the path is / get, the parameter is param, and the response is of String type. After that, as long as the common logic is defined, it can be called using this interface.

For the implementation design of these common logic, we can naturally think of aspects and dynamic agents. In the previous chapter, we mentioned that there is a dynamic proxy for the interface in the JDK, which actually implements java.lang.reflect.InvocationHandler, and then implements the proxy class for this interface. After that, you can use this proxy class to call and walk into the logic defined in InvocationHandler.

The above is the design and implementation idea and purpose of OpenFeign.

Introduction to OpenFeign

OpenFeign is an HTTP request client defined based on declarative (through class metadata definitions, such as annotations, etc.). This library allows you to automatically generate the client that calls the corresponding HTTP service through annotations. From the code point of view, calling this remote service is the same as calling the local service method. OpenFeign supports a variety of HTTP annotations, including Feign annotations and JAX-RS annotations, and can support different kinds of annotations by configuring plug-ins. At the same time, the encoder and decoder can be configured to encode the request and decode the response. The underlying HTTP Client can also be configured. You can use Java Native HTTP links, Apache HttpClient, OkHttpClient, etc.

At present, OpenFeign is still in iterative update, which can be accessed through This link View the current RoadMap. At present, we use OpenFeign 11. The features in the current implementation or plan include:

  • Response cache, supporting in-process or cross process response cache (under implementation)
  • Implement more complete URI template support (under implementation)
  • Refactoring Logger log API (under implementation)
  • Refactoring the Retry API (under implementation)
  • API related to collection indicators (to be implemented in the next step)
  • Use completabilefuture as the basic class to implement asynchronous API (currently there is a basic implementation, and the next step is to fully implement it)
  • Responsive API (to be implemented in the next step)
  • Circuit breaker related support (planned)

OpenFeign basic use

Let's first look at the use of OpenFeign. We don't care how to use it in the Spring Cloud environment, so we can better understand its underlying principle. Using OpenFeign alone can be divided into the following steps:

  1. Define remote HTTP call API interface
  2. Create the HTTP call interface implementation of Feign proxy
  3. Call with proxy class

Specific examples are:

interface GitHub {
     * Define get methods, including path parameters, and return serialized classes in response
     * @param owner
     * @param repository
     * @return
    @RequestLine("GET /repos/{owner}/{repo}/contributors")
    List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repository);

     * Responder structure class
    class Contributor {
        String login;
        int contributions;

        public Contributor() {

        public String getLogin() {
            return login;

        public void setLogin(String login) {
            this.login = login;

        public int getContributions() {
            return contributions;

        public void setContributions(int contributions) {
            this.contributions = contributions;

public static void main(String[] args) {
    //Create the HTTP call interface implementation of Feign proxy
    GitHub github = Feign.builder()
                        //Specify the decoder as FastJsonDecoder
                        .decoder(new FastJsonDecoder())
                        //Specify that the proxy class is GitHub and the base address is https://api.github.com
                        .target(GitHub.class, "https://api.github.com");
    List<GitHub.Contributor> contributors = github.contributors("OpenFeign", "feign");

 * Deserialization decoder based on fastjason
static class FastJsonDecoder implements Decoder {
    public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
        //Read body
        byte[] body = response.body().asInputStream().readAllBytes();
        return JSON.parseObject(body, type);

In the above example, we defined access to get https://api.github.com/repos/ The OpenFeign client of the {owner} / {repo} / contributors interface has customized the response decoder and deserialized the response body. This is the basic use of OpenFeign.

In this section, we introduce the design ideas of Openfeign and RoadMap in detail. After understanding these, we will analyze Openfeign in detail to understand some of its design and use ideas. In addition, we need to pay special attention to some features in refactoring, but we don't have to worry, because the features of using Openfeign in Spring Cloud are implemented by adding glue project dependencies, and the underlying API refactoring is something that the glue project needs to care about.

WeChat search "my programming meow" attention to the official account, daily brush, easy to upgrade technology, and capture all kinds of offer:

Tags: Java Spring Cloud feign

Posted on Sun, 03 Oct 2021 17:04:21 -0400 by fahrvergnuugen