使用Spring Boot Weblfux和嵌入式Tomcat

Kyl*_*lin 6 spring-boot spring-webflux

我试图在新的Spring Boot App中使用WebFlux响应类型.我在https://start.spring.io上使用了initializr 并选择了2.0.0-SNAPSHOT版本.我添加了Web反应依赖,我所做的一切都很棒.这是一个非常可靠的POC,目标是如何利用这些类型来实现API的现代化,为此,我们计划慢慢替换阻塞和/或同步过程的每个部分,并用非阻塞的替代实现替换它.

我遇到的问题是,当我试图将我的POC演变成更类似于我们在生产中的服务时,很多东西似乎都没有起作用.现在我明白webflux还不是GA,而且我不应该期待所有其他春季项目的完全反应支持.但我记得当webflux仍被称为web-reactive,你可以在underow/jetty/netty/tomcat/etc上运行但是现在我正在使用webflux启动器,一切都默认为netty,我没有看到文档调用如何将其更改为我们其他服务当前使用的嵌入式tomcat.

是否仍然可以将spring-boot-starter-webflux与其他应用程序容器一起使用,或者我现在是否需要手动引导webflux以使用除netty之外的其他东西?

Bri*_*zel 10

您可以spring-boot-starter-reactor-nettyspring-boot-starter-webflux依赖项和使用中排除spring-boot-starter-tomcat,spring-boot-starter-undertow或者spring-boot-starter-jetty替代.

  • 那仍然有效吗?我最终会遇到缺少类,例如“引起者:java.lang.NoClassDefFoundError:io / netty / buffer / Unpooled”。 (2认同)

Thi*_*han 6

如果您的目标是使用 tomcat 服务器,则无需排除 spring-boot-starter-reactor-netty。添加 spring boot starter tomcat 将启动 Tomcat 服务器中的应用程序。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <exclusions>
        <!-- Exclude the Netty dependency -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-reactor-netty</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- Use Tomcat instead -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)