In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about SpringCloud upgrade 2020.0.x version of OpenFeign introduction and the use of ideas is how, the editor feels very practical, so share to you to learn, I hope you can learn after reading this article, say no more, follow the editor to have a look.
The Origin and implementation of OpenFeign
In micro-service systems, we often make RPC calls. In Spring Cloud system, RPC call is generally the call of HTTP protocol. For each call, you have to go through the following steps:
Find the list of microservice instances and select an instance
Call parameter serialization
Send the request using the Http client
Response handling, deserialization, etc.
In addition to these common logic, the business only needs to define parameters, HTTP methods, HTTP URI, and responses, that is, they can be defined using interfaces:
Interface HttpBin {@ Get (uri = "/ get") String get (@ Param ("param") String param);}
For example, the above interface defines a HTTP request with a HTTP method of GET, a path of / get, a parameter of param, and a response of type String. After that, as long as the common logic is defined, you can use this interface to make the call.
For the implementation design of these common logic, we can naturally think of aspects and dynamic proxies. In the previous section, we mentioned that there are dynamic proxies for interfaces in JDK, which actually implement java.lang.reflect.InvocationHandler and then implement proxy classes for this interface. You can then use this proxy class to make calls to walk into the logic defined in InvocationHandler.
The above are the design and implementation ideas and uses of OpenFeign.
Introduction to OpenFeign
OpenFeign is an HTTP request client that is defined declaratively (through class metadata definitions, such as annotations, etc.). This library allows you to automatically generate a client that invokes the corresponding HTTP service through annotations. From a code point of view, invoking this remote service is the same as calling a 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 them in a form similar to plug-ins. At the same time, encoders and decoders 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, you can also use Apache HttpClient and OkHttpClient and so on.
OpenFeign is currently in the process of iterative updates, and you can view the current RoadMap through this link. Currently we are using OpenFeign 11, and the features in the current implementation or in the plan include:
Response caching, which supports intra-process or cross-process response caching (in implementation)
Implement better URI template support (in implementation)
Refactoring Logger log API (implementing)
Refactoring Retry retry API (implementing)
Collect metrics related API (the next step is to implement)
Use CompletableFuture as the basic class to implement asynchronous API (currently there is a basic implementation, the next step is a complete implementation)
Responsive API (the next step is to implement)
Circuit breaker related support (planned)
Basic use of OpenFeign
Let's first look at the use of OpenFeign, regardless of how it is used in the Spring Cloud environment, so that we can better understand its underlying principles. Using OpenFeign alone is divided into the following steps:
Define a remote HTTP call API interface
Create the HTTP calling interface implementation of Feign proxy
Use the proxy class to call
Specific examples are:
Interface GitHub {/ * defines get methods, including path parameters. Response returns serialization class * @ param owner * @ param repository * @ return * / @ RequestLine ("GET / repos/ {owner} / {repo} / contributors") List contributors (@ Param ("owner") String owner, @ Param ("repo") String repository) / * response body 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 the Feign proxy GitHub github = Feign.builder () / / specify the decoder as FastJsonDecoder .decoder (new FastJsonDecoder ()) / / specify the proxy class as GitHub The base address is https://api.github.com. Target (GitHub.class, "https://api.github.com");" List contributors = github.contributors ("OpenFeign", "feign");} / * * FastJson-based deserialization decoder * / static class FastJsonDecoder implements Decoder {@ Override 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 define an OpenFeign client that accesses the interface GET https://api.github.com/repos/{owner}/{repo}/contributors, customize the response decoder, and deserialize 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, and then we can understand some of the design and use ideas. And we need to pay special attention to some features in refactoring, but don't worry, because the features of using OpenFeign in Spring Cloud are realized by adding glue project dependencies, and the underlying API refactoring is something that glue projects need to care about.
The above is the introduction and implementation of OpenFeign in SpringCloud upgrade version 2020.0.x. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.