我想使用Springs新的reactive 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-snapshot是一个移动的目标,手头的问题有一天可能会因为依赖关系内部的变化而消失)
我的问题是:为什么netty一开始是在客户端启动的,我该如何预防?
根据Spring-Boot文档,Spring尝试确定是否需要Web支持,并相应地配置Spring应用程序上下文。
根据文档,这可以通过调用setWebEnvironment(false)来覆盖。我的客户端启动代码如下所示:
@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));
}
}
以下是自动配置报告的节选:
=========================
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
标志运行您的应用程序,并在您的问题中添加自动配置报告的相关部分,特别是处理服务器的自动配置。
我有一个简单spring应用程序 我的其他依赖项是: 当我启动应用程序时,我有一个奇怪的错误: 看来我和spring-webmvc和WebFlux有冲突。我想要反应和webmvc没有发挥好与spring reactive根据我读到的,所以我排除了spring-web-starter,但然后我有... 需要什么使spring oauth2与spring-webflux一起工作? 提前致谢
运行Spring Boot应用程序时,嵌入式tomcat服务器无法启动。我刚刚在pom.xml中添加了所需的依赖项,并创建了一个简单的java POJO类。应用程序属性已经按照oracle数据库所需的jdbc配置以及Hibernate方言信息进行了设置。 执行mvn spring-boot:run时的控制台日志
我正在开发一个cron,我正在使用一个web客户端向一个rest API发送一个post请求。我不想让嵌入式服务器保持开机状态,因为cron需要在几秒钟内完成其任务。但当我不使用服务器时: 更新:我试过了 但是获取由:java.lang.IllegalStateException:org.springframework.boot.web.reactive.context.AnnotationCon
我需要启动spring boot的嵌入式tomcat服务器,并在调试模式下启动服务器
Spring应用程序使用Maven只包含Spring Web依赖项,给出“无法启动嵌入式Tomcat服务”的错误。 相同的项目在其他计算机上运行,就像预期的那样。 null
我希望netty服务器A在启动时连接netty服务器B,听起来像代理,所以我尝试了netty代理示例,但它只是在中启动netty客户端,只有一个新的连接处于活动状态,客户端才会被创建。当服务器A启动时,我需要通知服务器B做一些“注册”的事情,我该怎么办?