如何防止嵌入式网状服务器从弹簧引导起动webflux开始?

弗兰克·凯瑟:

我要建立使用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)
布莱恩Clozel:

与您的代码的主要问题是,你目前正在制作一个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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

弹簧启动测试和嵌入式弹性服务器

如何关闭Spring Boot嵌入式服务器

如何找到嵌入式Redis端口并杀死嵌入式Redis服务器

嵌入式Jetty服务器

Azure云服务嵌入式FTP服务器

如何将嵌入式PostgreSQL服务器Java组件用作单独的服务?

嵌入式与非嵌入式Java服务器

在嵌入式设备上安装SSH服务器

使用FastCGI的嵌入式Web服务器

无法启动嵌入式Jetty服务器

启动嵌入式Jetty服务器的最短代码

Micronaut嵌入式服务器与本地主机

以编程方式配置嵌入式Underwow服务器

嵌入式码头服务器发生爆炸

如何在嵌入式板上检查/调整SSH服务器

如何配置嵌入式码头服务器记录所有请求?

如何在Undertow嵌入式服务器中登录文件?

如何创建Ktor嵌入式服务器的.jar(创建可执行文件)

如何创建嵌入式WebSocket服务器Jetty 9?

使用嵌入式码头,如何将ServerConnector链接到服务器?

如何连接到 ActiveMQ Artemis 嵌入式服务器?

使用Grizzly的嵌入式Java服务器:如何启用http2

如何与 Openmodelica 嵌入式 opc-ua 服务器交互

我将如何在 Openshift 上实现嵌入式 SFTP 服务器

如何在Spark嵌入式Web服务器中启用HTTP / 2

弹簧引导启动,网络和弹簧引导启动的Web服务和弹簧引导起动球衣的区别

Spring Boot服务器无法启动嵌入式Tomcat服务器

没有弹簧引导启动,网络和弹簧引导起动webflux一起工作?

JUnit测试与嵌入式Tomcat服务器,如何指定HTTP和HTTPS连接器自动端口?