尝试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,至此就可以了!
阅读全文
0 0
- 尝试Spring Boot2 WebFlux(启动失败了?不要怕)
- Spring Framework# WebFlux
- kotlin spring-webflux netty
- java spring-webflux netty
- 不要怕,不要悔
- 不要怕,不要悔
- 【Spring5】使用 Spring Webflux 开发 Reactive 应用
- 大胆授权不要怕
- 巴菲特:不要怕犯错
- 不要怕麻烦
- spring-boot 启动失败
- websphere 7.0 控制台中的企业服务启动失败 ( 尝试处理服务器 server1 时发生异常 )
- 一不小心,删错了,还把回收站清空了,不要怕!
- 一不小心修改了/etc/profile 文件导致命令不可用怎么办,不要怕~
- Spring 5 的 WebFlux 开发反应式 Web 应用
- 第一次启动AVD失败了
- Spring Boot启动失败:...rethrowRuntimeException....
- idea某些模块启动失败(spring cloud )
- opencv+zxing进行条码识别并解码
- Incude便签
- 软件质量保证--基于Google Test的 Add函数测试
- EM算法与思想
- iOS: 本地通知&远程推送
- 尝试Spring Boot2 WebFlux(启动失败了?不要怕)
- JNI中枚举类型作为参数
- android网络相关问题汇总
- python字符串/元组/列表/字典互转
- 负载均衡算法之轮询
- LLINUX_C编程实战-第七章《进程管理》学习笔记
- 关于Windwos10系统中电脑无法完全关机的解决办法
- maven eclipse搭建web项目并发布到tomcat
- 7-3 jmu-ds-单链表的基本运算