我要建立使用Springs新反应的客户端和服务器应用程序之间的通信webflux扩展。
对于依赖管理我使用gradle这个。我的build.gradle文件的服务器上,以及在客户端主要是:
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- 快照是一个移动的目标,问题在手可能只是消失1天由于依赖内的变化)
当我开始在服务器端应用程序的一切开始得很好,包括嵌入式网状服务器的开始。
但是,当启动客户端应用程序也是一个网状服务器启动后,引起了“java.net.BindException:已使用的地址”,因为相同的端口服务器端的网状服务器上的客户机侧网状服务器监听。
我的问题是:为什么网状首先开始在客户端和如何预防呢?
根据弹簧,引导文档春天试图确定是否需要并配置Web支持的Spring应用程序上下文相应。
并根据文件,这可以通过对setWebEnvironment调用(假)覆盖。那么我的客户端启动的代码如下所示:
@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));
}
}
不幸的是网状仍然启动。此外,我注意到,setWebEnvironment(假)为废弃的标记。
如何防止网状从开始但在其他方面保留所有webflux依赖性任何帮助表示赞赏。
下面是自动配置报告的摘录:
=========================
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)
。
下面应该工作:
@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
标志,并添加到您的问题自动配置报告的相关部分,尤其是自动配置处理服务器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句