Spring Cloud Gateway Server MVC- Can we use spring mvc with spring cloud gateway – Spring

by
Alexei Petrov
java spring spring-boot spring-cloud-gateway spring-mvc

Quick Fix: To use Spring MVC with Spring Cloud Gateway, leverage the Spring Cloud Gateway MVC module, define routes using ‘RouterFunction’, and utilize classes like ‘RouteLocator’ and ‘GatewayFilterFactory’.

The Problem:

Help comprehend the purpose of Spring Cloud Gateway Server MVC and using it instead of Spring Cloud Gateway Reactive.

The Solutions:

Solution 2: Spring Cloud Gateway Server MVC

Spring Cloud Gateway Server MVC provides a non-reactive alternative to Spring Cloud Gateway’s Webflux-based implementation. It allows you to use Spring MVC to handle HTTP requests and forward them to downstream services.

To use Spring Cloud Gateway Server MVC, you must include the following dependency in your project’s Maven or Gradle configuration:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gateway-server-mvc</artifactId>
</dependency>

Additionally, ensure that the following configuration is present in your project’s application.properties or application.yml file:

spring.cloud.gateway.mvc.enabled=true

With these configurations in place, you can create MVC controllers to handle HTTP requests and forward them to downstream services. For example, the following controller forwards requests to the “/api” path to the “http://localhost:8081” service:

@RestController
public class GatewayController {

    @Autowired
    private ProxyExchangeService proxyExchangeService;

    @GetMapping("/api/**")
    public ResponseEntity<Object> proxy(@PathVariable String path) throws Exception {
        ProxyExchange<Object> proxyExchange = proxyExchangeService.getProxyExchange(PathUtils.normalizePath("/api/" + path));
        return proxyExchange.get();
    }
}

Ensure that you use the correct ProxyExchangeService based on your application. For Spring Boot 2.6 or higher, the recommended implementation is the ReactorProxyExchangeService. For Spring Boot 2.5 or lower, the legacy ProxyExchangeService is recommended.

Please note that Spring Cloud Gateway Server MVC is a non-reactive alternative, and it is not compatible with Spring MVC (spring-boot-starter-web). This is detailed in [How to include Spring Cloud Gateway in the official reference documentation][1].

Therefore, it is important to remove the dependency on spring-boot-starter-web if you are using Spring Cloud Gateway Server MVC. You can do this by excluding it in your Maven or Gradle configuration:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <scope>provided</scope>
</dependency>

With these changes, you can use Spring Cloud Gateway Server MVC in your application to forward HTTP requests to downstream services using Spring MVC controllers.

Solution 3: Using Spring MVC with Spring Cloud Gateway

Spring Cloud Gateway provides an alternative implementation for the gateway router based on Spring MVC. This allows for synchronous request processing, making it a suitable option for applications that require traditional request/response handling. To use Spring MVC with Spring Cloud Gateway, follow these steps:

  1. Add the Spring Cloud Gateway MVC Dependency:

Add the following dependency to your project’s pom.xml file:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
</dependency>
  1. Configure Gateway Routing:

In your Spring Boot application class, create a RouterFunction bean to define the gateway routes. The following example shows a gateway route that forwards requests to a remote service:

@Bean
public RouterFunction<ServerResponse> getRoute() {
    return route().GET("/demo", http("http://localhost:8080/")).build();
}
  1. Run the Application:

Start your Spring Boot application as you usually would. The gateway server will be up and running, handling requests according to the configured routes.

Additional Notes:

  • The Spring Cloud Gateway MVC implementation uses Spring’s Servlet API for request handling, making it compatible with existing Spring MVC applications.
  • You can use the same configuration for authentication, security, and other features as you would with Spring MVC.
  • Spring Cloud Gateway MVC is a good choice for building synchronous gateways that integrate with non-reactive services. However, it might not be the best option for high-performance applications requiring scalability and low latency.

By following these steps, you can set up a Spring Cloud Gateway server using Spring MVC for synchronous request processing. This approach provides a traditional request/response programming model, allowing you to easily integrate with existing Spring MVC applications and services.

Q&A

Differences between using Spring Cloud gateway reactive and Spring Cloud gateway MVC.

Spring Cloud Gateway MVC is an alternative to Spring Cloud Gateway Reactive.Spring Cloud Gateway MVC uses classes such as RouterFunction to define routes, while Spring Cloud Gateway Reactive uses methods and classes such as Mono, Flux, ServerWebExchange, GatewayFilterFactory, and RouteLocator.

Can Spring cloud gateway MVC be used on the place of spring cloud gateway reactive

Yes, Spring Cloud Gateway MVC can be used in place of Spring Cloud Gateway Reactive. They both achieve the same goal, but they use different implementation approaches.

Video Explanation:

The following video, titled "Spring Cloud and the new MVC.fn-style Spring Cloud Gateway with ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Hi, Spring fans! In this installment, we talk to the legendary Spring Cloud cofounder and lead Spencer Gibb (@spencerbgibb)