Implementing Spring Cloud Gateway In The Same Project - spring

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?

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.

To add to sawim's answer regarding using Spring MVC, in the case that your project uses spring-boot >=2.4, spring-cloud-netflix-zuul won't be compatible since it is not included with spring-cloud as of version 2020.0.
In that case, a possible alternative would be to setup a proxy using spring-cloud-gateway in an MVC project using spring-cloud-gateway-mvc, which is compatible with spring-boot-starter-web.

Related

Which is the best way for the routs uri in spring boot except zuul and spring cloud gateway

I am upgrading the spring boot 1.3.7.RELEASE to 2.5.12 and spring framework 5.3.18 in my spring boot microservice based project we have upgrade successfully with all service except gateway service when i am unabling t add zuul dependency because its maintenance mode that why we have implemented spring cloud gateway then i am getting below issue.
***************************
APPLICATION FAILED TO START
***************************
Description:
Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway.
Action:
Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency.
Wwhat we need to do implementation for best way?
We have fix the routing issue using the spring cloud gateway.
Please add dependency in the pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
bootstrap.yml
spring:main:web-application-type:reactive
Thanks you guys for supporting.

Spring Boot Confusion (using spring cloud dependency in spring boot framework)

I am new to spring framework. I have a confusion regarding spring boot and spring cloud.
I used https://start.spring.io/ to initialize a spring boot application. I think I am using the spring boot framework. However, I would like to use some spring cloud dependencies such as spring-cloud-stream-binder-kafka.
Question 1: If I added this dependency above to my spring boot application, I am wondering if I still can go with the spring boot framework, or I have to change to spring cloud framework.
Question 2: I am wondering if there is any difference when deploying the spring boot or spring cloud application. Or, they just have the different frameworks, and we could deploy them in the same way.
Thank you so much!
You can use together Spring Boot and Spring Cloud packages.
Spring Boot is just a preconfigured Spring Framework with some extra functionalities. It also uses library versions compatibile with each other. Spring Cloud is also the part of the Spring ecosystem, contains libraries that mostly used in cloud applications.
In the background, these packages will pull all necessary Spring (and other) libraries into your project, as transitive dependencies.
So you can use the generated pom/gradle, and add other dependencies. In this case Spring boot will be your core and cloud add extras.

how to use Spring Cloud Gateway without Reactive stuff?

I want to create a new project with Spring Cloud Gateway but I don't want all the reactive functionality. for me, it will be fine if the other microservice will be blocking I/O and not Reactive.
how can I do that?
let's say I'm implementing cloud gateway as reactive and all the other MS's as blocking, its a good approach? what are the cons of that?
Spring cloud gateway are built on top of spring webflux and netty and this cannot be changed.
From the reference docs:
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.
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.
Spring Cloud Reference Docs
It is perfectly acceptable for a non blocking IO application to make network calls to a blocking IO application. The non blocking IO app will still have all of the benefits of having non blocking IO. It will not consume resources while waiting for responses from network calls to blocking IO applications and in theory should consume less resources and be able to handle more concurrent calls as a result.
Add this :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
then set :
spring.main.web-application-type=reactive
in application.properties

Accessing localhost:8080/actuator endpoint (with spring boot actuator)

In spring docs I have read about endpoint named "actuator" which is provided by actuator dependency, but I haven't managed to access it on my local testing app.
Question: Does someone know how to access that endpoint? of coarse if it is possible :)
Sub-question 1: If this endpoint exists, then why it is hidden?
Sub-question 2: If this endpoint doesn't exist, how can we notify spring.io to correct documentation page (open some kind of ticket) ?
Details:
I want to access exactly "actuator" endpoint, not other endpoints provided by spring boot actuator (localhost:8080/actuator)
Yes, I have tried to enable that endpoint manually in properties file (endpoints.enabled=true OR endpoints.actuator.enabled=true)
Yes, I have tried to enable/disable endpoints.sencitive property
Yes, other endpoints of actuator work just fine
No special reason why I need that, just want to try it out (just learning new stuff :) )
Please don't just answer "there is no such endpoint dude!", there should be some kind of reason why it is written in the docs
Please use spring boot version which I am using now before answering "it is working for me with these configs" (spring boot version: 1.5.4.RELEASE)
Thank you in advance :)
You must include the Spring Hateoas dependency in order for the /actuator endpoint to become available:
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
Per the docs:
Provides a hypermedia-based “discovery page” for the other endpoints.
Requires Spring HATEOAS to be on the classpath.

Spring Cloud Eureka together with RestEasy

we are evaluating Eureka as central SD environment for our Spring Boot applications. Here we are using Spring Cloud. We figured out, that due to the fact, that we make use of RestEasy quite a lot and Eureka is based on Jersey, we run into bigger conflicts. Our rest-easy based APIs are throwing a lot of errors. It's basically no good idea to mix Jersey with Resteasy in the same application.
Question: Is there a way to change the jersey dependency or remove the lib somehow in order to avoid lib problems with RestEasy?
Best
fri
Not currently, it's how the eureka client is built. An alternative might be spring-cloud-consul, which doesn't use eureka, but provides integration with Ribbon and Zuul. Hystrix also works fine.

Resources