使用Webflux和Spring Cloud时如何用netty替换tomcat?

Posted

技术标签:

【中文标题】使用Webflux和Spring Cloud时如何用netty替换tomcat?【英文标题】:How to replace tomcat with netty when using Webflux and Spring Cloud? 【发布时间】:2019-12-07 12:01:04 【问题描述】:

在阅读了几篇关于如何在使用 Webflux 时摆脱 Tomcat 的 SO 文章后,我仍然无法使用 Netty 作为 Tomcat 的替代品。

因为它是一个涉及不同模块和父 pom 等的项目。我将在这里发布到 repo 的链接: https://github.com/Deviad/clarity

如果您希望我在此处复制粘贴更多文件等,请告诉我,以符合 SO 政策。

我感兴趣的模块是clearance-transaction-dispatcher。

我认为问题与 Spring Cloud 在某种程度上需要 Tomcat(一些传递依赖项)以使我的中间件(clarity-transaction-dispatcher)与 Spring Cloud 配置服务器通信有关。

因此我很想知道这个问题是否有任何可能的解决方案。

谢谢。

我尝试使用 @Webflux 注释从 maven 中的某些依赖项中排除 tomcat,不包括 SpringMVC 自动配置。 我也试过这个: Spring boot enable/disable embedded tomcat with profile

家长会

<?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">
    <groupId>com.clarity</groupId>
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>clarity-transaction-dispatcher</module>
        <module>clarity-eureka-server</module>
        <module>clarity-persistence</module>
        <module>clarity-config-server</module>
        <module>clarity-shared</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <artifactId>clarity</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        <org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
        <jool.version>0.9.14</jool.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>$spring-cloud.version</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
            <version>1.2.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-web</artifactId>-->
<!--        </dependency>-->

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <version>$lombok.version</version>
        </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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>$org.mapstruct.version</version>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jool</artifactId>
            <version>$jool.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!-- Exclude the Tomcat dependency -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>an.awesome</groupId>
            <artifactId>pipelinr</artifactId>
            <version>0.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version> <!-- or newer version -->
                <configuration>
<!--                    <compilerVersion>11</compilerVersion> &lt;!&ndash; depending on your project &ndash;&gt;-->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>$org.mapstruct.version</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>$lombok.version</version>
                        </path>
                        <!-- other annotation processors -->
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

clearance-transaction-dispatcher 的子 pom

<?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>
    <parent>
        <groupId>com.clarity</groupId>
        <artifactId>clarity</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../</relativePath> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <spring-integration-kafka.version>3.1.4.RELEASE</spring-integration-kafka.version>
        <web3j.version>4.2.0</web3j.version>

    </properties>
    <artifactId>clarity-transaction-dispatcher</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>clarity-transaction-dispatcher</name>
    <description>This microservice dispatches transactions from a cryptocurrency server to Hyperledger</description>
    <packaging>jar</packaging>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.clarity</groupId>
                <artifactId>clarity</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>com.clarity</groupId>
            <artifactId>clarityshared</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-streams</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-kafka</artifactId>
            <version>$spring-integration-kafka.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-schema</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>$web3j.version</version>
        </dependency>
        <dependency>
            <groupId>com.aol.cyclops</groupId>
            <artifactId>cyclops-validation</artifactId>
            <version>7.2.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

同一模块的主类:

package com.clarity.transactiondispatcher;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.reactive.config.EnableWebFlux;

import java.util.TimeZone;

@SpringBootApplication(exclude = WebMvcAutoConfiguration.class)

@EnableWebFlux
public class TransactionDispatcherApplication implements InitializingBean 

    private void started() 
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    

    @Override
    public void afterPropertiesSet() 
        started();
    

    public static void main(String... args) 
        new SpringApplicationBuilder(TransactionDispatcherApplication.class).web(WebApplicationType.REACTIVE).run(args);

    

从我能想到的几乎任何地方排除 Tomcat 后,我​​仍然可以使用 Tomcat 而不是 Netty 运行应用程序。

【问题讨论】:

很难知道这么多外部库出了什么问题,有点乱。你能删除@EnableWebFluxexclude = WebMvcAutoConfiguration.class 【参考方案1】:

要使用 Spring-Boot 开发微服务并使用 Netty 服务器运行,需要很少的依赖项。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</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>

在主类中,我们只需要@SpringBootApplication

请相应地更改您的父母和孩子 pom.xml。根本不应该有任何spring-boot-starter-web 依赖。

【讨论】:

以上是关于使用Webflux和Spring Cloud时如何用netty替换tomcat?的主要内容,如果未能解决你的问题,请参考以下文章

SFA官方翻译Spring WebFlux和Spring Cloud进行响应式微服务开发

Spring Cloud Hoxton.SR5 与 Eureka 和 WebFlux 的问题:超过了缓冲区的最大字节数限制

Spirng Cloud Gateway中通过Spring security + WebFlux 实现权限认证

spring-boot-starter-webflux 与spring-cloud-starter-openfeign冲突

使用 @EnableResourceServer 支持 Spring Boot 反应式 (webflux)

使用 Spring WebFlux 路由器功能时如何获取模型属性?