【问题标题】:Why spring-webflux application is running on tomcat as default, instead of netty?为什么 spring-webflux 应用程序默认在 tomcat 上运行,而不是 netty?
【发布时间】:2020-11-09 13:02:47
【问题描述】:

我使用下面的pom.xml 创建了一个 Spring-webflux 项目,但是当我启动应用程序时,它是在 Tomcat 上启动的。它不应该在 Netty 上默认启动吗?

<?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.3.5.RELEASE</version>
    </parent>
    <groupId>com.test</groupId>
    <artifactId>reactive-rest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>reactive-rest</name>

    <properties>
        <java-version>1.8</java-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-rsocket</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-messaging</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-rsocket</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${java-version}</source>
                        <target>${java-version}</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

主类:

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class, WebMvcAutoConfiguration.class})
public class ReactiveRestApplication {

    public static void main(String[] args) {
        SpringApplication.run(ReactiveRestApplication.class, args);
    }

}

开始日志:

2020-11-09 18:29:26.311  INFO 80838 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-11-09 18:29:26.375  INFO 80838 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-09 18:29:26.390  INFO 80838 --- [  restartedMain] c.o.r.ReactiveRestApplication            : Started ReactiveRestApplication in 18.256 seconds (JVM running for 25.2)

我什至尝试删除 tomcat 依赖,但应用程序仍然在 Tomcat 上启动

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

甚至尝试设置spring.main.web-application-type: reactive,但仍然是tomcat。

更新:我只保留了spring-boot-starter-webflux 并删除了所有其他依赖项。这次也是在“Tomcat”上启动服务

【问题讨论】:

    标签: spring-webflux


    【解决方案1】:

    如果您查找包含在 mvnrepository 中的不同库,您可以看到 spring-boot-starter-websocket

    依赖于spring-boot-starter-web,这反过来会强制默认启动您的应用程序作为 springMVC 应用程序,而不是 webflux 应用程序。

    我的假设是 websockets 已经包含在 webflux 依赖项中。

    【讨论】:

    • 我注释掉了所有其他依赖项。只保留spring-boot-starter-webflux,但服务仍然在 Tomcat 上启动。
    • 那么你需要发布你的 mvn 依赖树
    • 我创建了一个只有 webflux 作为依赖项的新项目,它从 Netty 开始。这意味着你是对的,我的依赖中存在一些东西。一一添加。让我们看看是什么导致了问题
    • 正如我之前所说,如果您发布您的依赖关系树,我们可能会弄清楚
    • @TheCoder 请提供任何文件来支持您的声明。 ReactiveWebsockHandler 是在反应包 docs.spring.io/spring-framework/docs/current/javadoc-api/org/… 中提供的,所以你的声明是错误的。
    猜你喜欢
    • 2020-08-05
    • 2019-11-09
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    相关资源
    最近更新 更多