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?

Recommend

Why do you need to master the data center structure diagram?

The computer room of a data center often encounte...

Ten features of IPv6 that are superior to IPv4

It is 2019, and there is a serious problem that b...

...

The cutting-edge of Internet technology - a comprehensive analysis of TSN

[[271597]] TSN is the abbreviation of time-sensit...

...

5G services market expected to exceed $919.4 billion by 2031

According to a recent report by Transparency Mark...

Tell you the real strength of the four major communication operators' 5G

Recently, our country has determined the 2020 &qu...

RackNerd "Memorial Day" Sale: Los Angeles VPS from $14.99 per year

RackNerd has launched a Memorial Day promotion, w...