Still using OpenFeign? Try this new thing in SpringBoot3!

Still using OpenFeign? Try this new thing in SpringBoot3!

The New Year is over, and Brother Song has been moving bricks for three days.

After the epidemic was relaxed, this year was particularly relaxing and comfortable. A particularly depressing thing in my heart was removed. I saw in the news that there were tourists from all over the country and the Tang Dynasty City in Xi'an was crowded with tourists. It really seemed like I was back in 2019. My friends in the circle were all happy, and life was still very good.

I haven't posted a technical article for a long time. I recently returned to work and have time to code technology again in the evening. Today we will talk about a new thing in Spring Boot3, declarative HTTP calls.

1. Origin

Spring Boot 3 was officially released at the end of last year, and I have also had a taste of it. Recently, I will have time to talk to my friends about what new things Spring Boot 3 has brought us.

Today we will first take a look at the declarative HTTP interface.

Friends who have used Spring Cloud know that in the Spring Cloud family, we can use RestTemplate or OpenFeign to be responsible for inter-process communication (of course there are other ways such as message-driven microservices based on message middleware or gRPC-based calls, etc.).

We can treat RestTemplate as an ordinary HTTP calling tool. Different from other HTTP clients, RestTemplate is particularly convenient for calling RESTful-style interfaces.

However, OpenFeign is more convenient than RestTemplate. Remote calls can be implemented through interface declarations. Song Ge has talked about the specific usage in the previous video, so I will not repeat it here.

In the past, if we wanted to use declarative HTTP calls, we had to use OpenFeign, which required third-party dependencies. Starting from Spring 6 (Spring Boot 3), Spring itself provides similar functions, which can also easily implement declarative HTTP calls through the @HttpExchange annotation. In the future, there will be another option for cross-service calls.

2. Use

Next, Songge will use a case to demonstrate the specific usage of @HttpExchange annotation.

First, we create a common Spring Boot project named server. In this common Spring Boot project, we only need to provide a simple test interface, as follows:

 @RestController
public class HelloController {

@GetMapping ( "/server/hello" )
public String hello ( String name ) {
return "hello " + name ;
}

}

This should not be difficult for everyone, so I won’t say much about it.

Now suppose I have another service named client, and I want to call the interface provided by the server in the client.

First, let's create the client project. Please note that when creating it, we need to add not only Web dependencies, but also Reactive Web, because the underlying layer of @HttpExchange is based on WebClient, which is provided by Reactive Web:

After the creation is complete, we can then declare the Http interface:

 @HttpExchange ( "/server" )
public interface ToDoService {
@GetExchange ( "/hello" )
String hello ( @RequestParam String name ) ;
}

These usages are very similar to @RequestMapping and @GetMapping that we commonly use in SpringMVC:

  • @HttpExchange​ is similar to @RequestMapping. It can be placed on a class to narrow the request, or it can be placed on a method. We can use the method attribute to specify the specific request method, which is also similar to @RequestMapping: @HttpExchange(value = "/server", method = "GET").
  • @GetExchange​ is similar to @GetMapping, which will not be elaborated on here. Other similar annotations include @DeleteExchange, @PatchExchange, @PostExchange, @PutExchange, etc.
  • Another thing to note is that the parameters of the request method need to be annotated with @RequestParam, which is similar to OpenFeign.

After the interface is declared, it is not over yet. We still need to configure it before we can use it. As follows:

 @Configuration
public class WebConfig {
@Bean
WebClient webClient ( ) {
returnWebClient.builder ( )
.baseUrl ( "http://localhost:8080" )
.build ( ) ;
}
@Bean
ToDoService toDoService ( ) {
HttpServiceProxyFactory httpServiceProxyFactory =
HttpServiceProxyFactory .builder ( WebClientAdapter .forClient ( webClient ( ) ) )
.build ( ) ;
return httpServiceProxyFactory .createClient ( ToDoService .class ) ;
}
}

This configuration mainly has two aspects:

  1. @HttpExchange​ is based on WebClient, so we first need to configure WebClient. When configuring WebClient, we also configure the specific address of the request (because in @HttpExchange
  2. Since the ToDoService we provided earlier is an interface, we also need to provide an implementation class of this interface. Of course, this configuration is completely routine and templated, so there is nothing much to say about it.

After all the configurations are completed, we can then directly inject the ToDoService instance wherever we need it. Here is a simple example for your reference:

 @SpringBootTest
class ClientApplicationTests {

@Autowired
ToDoService toDoService ;

@Test
void contextLoads ( ) {
String hello = toDoService .hello ( "javaboy" ) ;
System .out .println ( "hello = " + hello ) ;
}

}

Okay, here is a simple example, you may want to try it out.

In the future, declarative service calls can be implemented without OpenFeign~

<<:  my country's mobile IoT connections account for 70% of the world's total, with "things" connections rapidly surpassing "people" connections

>>:  Do you know the origin and function of Wi-Fi?

Blog    

Recommend

A brief history of computer networks

The development of computer networks has come a l...

Accelerating the development of edge computing

5G is being rolled out faster than any previous g...

Several steps and tools for troubleshooting the network

Let me ask a question first. If one day you sudde...

Understanding WiFi 6 Features for Wave 1 and Wave 2

The rollout of Wi-Fi 6 will consist of two waves ...

Smart Home: Which One Wins, 5G or WiFi 6?

Smart homes are becoming an increasingly importan...

Overview of the five major 5G wireless technologies

Two of the five most important wireless technolog...

What you don’t know about blockchain is quietly subverting banks, BAT

Fintech is a phenomenal concept. With the rapid d...

...

Industry Observation: 6G will mainly become an industrial IoT network

Cellular positioning technology for key IoT indus...