问题描述
我想使用 Springs 新的响应式 webflux 扩展在客户端和服务器应用程序之间建立通信.
I want to establish a communication between a client and server application using Springs new reactive webflux extension.
对于依赖项管理,我使用 gradle.我在服务器端以及客户端的 build.gradle 文件基本上是:
For dependency management I use gradle.My build.gradle file on the server, as well as on the client side basically is:
buildscript {
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.0.BUILD-SNAPSHOT")
}
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: "io.spring.dependency-management"
dependencies {
compile("org.springframework.boot:spring-boot-starter-webflux")
}
(需要注意的是 2.0.0.BUILD-SNAPSHOT 是一个移动的目标,由于依赖项内部的变化,手头的问题可能有一天会消失)
(It should be noted that 2.0.0.BUILD-SNAPSHOT is a moving target and the problem at hand may just vanish one day due to changes inside the dependency)
当我启动服务器端应用程序时,一切都很好,包括嵌入式 netty 服务器的启动.
When I start the server side application everything starts up well, including the start of an embedded netty server.
但是在启动客户端应用程序时,也会启动一个 netty 服务器,导致java.net.BindException: Address already in use",因为客户端 netty 服务器与服务器端 netty 服务器侦听相同的端口.
But when start the client application also a netty server is started, causing a "java.net.BindException: Address already in use", because the clientside netty server listens on the same port as the serverside netty server.
我的问题是:为什么首先在客户端启动 netty 以及如何防止它?
根据 Spring-Boot 文档,Spring 尝试确定是否需要 Web 支持并相应地配置 Spring 应用程序上下文.
According to the Spring-Boot Documentation Spring tries to determine if Web support is required and configures the Spring Application context accordingly.
根据文档,这可以通过调用 setWebEnvironment(false) 来覆盖.我的客户端启动代码如下所示:
And according to the Docs this can be overridden by a call to setWebEnvironment(false). My client startup code then looks like:
@SpringBootApplication(scanBasePackages = { "com.tatics.flux.main" })
public class Client {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(Client.class);
app.setWebEnvironment(false);
app.run(Client.class, args);
WebClient webClient = WebClient.create();
Mono<String> result = webClient
.post()
.uri("http://localhost:8080/fluxService")
// This does not work any more: .body("Hallo")
// and must be replaced by:
.body(BodyInserters.fromObject("Hallo"))
.accept(MediaType.TEXT_PLAIN)
.exchange()
.flatMap(response -> response.bodyToMono(String.class));
}
}
可惜netty还在启动.我还注意到 setWebEnvironment(false) 被标记为已弃用.
Unfortunately netty is still started.Also I note that setWebEnvironment(false) is marked as deprecated.
对于如何防止 netty 启动但保留所有 webflux 依赖项的任何帮助表示赞赏.
Any help on how to prevent netty from starting but otherwise preserve all webflux-dependencies is appreciated.
这是自动配置报告的摘录:
Here is an excerpt from the auto-configuration Report:
=========================
AUTO-CONFIGURATION REPORT
=========================
Positive matches:
-----------------
...
ReactiveWebServerAutoConfiguration matched:
- found ReactiveWebApplicationContext (OnWebApplicationCondition)
ReactiveWebServerAutoConfiguration#defaultReactiveWebServerCustomizer matched:
- @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.web.reactive.DefaultReactiveWebServerCustomizer; SearchStrategy: all) did not find any beans (OnBeanCondition)
ReactiveWebServerConfiguration.ReactorNettyAutoConfiguration matched:
- @ConditionalOnClass found required class 'reactor.ipc.netty.http.server.HttpServer'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- @ConditionalOnMissingBean (types: org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
ReactorCoreAutoConfiguration matched:
- @ConditionalOnClass found required classes 'reactor.core.publisher.Mono', 'reactor.core.publisher.Flux'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
...
Negative matches:
-----------------
...
ReactiveWebServerConfiguration.JettyAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.eclipse.jetty.server.Server' (OnClassCondition)
ReactiveWebServerConfiguration.TomcatAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.apache.catalina.startup.Tomcat' (OnClassCondition)
ReactiveWebServerConfiguration.UndertowAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'io.undertow.Undertow' (OnClassCondition)
...
ReactiveWebServerConfiguration.JettyAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.eclipse.jetty.server.Server' (OnClassCondition)
ReactiveWebServerConfiguration.TomcatAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.apache.catalina.startup.Tomcat' (OnClassCondition)
ReactiveWebServerConfiguration.UndertowAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'io.undertow.Undertow' (OnClassCondition)
推荐答案
您的代码的主要问题是您当前正在创建一个 SpringApplication
,然后您对其进行自定义 - 最后删除所有内容并运行静态方法 run(Object primarySource, String... args)
.
The main issue with your code is that you're currently creating a SpringApplication
, then you customize it - to finally drop everything and run the static method run(Object primarySource, String... args)
.
以下应该有效:
@SpringBootApplication
public class Client {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(Client.class);
app.setWebApplicationType(WebApplicationType.NONE);
app.run(args);
}
@Bean
public CommandLineRunner myCommandLineRunner() {
return args -> {
// we have to block here, since command line runners don't
// consume reactive types and simply return after the execution
String result = WebClient.create("http://localhost:8080")
.post()
.uri("/fluxService")
.body("Hallo")
.accept(MediaType.TEXT_PLAIN)
.retrieve()
.bodyToMono(String.class)
.block();
// print the result?
};
}
}
如果没有,请使用 --debug
标志运行您的应用程序,并将自动配置报告的相关部分添加到您的问题中,尤其是处理服务器的自动配置.
If not, please run your application using the --debug
flag and add to your question the relevant parts of the auto-configuration report, especially the auto-configurations dealing with servers.
这篇关于如何防止嵌入式 netty 服务器从 spring-boot-starter-webflux 启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!