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.

image

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.

image

Get Started Quickly

image

Adding dependencies

Since spring-cloud-square is not officially released yet, you need to configure the spring maven repository.

1
2
3
4
5
6
<repositories>
  <repository>
    <id>spring-milestones</id>
    <url>https://repo.spring.io/milestone</url>
  </repository>
</repositories>

maven dependencies

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-square-okhttp</artifactId>
  <version>${square.version}</version>
</dependency>
<dependency>
  <groupId>com.squareup.okhttp3</groupId>
  <artifactId>okhttp</artifactId>
  <version>${okhttp.version}</version>
</dependency>

<!--Add load balancing support-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

Configuration

1
2
3
4
5
@Bean
@LoadBalanced
public OkHttpClient.Builder okHttpClientBuilder() {
    return new OkHttpClient.Builder();
}

Remote Calling

As with the earliest ribbon calls, it’s very simple.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@Autowired
OkHttpClient.Builder builder;

@GetMapping
public String req() {
    Request request = new Request.Builder()
            .url("http://square-provider/req").build();
    Response response = builder.build().newCall(request).execute();
    return response.body().string();
}

Advanced Usage

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

Adding dependencies

1
2
3
4
5
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-square-retrofit</artifactId>
  <version>${square.version}</version>
</dependency>

Statement calling client

1
2
3
4
5
6
@RetrofitClient("square-provider")
public interface DemoService {

    @GET("/")
    Call<String> req();
}

EnableRetrofitClients

1
@EnableRetrofitClients

Remote Calling

1
2
3
4
5
6
7
8
@Autowired
DemoService demoService;

@SneakyThrows
@GetMapping("/retrofit")
public String retrofit(){
    return demoService.req().execute().body();
}

Summary

    1. spring-cloud-square is directly based on retrofit implementation, the overall source code is very simple, we recommend you to read it.
    1. The current version does not implement fallback-related implementations yet.
    1. the supporting code of this article https://github.com/lltx/spring-cloud-square-demo

Reference https://segmentfault.com/a/1190000039847042