尝试Spring Boot2 WebFlux(启动失败了?不要怕)

原文链接:http://www.dubby.cn/detail.html?id=9030

代码地址:https://github.com/dubby1994/web-flux-demo

已经迫不及待的要试试刚刚出炉的WebFlux了?哈哈,那你就来对了

pom.xml

 <parent>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-starter-parentartifactId>
     <version>2.0.0.M3version>
 parent>

 <dependencies>
     
     <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-webfluxartifactId>
     dependency>
     <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-actuatorartifactId>
     dependency>
     <dependency>
            <groupId>org.synchronoss.cloudgroupId>
            <artifactId>nio-multipart-parserartifactId>
            <version>1.1.0version>
     dependency>
     
     <dependency>
         <groupId>org.springframework.bootgroupId>
         <artifactId>spring-boot-starter-testartifactId>
         <scope>testscope>
     dependency>
     <dependency>
         <groupId>io.projectreactorgroupId>
         <artifactId>reactor-testartifactId>
         <scope>testscope>
     dependency>
 dependencies>

SampleWebFluxApplication.java:

package cn.dubby.webflux;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

/**
 * Created by teeyoung on 17/9/22.
 */
@SpringBootApplication
public class SampleWebFluxApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleWebFluxApplication.class);
    }

    @Bean
    public RouterFunction monoRouterFunction(EchoHandler echoHandler) {
        return route(POST("/echo"), echoHandler::echo);
    }

}

EchoHandler.java

package cn.dubby.webflux;

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * Created by teeyoung on 17/9/22.
 */
@Component
public class EchoHandler {

    public Mono echo(ServerRequest request) {
        return ServerResponse.ok().body(request.bodyToMono(String.class), String.class);
    }

}

WelcomeController.java

package cn.dubby.webflux;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.publisher.Mono;
import reactor.core.publisher.MonoSink;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

/**
 * Created by teeyoung on 17/9/22.
 */
@RestController
public class WelcomeController {

    @GetMapping("/")
    public String welcome() {
        return "Hello World:";
    }

    @GetMapping("/hello")
    public Mono hello(String name) {
        return Mono.create(new Consumer>() {
            @Override
            public void accept(MonoSink stringMonoSink) {
                stringMonoSink.success("hello, " + name);
            }
        });
    }

    @GetMapping("/hi/{name}")
    public Flux> hi(@PathVariable String name) {
        return Flux.create(new Consumer>>() {
            @Override
            public void accept(FluxSink> stringFluxSink) {

                List stringList1 = new ArrayList();
                stringList1.add("hi 1, " + name);

                List stringList2 = new ArrayList();
                stringList2.add("hi 2, " + name);

                stringFluxSink.next(stringList1).next(stringList2).complete();
            }
        });
    }

}

启动的时候失败了?

org.springframework.boot.web.server.WebServerException: Unable to start Netty
        at org.springframework.boot.web.embedded.netty.NettyWebServer.start(NettyWebServer.java:74) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
        at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.startReactiveWebServer(ReactiveWebServerApplicationContext.java:139) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
        at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.finishRefresh(ReactiveWebServerApplicationContext.java:72) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
        at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:49) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
        at ..main(MyApplication.kt:10) [main/:na]
Caused by: reactor.core.Exceptions$ReactiveException: java.util.concurrent.TimeoutException: HttpServer couldn't be started within 3000ms
        at reactor.core.Exceptions.propagate(Exceptions.java:240) ~[reactor-core-3.1.0.M3.jar:3.1.0.M3]
        at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:87) ~[reactor-core-3.1.0.M3.jar:3.1.0.M3]
        at reactor.core.publisher.Mono.block(Mono.java:1280) ~[reactor-core-3.1.0.M3.jar:3.1.0.M3]
        at reactor.ipc.netty.tcp.BlockingNettyContext.(BlockingNettyContext.java:55) ~[reactor-netty-0.7.0.M1.jar:0.7.0.M1]
        at reactor.ipc.netty.tcp.BlockingNettyContext.(BlockingNettyContext.java:45) ~[reactor-netty-0.7.0.M1.jar:0.7.0.M1]
        at reactor.ipc.netty.NettyConnector.start(NettyConnector.java:53) ~[reactor-netty-0.7.0.M1.jar:0.7.0.M1]
        at org.springframework.boot.web.embedded.netty.NettyWebServer.start(NettyWebServer.java:64) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
        ... 10 common frames omitted
        Suppressed: java.lang.Exception: #block terminated with an error
                at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:88) ~[reactor-core-3.1.0.M3.jar:3.1.0.M3]
                ... 15 common frames omitted
Caused by: java.util.concurrent.TimeoutException: HttpServer couldn't be started within 3000ms
        at reactor.ipc.netty.tcp.BlockingNettyContext.(BlockingNettyContext.java:53) ~[reactor-netty-0.7.0.M1.jar:0.7.0.M1]
        ... 13 common frames omitted

很正常,这是因为java.net.InetAddress.getLocalHost()超时了,解决方法很简单,修改你的hosts文件,加上这两个,一定要记得加上你的主机名,在这里就是mbpro.local

127.0.0.1   localhost mbpro.local
::1         localhost mbpro.local

ok,至此就可以了!

你可能感兴趣的:(webflux,springboot)