Talking about Spring Cloud ecosystem, you must be familiar with Feign, as shown below, Feign can hide the underlying (okhttp, httpclient) Rest requests, disguised as a SpringMVC-like Controller. You don’t have to splice the url, splice the parameters and so on by yourself, everything is left to Feign. Using Feign to call the API is like calling a local method, avoiding the tedium of constantly parsing/wrapping json data when calling the target microservice.
The Spring Cloud Square project aims to replace the original Spring Cloud Feign , with Retrofit’s wrapping of the underlying communication class library encapsulation to achieve cross-service calls, and is currently incubated in the spring-cloud-incubator (the last incubator incubator spring-cloud-loadbalancer has officially succeeded Ribbon as a recommended component).
Before understanding Spring Cloud Square, it is important to understand the following components.
- OkHttp is a third-party class library for network requests, which encapsulates the underlying implementation of get, post, and other operations of network requests, and is one of the hottest frameworks for network requests.
- Retrofit is a RESTful HTTP network request framework, it is based on OkHttp . It is configured through annotations network parameters , support for a variety of data parsing and serialization (Gson, Json, Xml , etc., and RxJava is also supported .
Then the Spring Cloud Square based service calls can be abstracted as shown in the following diagram.
Get Started Quickly
Since spring-cloud-square is not officially released yet, you need to configure the spring maven repository.
As with the earliest ribbon calls, it’s very simple.
As an alternative to Spring Cloud Feign, square also supports a declarative client form. Note the following code which has the same flavor as feign
Statement calling client
- spring-cloud-square is directly based on retrofit implementation, the overall source code is very simple, we recommend you to read it.
- The current version does not implement fallback-related implementations yet.
- the supporting code of this article https://github.com/lltx/spring-cloud-square-demo