Kill feign and release spring cloud square components

Posted by cesar_ser on Fri, 04 Mar 2022 02:48:18 +0100

What is Spring Cloud Square

When talking about Spring Cloud ecology, you must be familiar with Feign. As shown in the figure below, Feign can hide the Rest requests of the underlying (okhttp, httpclient) and pretend to be similar to the Controller of spring MVC. You don't have to splice URLs, splice parameters and other operations by yourself. Let Feign do everything. Using Feign to call API is like calling local methods, which avoids the tedious task of parsing / encapsulating json data when calling the target microservice.

Spring Cloud Square The project aims to replace the original Spring Cloud Feign and realize cross service invocation by encapsulating the underlying communication class library with Retrofit. At present, it has been incubated in the spring cloud incubator (the last spring cloud loadbalancer incubated in the incubator has officially replaced Ribbon as the officially recommended component).

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

  • OkHttp is a third-party class library about network request, which encapsulates the bottom implementation of get, post and other operations of network request. It is one of the most popular network request frameworks at present.

  • Retrofit is a RESTful HTTP network request framework based on OkHttp. It configures network parameters through annotations, supports the parsing and serialization of a variety of data (Gson, Json, Xml, etc.), and also supports RxJava.

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

Get started quickly

Add dependency

  • Since spring cloud square has not been officially released at present, the spring maven warehouse needs to be configured.
  • maven dependency

<!--Add load balancing support-->

Code configuration

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

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

Advanced use

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

Add dependency


Claim calling client

public interface DemoService {

    Call<String> req();

Turn on client scan


Code call

DemoService demoService;

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


    1. Since spring cloud square is directly implemented based on retrofit, the overall source code is very simple. It is recommended that you read it.
    1. The implementation of fallback is not implemented in the current version.
    1. The supporting code of this paper

Topics: Java Spring Spring Boot Spring Cloud