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

来源:互联网 发布:手机在淘宝怎么退换货 编辑:程序博客网 时间:2024/06/04 08:19

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

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

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

pom.xml

 <parent>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-parent</artifactId>     <version>2.0.0.M3</version> </parent> <dependencies>     <!-- Compile -->     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-webflux</artifactId>     </dependency>     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-actuator</artifactId>     </dependency>     <dependency>            <groupId>org.synchronoss.cloud</groupId>            <artifactId>nio-multipart-parser</artifactId>            <version>1.1.0</version>     </dependency>     <!-- Test -->     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-test</artifactId>         <scope>test</scope>     </dependency>     <dependency>         <groupId>io.projectreactor</groupId>         <artifactId>reactor-test</artifactId>         <scope>test</scope>     </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. */@SpringBootApplicationpublic class SampleWebFluxApplication {    public static void main(String[] args) throws Exception {        SpringApplication.run(SampleWebFluxApplication.class);    }    @Bean    public RouterFunction<ServerResponse> 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. */@Componentpublic class EchoHandler {    public Mono<ServerResponse> 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. */@RestControllerpublic class WelcomeController {    @GetMapping("/")    public String welcome() {        return "Hello World:";    }    @GetMapping("/hello")    public Mono<String> hello(String name) {        return Mono.create(new Consumer<MonoSink<String>>() {            @Override            public void accept(MonoSink<String> stringMonoSink) {                stringMonoSink.success("hello, " + name);            }        });    }    @GetMapping("/hi/{name}")    public Flux<List<String>> hi(@PathVariable String name) {        return Flux.create(new Consumer<FluxSink<List<String>>>() {            @Override            public void accept(FluxSink<List<String>> stringFluxSink) {                List<String> stringList1 = new ArrayList<String>();                stringList1.add("hi 1, " + name);                List<String> stringList2 = new ArrayList<String>();                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 <mypackage>.<myclass>.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.<init>(BlockingNettyContext.java:55) ~[reactor-netty-0.7.0.M1.jar:0.7.0.M1]        at reactor.ipc.netty.tcp.BlockingNettyContext.<init>(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 omittedCaused by: java.util.concurrent.TimeoutException: HttpServer couldn't be started within 3000ms        at reactor.ipc.netty.tcp.BlockingNettyContext.<init>(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,至此就可以了!

原创粉丝点击