可用示例中的 Netty HttpServer api changed/differs

Netty HttpServer api changed/differs from available examples

Arjen Poutsma's blog post and Josh Long's video example 中的 Netty 服务器实例化是通过创建一个 reactor.ipc.netty.http.HttpServer 实例,然后使用 ReactorHttpHandlerAdapter 实例调用它的 startstartAndAwait 方法来完成的一个论点。

但是 API 似乎已经改变,现在 startstartAndAwait 方法现在需要具有以下签名的 lambda:

java.util.function.Function<? super reactor.ipc.netty.http.HttpChannel,? extends org.reactivestreams.Publisher<java.lang.Void>>

项目依赖项及其版本与 Arjen Poutsma 的示例项目相同

    <dependency>
        <groupId>org.reactivestreams</groupId>
        <artifactId>reactive-streams</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-core</artifactId>
        <version>3.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>io.projectreactor.ipc</groupId>
        <artifactId>reactor-netty</artifactId>
        <version>0.5.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>8.5.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web-reactive</artifactId>
        <version>5.0.0.BUILD-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.2</version>
    </dependency>

使用 spring 反应器支持实例化 netty 服务器的 new/proper 方法是什么?

目前推荐的设置项目的方法是使用 http://start.spring.io/,正如 Josh Long 在他的视频中所建议的那样。这是因为 spring reactive 现在只是候选发布版本,我们需要 运行 samples.This 的兼容版本是通过将这段代码添加到代码中实现的:

    <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot.experimental</groupId>
            <artifactId>spring-boot-dependencies-web-reactive</artifactId>
            <version>0.1.0.BUILD-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

根据您关于 HttpServer 接口更改的问题,最小工作示例如下:

import org.reactivestreams.Publisher;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.web.reactive.function.RouterFunction;
import org.springframework.web.reactive.function.ServerRequest;
import org.springframework.web.reactive.function.ServerResponse;
import reactor.core.publisher.Mono;
import reactor.ipc.netty.http.server.HttpServer;

import java.io.IOException;

import static org.springframework.web.reactive.function.RequestPredicates.GET;
import static org.springframework.web.reactive.function.RouterFunctions.route;
import static org.springframework.web.reactive.function.RouterFunctions.toHttpHandler;

public class FunctionalReactiveServer {

    public static final String HOST = "localhost";
    public static final int PORT = 8080;

    public static void main(String[] args) throws InterruptedException, IOException {
        RouterFunction<?> route = route(GET("/sayHello"), FunctionalReactiveServer::sayHelloHandler);
        HttpHandler httpHandler = toHttpHandler(route);

        ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
        HttpServer server = HttpServer.create(HOST, PORT);
        server.newHandler(adapter).block();

        System.out.println("Press ENTER to exit.");
        System.in.read();
    }

    public static ServerResponse<Publisher<String>> sayHelloHandler(ServerRequest request) {
        return ServerResponse.ok().body(Mono.just("Hello!"), String.class);
    }

}