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.


maven dependency.


<!-- Add load balancing support -->

1.2 Code configuration

public OkHttpClient.Builder okHttpClientBuilder() {
    return new OkHttpClient.Builder();

1.3 Code call

Like the earliest ribbon call, it is very simple.

OkHttpClient.Builder builder;

public String req() {
    Request request = new Request.Builder()
    Response response =;
    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


2.2 Declare the calling client

public interface DemoService {

    Call<String> req();

2.3 Turn on client scanning


2.4 Code call

DemoService demoService;

public String retrofit(){
    return demoService.req().execute().body();

Leave a Reply