Kill Feign, Spring Cloud Square component release

Speaking of the Spring Cloud ecology, everyone must be familiar with Feign. As shown in the figure below, Feign can hide the request of the bottom layer (okhttp, httpclient) Rest and pretend to be a Controller similar to SpringMVC. You don’t need to splice urls, splice parameters and so on by yourself, leave it to Feign to do everything. Using Feign to call an API is like calling a local method, which avoids the cumbersome need to constantly parse/encapsulate json data when calling target microservices.

The Spring Cloud Square project aims to replace the original Spring Cloud Feign, and use Retrofit to encapsulate the underlying communication library to implement cross-service calls. It has been incubated in the spring-cloud-incubator incubator (the last one is in the incubator spring-cloud-loadbalancer Has officially replaced Ribbon as an official recommended component).

Before understanding Spring Cloud Square, you need to understand the following components

  1. OkHttp is a third-party library for network requests, which encapsulates the underlying implementation of get, post and other operations of network requests. It is one of the most popular network request frameworks.
  2. Retrofit is a RESTful HTTP network request framework, which is based on OkHttp. It configures network parameters through annotations, supports a variety of data parsing and serialization (Gson, Json, Xml, etc., and also supports RxJava.

Then the service call based on Spring Cloud Square can be abstracted as shown in the figure below

1. Get started quickly

1.1 Add dependency

Since spring-cloud-square has not been officially released, the spring maven repository needs to be configured.

<repositories>
  <repository>
    <id>spring-milestones</id>
    <url>https://repo.spring.io/milestone</url>
  </repository>
</repositories>

maven dependency.

<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>

1.2 Code configuration

@Bean
@LoadBalanced
public OkHttpClient.Builder okHttpClientBuilder() {
    return new OkHttpClient.Builder();
}

1.3 Code call

Like the earliest ribbon call, it is very simple.

@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();
}

2. Advanced use

As an alternative to Spring Cloud Feign, square also supports the form of declarative client. Note that the following code has the same taste as feign.

2.1 Add dependency

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-square-retrofit</artifactId>
  <version>${square.version}</version>
</dependency>

2.2 Declare the calling client

@RetrofitClient("square-provider")
public interface DemoService {

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

2.3 Turn on client scanning

@EnableRetrofitClients

2.4 Code call

@Autowired
DemoService demoService;

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

Leave a Reply