Search code examples
springspring-bootmicroservicesapi-gatewayspring-cloud-gateway

Implementing Spring Cloud Gateway In The Same Project


I provide an api for other microservices in my spring boot microservice and I want to put a spring-cloud-gateway in front of this microservice.

I have reviewed the well-known spring document (https://spring.io/guides/gs/gateway/) but as far as I understand it requires me to launch the cloud gateway in a separate project. But I want to run the RouteLocator bean there in my microservice. Not in a separate project, but in the same project.

When I use it in the same project, I get a warning like this

"Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency."

Later, as he said in the warning, I remove the spring-boot-starter-web dependency, even coming in other projects, excluding them from there like this

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>spring-boot-starter-web</artifactId>
                <groupId>org.springframework.boot</groupId>
            </exclusion>
        </exclusions>
    </dependency>

This time I logically get the error

"org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.company operations.Service operationsApplication]; nested exception is org.spring. .core.NestedIOException: Failed to load class [javax.servlet.Filter]; nested exception is java.lang.ClassNotFoundException: javax.servlet.Filter ".

We deleted the core library from the project that should provide web api service :D

As a result, can I use cloud gateway in the same project? What is the reason and logic for not using it?

If it is not possible to use it in the same project, how should the well-known practices of gateway be, should I put a gateway in front of each api microservice, or should I have a single gateway in my project consisting of more than one microservice?


Solution

  • You can start gateway in the same project, but this is a webflux based project. From documentation

    Spring Cloud Gateway is built on Spring Boot 2.x, Spring WebFlux, and Project Reactor. As a consequence, many of the familiar synchronous libraries (Spring Data and Spring Security, for example) and patterns you know may not apply when you use Spring Cloud Gateway. If you are unfamiliar with these projects, we suggest you begin by reading their documentation to familiarize yourself with some of the new concepts before working with Spring Cloud Gateway.

    and

    Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux. It does not work in a traditional Servlet Container or when built as a WAR.

    so you should have used spring-boot-starter-webflux instead of spring-boot-starter-web.

    Alternatively, if you need to use traditional Spring MVC, consider using Spring Netflix Zuul. This project is currently in maintenance mode and Spring Gateway is the successor of it, but it should work.