Spring Cloud 2023 supports synchronization gateway, one of the most eye-catching new features

picture

I. Introduction

One of the most notable new features in the Spring Cloud 2023 release is support for sync gateways. Synchronous gateway is a new gateway implementation that guarantees the order of requests. In the traditional microservice architecture, different services usually communicate through the HTTP protocol. This communication method is non-blocking, which means that a service can return immediately after sending a request without waiting for a response. But in some scenarios, we need to ensure the order of requests, such as order processing, financial transactions, etc. At this time we need to use a synchronization gateway to achieve this requirement.

The synchronization gateway ensures the order of requests by introducing a global lock, and can also support asynchronous processing of requests. Specifically, when a request enters the synchronization gateway, it will first try to acquire the lock. If the acquisition is successful, it will process the request and return a response; if the acquisition fails, it will wait for a period of time and try again. This method can ensure that requests are processed in order, and can also avoid data inconsistency problems caused by network delays and other reasons.

2. Version requirements

The minimum version requirements are Java 17, Jakarta EE 9, Spring Framework 6.x, Spring Boot 3.x.

3. Code implementation

The implementation of synchronization gateway depends on the Spring Cloud Gateway component. Spring Cloud Gateway is an API gateway based on Spring Framework 5.x and Project Reactor, which provides rich functionality and extensibility. In Spring Cloud Gateway, the synchronization gateway is enabled through the @EnableDiscoveryClient annotation. This annotation indicates that the service discovery client function is enabled, allowing the application to register its own service information with the registration center. At the same time, the @EnableCircuitBreaker annotation indicates that the fuse function is turned on, and fuse protection is automatically performed when the service fails.

Here is an example using a sync gateway:

@EnableDiscoveryClient
@EnableCircuitBreaker
@SpringBootApplication
public class OrderServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }
}

In this example, we first use the @EnableDiscoveryClient annotation to enable the service discovery client function, allowing the application to register its own service information with the registration center; then use the @EnableCircuitBreaker annotation to turn on the circuit breaker function, which will automatically occur when the service fails. Fuse protection. Finally, use the @SpringBootApplication annotation to start a Spring Boot application.

When we call the OrderServiceApplication class, Spring Boot will automatically create an OrderServiceApplication instance and run its run() method. In the run() method, Spring Boot scans all classes that implement the CommandLineRunner, CommandLineRunnerAware, ApplicationContextAware, and ApplicationListener interfaces and injects them into the OrderServiceApplication instance respectively. This way we can add custom functionality and logic to these classes.

4. Conclusion

The above is an introduction to the new feature of supporting synchronization gateway in Spring Cloud 2023 version. Synchronization gateway can help us better control the order of requests and avoid data inconsistency problems caused by non-blocking communication methods. Of course, this new feature also requires us to carefully consider various situations and constraints during design and implementation to ensure the stability and reliability of the system.

5. Architecture diagram

5.1 Microservice system architecture diagram

5.2 Microservice Architecture Skills Map

picture

Guess you like

Origin blog.csdn.net/weixin_40381772/article/details/133787966