Home > Enterprise >  Spring Webflux change from Netty to Tomcat embedded server
Spring Webflux change from Netty to Tomcat embedded server

Time:12-05

I have changed the pom to switch from Netty to Tomcat as below, but now I get : java.lang.IllegalStateException: No suitable default ClientHttpConnector found, why is that? With Netty before the change was all good.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.5</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fer</groupId>
<artifactId>DemoReactive</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DemoReactive</name>
<description>Demo project for Reactive</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>spring-boot-starter-reactor-netty</artifactId>
                <groupId>org.springframework.boot</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

CodePudding user response:

Netty is optimal for Webflux. Don't replace it. The basic principle of operation of the two systems is different. If you want to use Tomcat, use the spring-boot-starter-web dependency, not webflux.

Spring webFlux differrences when Netty vs Tomcat is used under the hood

Spring Web MVC vs Spring WebFlux. Blocking and Non-blocking


Non-blocking programming requires a different approach because you must not block any thread. But you can use a blocker mechanism it just be wrapped.

https://projectreactor.io/docs/core/release/reference/#faq.wrap-blocking


If you want to program like with spring-boot-starter-web pack, not as "stream". I recommend using the Kotlin language with Coroutine, which is two-way compatible with ProjectReactor and Java.


And you can't use thread-based data like Tomcat because more processes can use one thread so there may be thread data that another process can override.

https://projectreactor.io/docs/core/release/reference/#context

CodePudding user response:

I believe this is how you would do something equivalent, note the difference in exclusion:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>my-webflux-app</artifactId>
    <version>1.0.0</version>

    <properties>
        <java.version>11</java.version>
        <spring-boot.version>2.3.3.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.netty</groupId>
                    <artifactId>netty-all</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • Related