【问题标题】:Netty HttpServer api changed/differs from available examplesNetty HttpServer api 更改/与可用示例不同
【发布时间】:2017-04-11 08:25:42
【问题描述】:

Arjen Poutsma's blog postJosh 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 reactor 支持实例化 netty 服务器的新方法/正确方法是什么?

【问题讨论】:

    标签: java spring netty project-reactor


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2014-12-14
      • 2013-03-27
      • 1970-01-01
      • 2017-04-27
      • 2012-06-23
      • 2019-07-05
      相关资源
      最近更新 更多