哪个等同于 reactor.netty.http.server 包中的旧方法 startAndAwait?

Which is the equivalent of the old method startAndAwait in reactor.netty.http.server package?

我开始学习 Spring,在我学习的教程中,讲师使用了方法:startAndAwait,它在 reactor.ipc.netty.http.server.HttpServer 包中。现在没有方法了,包是reactor.netty.http.server.HttpServer.

我想学习基于最新包的解决方案,因此我的问题是以下代码的当前等效项:

import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.ipc.netty.http.server.HttpServer;


import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

public class HelloServerApplication {

    public static void main(String[] args)
    {
        RouterFunction route = route( GET("/"),
                request -> ServerResponse.ok().body(fromObject("Hello")));
        HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);

        HttpServer server = HttpServer.create("localhost",8080);
        server.startAndAwait(new ReactorHttpHandler(httpHandler));
    }

}

我一直在寻找解决方案,但我的知识太少,无法独自应对这个问题。到目前为止我写我把代码改到"server.startAndAwait"的地方还是不能替换这个方法:

package pl.javasurvival.helloServer;

import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.netty.http.server.HttpServer;


import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

public class HelloServerApplication {

    public static void main(String[] args)
    {
        RouterFunction route = route( GET("/"),
                request -> ServerResponse.ok().body(fromObject("Hello")));
        HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);

        HttpServer server = HttpServer
                .create()
                .host("localhost")
                .port(8080);

        //what is a new method which is equals to startAndAwait

    }

}

PS:忘记补充说我用的是gradle。这是 build.gradle 文件:

plugins {
    id 'org.springframework.boot' version '2.2.0.M4'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'pl.javasurvival'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/snapshot' }
    maven { url 'https://repo.spring.io/milestone' }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux:2.2.0.M4'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        exclude group: 'junit', module: 'junit'
    }
    testImplementation 'io.projectreactor:reactor-test'
}

test {
    useJUnitPlatform()
}

已经有一段时间了,但我在 1 小时前发现了这个问题,现在我有了解决方案,所以它可能对其他人有帮助。

在不导入旧版本reactor.netty的情况下,你可以试试这个(添加扫描器只是为了等待操作)

import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.netty.http.server.HttpServer;

import java.util.Scanner;

import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;


public class HelloServerApplication {

    public static void main(String[] args) {
        RouterFunction route = route(GET("/"),
                request -> ServerResponse.ok().body(fromObject("Hello")));

        var httpHandler = RouterFunctions.toHttpHandler(route);

        var adapter = new ReactorHttpHandlerAdapter(httpHandler);

        var server = HttpServer.create().host("localhost").port(8080).handle(adapter).bindNow();

        System.out.println("press enter");

        Scanner sc = new Scanner(System.in);
        sc.next();
        server.disposeNow();
    }
}

您可以使用方法 block,如:

    DisposableServer server =
            HttpServer.create()   
                      .bindNow(); 

    server.onDispose()
          .block();

Reactor Netty docs 阅读更多内容。