更改 Webflux 在 Jetty 而不是 Netty 上运行后出现异常

Posted

技术标签:

【中文标题】更改 Webflux 在 Jetty 而不是 Netty 上运行后出现异常【英文标题】:Getting Exception after changing Webflux run on Jetty instead of Netty 【发布时间】:2018-10-22 05:32:04 【问题描述】:

我想在Jetty 而不是Netty 上运行我的Spring Boot Webflux 应用程序,我发现on this link 我可以轻松做到:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-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-jetty</artifactId>
</dependency>

当我尝试在我的应用程序中遇到异常时:

....
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myService': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: reactor/ipc/netty/http/client/HttpClient
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:138) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:423) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1702) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    ... 32 common frames omitted
Caused by: java.lang.NoClassDefFoundError: reactor/ipc/netty/http/client/HttpClient

排除 netty 后,我仍然可以看到我的 Redis 异步调用中有 netty 依赖项

mvn dependency:tree的输出:

[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building testproject 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.0.1:tree (default-cli) @ testproject ---
[INFO] com.sample.platform:testproject:jar:1.0-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-webflux:jar:2.0.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.0.0.RELEASE:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.4:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.4:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.4:compile
[INFO] |  +- org.hibernate.validator:hibernate-validator:jar:6.0.7.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] |  +- org.springframework:spring-web:jar:5.0.4.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:5.0.4.RELEASE:compile
[INFO] |  +- org.springframework:spring-webflux:jar:5.0.4.RELEASE:compile
[INFO] |  \- org.synchronoss.cloud:nio-multipart-parser:jar:1.1.0:compile
[INFO] |     \- org.synchronoss.cloud:nio-stream-storage:jar:1.1.3:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-redis-reactive:jar:2.0.0.RELEASE:compile
[INFO] |  \- org.springframework.boot:spring-boot-starter-data-redis:jar:2.0.0.RELEASE:compile
[INFO] |     \- io.lettuce:lettuce-core:jar:5.0.2.RELEASE:compile
[INFO] |        +- io.netty:netty-common:jar:4.1.22.Final:compile
[INFO] |        +- io.netty:netty-transport:jar:4.1.22.Final:compile
[INFO] |        |  +- io.netty:netty-buffer:jar:4.1.22.Final:compile
[INFO] |        |  \- io.netty:netty-resolver:jar:4.1.22.Final:compile
[INFO] |        \- io.netty:netty-handler:jar:4.1.22.Final:compile
[INFO] |           \- io.netty:netty-codec:jar:4.1.22.Final:compile
[INFO] +- org.springframework.data:spring-data-redis:jar:2.0.5-sample:compile
[INFO] |  +- org.springframework.data:spring-data-keyvalue:jar:2.0.5.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:2.0.5.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context:jar:5.0.4.RELEASE:compile
[INFO] |  |     \- org.springframework:spring-expression:jar:5.0.4.RELEASE:compile
[INFO] |  +- org.springframework:spring-tx:jar:5.0.4.RELEASE:compile
[INFO] |  +- org.springframework:spring-oxm:jar:5.0.4.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:5.0.4.RELEASE:compile
[INFO] |  +- org.springframework:spring-context-support:jar:5.0.4.RELEASE:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- org.springframework.cloud:spring-cloud-starter-config:jar:2.0.0.M8:compile
[INFO] |  +- org.springframework.cloud:spring-cloud-starter:jar:2.0.0.M8:compile
[INFO] |  |  +- org.springframework.cloud:spring-cloud-context:jar:2.0.0.M8:compile
[INFO] |  |  |  \- org.springframework.security:spring-security-crypto:jar:5.0.3.RELEASE:compile
[INFO] |  |  +- org.springframework.cloud:spring-cloud-commons:jar:2.0.0.M8:compile
[INFO] |  |  |  \- org.apache.httpcomponents:httpclient:jar:4.5.5:compile
[INFO] |  |  |     +- org.apache.httpcomponents:httpcore:jar:4.4.9:compile
[INFO] |  |  |     \- commons-codec:commons-codec:jar:1.11:compile
[INFO] |  |  \- org.springframework.security:spring-security-rsa:jar:1.0.5.RELEASE:compile
[INFO] |  |     \- org.bouncycastle:bcpkix-jdk15on:jar:1.56:compile
[INFO] |  |        \- org.bouncycastle:bcprov-jdk15on:jar:1.56:compile
[INFO] |  +- org.springframework.cloud:spring-cloud-config-client:jar:2.0.0.M8:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-databind:jar:2.9.4:compile
[INFO] |     \- com.fasterxml.jackson.core:jackson-core:jar:2.9.4:compile
[INFO] +- io.jsonwebtoken:jjwt:jar:0.9.0:compile
[INFO] +- io.projectreactor:reactor-test:jar:3.1.5.RELEASE:test
[INFO] |  \- io.projectreactor:reactor-core:jar:3.1.5.RELEASE:compile
[INFO] |     \- org.reactivestreams:reactive-streams:jar:1.0.2:compile
[INFO] +- com.sample.platform:testproject-core:jar:18-05-08_15-01_2513f08:compile
[INFO] |  +- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO] |  +- org.hibernate.validator:hibernate-validator-annotation-processor:jar:6.0.7.Final:compile
[INFO] |  +- javax.el:javax.el-api:jar:3.0.0:compile
[INFO] |  +- org.glassfish.web:javax.el:jar:2.2.6:compile
[INFO] |  +- com.google.guava:guava:jar:24.1-jre:compile
[INFO] |  |  +- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] |  |  +- org.checkerframework:checker-compat-qual:jar:2.0.0:compile
[INFO] |  |  +- com.google.errorprone:error_prone_annotations:jar:2.1.3:compile
[INFO] |  |  +- com.google.j2objc:j2objc-annotations:jar:1.1:compile
[INFO] |  |  \- org.codehaus.mojo:animal-sniffer-annotations:jar:1.14:compile
[INFO] |  \- com.fasterxml.jackson.datatype:jackson-datatype-joda:jar:2.9.4:compile
[INFO] |     \- joda-time:joda-time:jar:2.9.9:compile
[INFO] +- com.github.kstyrc:embedded-redis:jar:0.6:test
[INFO] |  \- commons-io:commons-io:jar:2.4:test
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.0.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:2.0.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.0.RELEASE:compile
[INFO] |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] |  +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] |  +- org.springframework:spring-core:jar:5.0.4.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.0.4.RELEASE:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.19:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:2.0.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:2.0.0.RELEASE:compile
[INFO] |  |  \- org.springframework.boot:spring-boot-actuator:jar:2.0.0.RELEASE:compile
[INFO] |  \- io.micrometer:micrometer-core:jar:1.0.1:compile
[INFO] |     +- org.hdrhistogram:HdrHistogram:jar:2.1.10:compile
[INFO] |     \- org.latencyutils:LatencyUtils:jar:2.0.3:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.0.0.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:2.0.0.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.0.0.RELEASE:test
[INFO] |  +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO] |  |  \- net.minidev:json-smart:jar:2.3:test
[INFO] |  |     \- net.minidev:accessors-smart:jar:1.2:test
[INFO] |  |        \- org.ow2.asm:asm:jar:5.0.4:test
[INFO] |  +- junit:junit:jar:4.12:test
[INFO] |  +- org.assertj:assertj-core:jar:3.9.1:test
[INFO] |  +- org.mockito:mockito-core:jar:2.15.0:test
[INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.7.10:test
[INFO] |  |  +- net.bytebuddy:byte-buddy-agent:jar:1.7.10:test
[INFO] |  |  \- org.objenesis:objenesis:jar:2.6:test
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] |  +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] |  +- org.springframework:spring-test:jar:5.0.4.RELEASE:test
[INFO] |  \- org.xmlunit:xmlunit-core:jar:2.5.1:test
[INFO] \- com.sample.platform:metrics:jar:18-03-13_14-48_624a3e7:compile
[INFO]    \- io.micrometer:micrometer-registry-statsd:jar:1.0.1:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.162 s
[INFO] Finished at: 2018-05-11T14:16:46-04:00
[INFO] Final Memory: 28M/309M
[INFO] ------------------------------------------------------------------------

所以我的问题是在Jetty 上运行 Webflux 还没有完全支持?还是我的依赖版本有问题,我也需要对redis做一些事情?

【问题讨论】:

【参考方案1】:

首先,该文档中可能存在拼写错误。 spring-boot-starter-reactor-netty 工件应从 spring-boot-starter-webflux 中排除,而不是从 spring-security-webflux 中排除:

<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-jetty</artifactId>
</dependency>

另一件事是,myService bean 直接请求 HttpClient bean。 myService bean 听起来像是你的课程之一。您应该检查是否在任何字段中注入了HttpClient

【讨论】:

以上是关于更改 Webflux 在 Jetty 而不是 Netty 上运行后出现异常的主要内容,如果未能解决你的问题,请参考以下文章

Spring WebFlux 应用程序运行 tomcat 而不是 Netty

webFlux&Reactor

Webflux 和 keycloak 使用 jwt 中的方法安全级别 @Preauthorize 自定义声明而不是默认范围

Jetty 9.2.x Websocket Server连接在使用Firefox而不是Chrome时连接后自动关闭

从 Spring WebFlux 返回 Flux<String> 返回一个字符串而不是 JSON 中的字符串数组

Jetty 和 Dropwizard:如何始终返回 200 而不是 404、500 等