Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This series code address: github.com/JoJoTec/spr…
Origin and implementation of OpenFeign
In microservice systems, we often make RPC calls. In the Spring Cloud architecture, RPC calls are generally HTTP calls. For each invocation, the following steps are followed:
- Find the list of microservice instances and select one
- Call parameter serialization
- The request is sent out using an Http client
- Response processing, deserialization, and so on
In addition to the common logic, the business only needs to define parameters, HTTP methods, HTTP URIs, and responses, that is, using interfaces:
interface HttpBin {
@Get(uri = "/get")
String get(@Param("param") String param);
}
Copy the code
For example, this interface defines an HTTP request with the HTTP method GET, the path/GET, the parameter param, and the response String. After defining the common logic, you can use this interface to make calls.
For the implementation design of these common logic, it is natural to think of aspects and dynamic proxies. In previous chapters, we mentioned the JDK dynamic proxy for interface, is actually implement the Java. Lang. Reflect. InvocationHandler then aimed at the interface to realize the proxy class. Then make calls using this proxy class to walk into the logic defined in InvocationHandler.
These are the design and implementation ideas and uses of OpenFeign.
OpenFeign profile
OpenFeign is an HTTP request client that is defined declaratively (through class metadata definitions, such as annotations, etc.). This library allows you to annotate and automatically generate clients that call the corresponding HTTP service. Calling the remote service looks like calling a local service method. OpenFeign supports a variety of HTTP annotations, including Feign annotations and JAX-RS annotations, and can be configured to support different kinds of annotations in a plugin-like form. Encoders, decoders, can also be configured to encode requests and decode responses. The underlying HTTP Client can also be configured. You can use Java native HTTP links, Apache HttpClient, OkHttpClient, etc.
OpenFeign is still iterating on its RoadMap and can be viewed with this link. Currently we are using OpenFeign 11. Features currently implemented or planned include:
- Response caching, support for in-process and cross-process response caching (in implementation)
- Better URI template support (in implementation)
- Refactoring the Logger logging API (in implementation)
- Refactoring the Retry Retry API (in implementation)
- Collection index related API (to be implemented in the next step)
- through
CompletableFuture
Implement the asynchronous API as a base class (basic implementation is available now, full implementation next) - Reactive APIS (next step)
- Circuit breaker support (planned)
OpenFeign is basically used
Let’s take a look at using OpenFeign without worrying about how to use it in the Spring Cloud environment to better understand the underlying principles. Using OpenFeign alone consists of the following steps:
- Define the remote HTTP call API interface
- Create the HTTP call interface implementation for the Feign proxy
- The invocation is made using a proxy class
Examples are:
Interface GitHub {/** * define the get method, including path parameters, Response returns serialization classes * @param owner * @Param Repository * @return */ @Requestline ("GET /repos/{owner}/{repo}/ polymorphism ") List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repository); /** * Contributor class */ class Contributor {String Contributor; 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) {GitHub GitHub = feign.builder ( FastJsonDecoder. Decoder(new FastJsonDecoder())) The base address is https://api.github.com. Target (GitHub. Class, "https://api.github.com"); List<GitHub.Contributor> contributors = github.contributors("OpenFeign", "feign"); } /** * static class implements Decoder {@override public Object decode(Response response, Type type) throws IOException, DecodeException, [] body = response.body().asinputStream ().readallBytes (); FeignException {// Read body byte[] body = response.body().asinputStream ().readallBytes (); return JSON.parseObject(body, type); }}Copy the code
In the example above, we define the access to GET https://api.github.com/repos/ {owner} / {‘} / contributors OpenFeign client in this interface, and custom response decoder, deserialize the response body. This is the basic use of OpenFeign.
This section details OpenFeign’s design RoadMap and RoadMap. With that in mind, we’ll look at OpenFeign in more detail to understand some of the design and use RoadMap. Some of the refactoring features need to be paid attention to, but there is no need to worry, because all of the OpenFeign features in Spring Cloud are implemented by adding glue project dependencies, and the underlying API refactoring is what glue project needs to care about.
Wechat search “my programming meow” public account, a daily brush, easy to improve skills, won a variety of offers