What is Spring Cloud Square
Feign is familiar with the Spring Cloud ecosystem. As shown below, Feign can hide the underlying Rest requests (OKHTTP, HttpClient) as a Controller similar to SpringMVC. You don’t have to concatenate urls, concatenate parameters, and so on. Feign does everything. Using Feign to call the API is like calling native methods, eliminating the need to constantly parse/encapsulate JSON data when calling the target microservice.
The Spring Cloud Square project aims to replace Spring Cloud Feign and implement cross-service invocation with Retrofit’s encapsulation of the underlying communication library. It is currently incubated in the Spring-Cloud-Load Balancer incubator (the last one incubated in the Incubator spring-Cloud-load Balancer has officially replaced Ribbon as the official recommendation).
Before you know Spring Cloud Square, you need to know the following components:
-
OkHttp is a third party library about network request, which encapsulates the low-level 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 Web request framework that is based on OkHttp. It is annotated to configure network parameters, supports parsing and serialization of various types of data (Gson, Json, Xml, etc.), and also supports RxJava.
The service invocation based on Spring Cloud Square can be abstracted as follows:
Quick learning
Add the dependent
- Since Spring-Cloud-Square is not officially released, you need to configure the Spring Maven repository.
<repositories>
<repository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
Copy the code
- Maven rely on
<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>
Copy the code
Code configuration
@Bean
@LoadBalanced
public OkHttpClient.Builder okHttpClientBuilder(a) {
return new OkHttpClient.Builder();
}
Copy the code
Code calls
- As with the earliest ribbon calls, it’s very simple.
@Autowired
OkHttpClient.Builder builder;
@GetMapping
public String req(a) {
Request request = new Request.Builder()
.url("http://square-provider/req").build();
Response response = builder.build().newCall(request).execute();
return response.body().string();
}
Copy the code
Use the advanced
Square, an alternative to Spring Cloud Feign, also supports a declarative client form. Note that the following code has the same taste as Feign
Add the dependent
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-square-retrofit</artifactId>
<version>${square.version}</version>
</dependency>
Copy the code
Declare the calling client
@RetrofitClient("square-provider")
public interface DemoService {
@GET("/")
Call<String> req(a);
}
Copy the code
Enable client scan
@EnableRetrofitClients
Copy the code
Code calls
@Autowired
DemoService demoService;
@SneakyThrows
@GetMapping("/retrofit")
public String retrofit(a){
return demoService.req().execute().body();
}
Copy the code
conclusion
-
- Since Spring-Cloud-Square is directly based on retrofit, the overall source code is very simple and recommended to read.
-
- Fallback is not implemented in the current version.
-
- The accompanying code for this article is github.com/lltx/spring…