Netty HttpServer api与可用示例有所不同 [英] Netty HttpServer api changed/differs from available examples

查看:221
本文介绍了Netty HttpServer api与可用示例有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Arjen Poutsma的博客帖子和乔什·朗的视频示例是通过创建reactor.ipc.netty.http.HttpServer实例,然后使用ReactorHttpHandlerAdapter实例作为参数调用它的startstartAndAwait方法.

Netty server instantiation in Arjen Poutsma's blog post and Josh Long's video example is done by creating an reactor.ipc.netty.http.HttpServer instance and then calling it's start or startAndAwait method with an ReactorHttpHandlerAdapter instance as an argument.

但是,由于现在startstartAndAwait方法现在期望具有以下签名的lambda,因此API似乎已发生更改:

However the API seems to have changed as now start and startAndAwait methods now expect a lambda with the following signature:

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

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

Project dependencies and their versions are the same as in Arjen Poutsma's example project

    <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 Reactor支持的Netty服务器的新/正确方法是什么?

What is the new/proper way of instantiating a netty server with spring reactor support?

推荐答案

目前建议的设置项目的方法是使用

The recommended way to set up project for now is to use http://start.spring.io/ as Josh Long suggests in his video. This is because spring reactive is only release candidate now and we need compatible versions to run samples.This is achieved via adding this piece to code:

    <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接口更改的问题,最小的工作示例如下:

According your question about HttpServer interface change, the minimal working example is the following:

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);
    }

}

这篇关于Netty HttpServer api与可用示例有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆