下面列出了怎么用org.springframework.http.server.reactive.ReactorHttpHandlerAdapter的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("unchecked")
private RSocketFactory.Start<CloseableChannel> createRSocketStarter(HttpHandler httpHandler) {
RSocketFactory.ServerRSocketFactory rSocketFactory = applyCustomizers(RSocketFactory.receive());
HttpServer httpServer = createHttpServer();
ReactorHttpHandlerAdapter handlerAdapter = new ReactorHttpHandlerAdapter(httpHandler);
return rSocketFactory
.acceptor(socketAcceptor)
.transport((ServerTransport) new WebsocketRouteTransport(
httpServer,
r -> r.route(hsr -> !("/" + hsr.path()).equals(path), handlerAdapter),
path
));
}
public static void main(String... args) {
long start = System.currentTimeMillis();
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routes(
new BCryptPasswordEncoder(18)
));
ReactorHttpHandlerAdapter reactorHttpHandler = new ReactorHttpHandlerAdapter(httpHandler);
DisposableServer server = HttpServer.create()
.host("localhost")
.port(8080)
.handle(reactorHttpHandler)
.bindNow();
LOGGER.debug("Started in " + (System.currentTimeMillis() - start) + " ms");
server.onDispose()
.block();
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
ApplicationContext context = ((ContextRefreshedEvent) event).getApplicationContext();
if (context != this.context) {
return;
}
if (!ClassUtils.isPresent("org.springframework.http.server.reactive.HttpHandler", null)) {
logger.info("No web server classes found so no server to start");
return;
}
Integer port = Integer.valueOf(context.getEnvironment().resolvePlaceholders("${server.port:${PORT:8080}}"));
String address = context.getEnvironment().resolvePlaceholders("${server.address:0.0.0.0}");
if (port >= 0) {
HttpHandler handler = context.getBean(HttpHandler.class);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host(address).port(port).handle(adapter);
Thread thread = new Thread(
() -> httpServer.bindUntilJavaShutdown(Duration.ofSeconds(60), this::callback),
"server-startup");
thread.setDaemon(false);
thread.start();
}
}
@Override
public void executeApp() throws Exception {
int port = getAvailablePort();
DisposableServer httpServer = HttpServer.create()
.host("localhost")
.port(port)
.handle(new ReactorHttpHandlerAdapter(new MyHttpHandler()))
.bind()
.block();
WebClient client = WebClient.create("http://localhost:" + port);
client.get()
.uri("/webflux/abc")
.retrieve()
.bodyToMono(String.class)
.block();
httpServer.dispose();
}
@Override
public void executeApp() throws Exception {
int port = getAvailablePort();
NettyContext nettyContext = HttpServer.create("localhost", port)
.newHandler(new ReactorHttpHandlerAdapter(new MyHttpHandler()))
.block();
WebClient client = WebClient.create("http://localhost:" + port);
client.get()
.uri("/webflux/abc")
.retrieve()
.bodyToMono(String.class)
.block();
nettyContext.dispose();
}
public void startReactorServer() throws InterruptedException {
RouterFunction<ServerResponse> route = routingFunction();
HttpHandler httpHandler = toHttpHandler(route);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
HttpServer server = HttpServer.create(HOST, PORT);
server.newHandler(adapter).block();
}
public void startReactorServer() throws InterruptedException {
RouterFunction<ServerResponse> route = routingFunction();
HttpHandler httpHandler = toHttpHandler(route);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
HttpServer server = HttpServer.create(HOST, PORT);
server.newHandler(adapter).block();
}
/**
* 暴露6200管理端口
*/
@Override
public void run(ApplicationArguments args) throws Exception {
RouterFunction manageRouterFunction = RouterFunctions
.route(RequestPredicates.GET("/ok"), req -> {
return ServerResponse.ok()
.syncBody("online");
})
.andRoute(RequestPredicates.GET("/cmd/{line}"), request -> {
String pathVar = request.pathVariable("line");
try {
if ("down".equals(pathVar)) {
refuseRequestService.setRefuseRequest(true);
} else if ("up".equals(pathVar)) {
refuseRequestService.setRefuseRequest(false);
}
return ServerResponse.ok().body(Mono.just("ok"), String.class);
} catch (Throwable e) {
LOGGER.error("{} request err!", pathVar, e);
return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
});
HttpHandler handler = RouterFunctions.toHttpHandler(manageRouterFunction);
int managePort = serverPort - 1000;
ReactorHttpHandlerAdapter inboundAdapter = new ReactorHttpHandlerAdapter(handler);
// manage port
HttpServer.create().port(managePort).handle(inboundAdapter).bind();
// HttpServer.create(managePort).newHandler(inboundAdapter).block();
LOGGER.info("management services run on port:{}", managePort);
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("8095"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("8908"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("8901"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("9007"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("9006"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("8901"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("8909"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("8125"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("9008"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("8901"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("8095"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("9008"));
return httpServer.newHandler(adapter).block();
}
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = DispatcherHandler.toHttpHandler(context);
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", Integer.valueOf("8908"));
return httpServer.newHandler(adapter).block();
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}
@Profile("default")
@Bean
public HttpServer nettyHttpServer(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create().host("localhost").port(this.port);
return httpServer.handle(adapter);
}