摘要:的這幾天看了看的請(qǐng)求處理流程,因?yàn)橹耙恢庇玫暮?,一開始對(duì)的處理流程有點(diǎn)懵逼,找不到入口,后來(lái)跟了代碼,在網(wǎng)上找了點(diǎn)資料,發(fā)現(xiàn)的入口在的方法該方法的作用就是把接收到的或者最終需要返回的,包裝轉(zhuǎn)換為和。
spring-cloud-gateway 的ReactorHttpHandlerAdapter
這幾天看了看spring-cloud-gateway的請(qǐng)求處理流程,因?yàn)橹耙恢庇玫膕pringboot1.x和spring4,一開始對(duì)spring-cloud-gateway的處理流程有點(diǎn)懵逼,找不到入口,后來(lái)跟了代碼,在網(wǎng)上找了點(diǎn)資料,發(fā)現(xiàn)spring-cloud-gateway的入口在ReactorHttpHandlerAdapter的apply方法
public class ReactorHttpHandlerAdapter implements BiFunction> { private static final Log logger = HttpLogging.forLogName(ReactorHttpHandlerAdapter.class); private final HttpHandler httpHandler; public ReactorHttpHandlerAdapter(HttpHandler httpHandler) { Assert.notNull(httpHandler, "HttpHandler must not be null"); this.httpHandler = httpHandler; } @Override public Mono apply(HttpServerRequest reactorRequest, HttpServerResponse reactorResponse) { NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(reactorResponse.alloc()); try { ReactorServerHttpRequest request = new ReactorServerHttpRequest(reactorRequest, bufferFactory); ServerHttpResponse response = new ReactorServerHttpResponse(reactorResponse, bufferFactory); if (request.getMethod() == HttpMethod.HEAD) { response = new HttpHeadResponseDecorator(response); } return this.httpHandler.handle(request, response) .doOnError(ex -> logger.trace(request.getLogPrefix() + "Failed to complete: " + ex.getMessage())) .doOnSuccess(aVoid -> logger.trace(request.getLogPrefix() + "Handling completed")); } catch (URISyntaxException ex) { if (logger.isDebugEnabled()) { logger.debug("Failed to get request URI: " + ex.getMessage()); } reactorResponse.status(HttpResponseStatus.BAD_REQUEST); return Mono.empty(); } } }
該方法的作用就是把接收到的HttpServerRequest或者最終需要返回的HttpServerResponse,包裝轉(zhuǎn)換為ReactorServerHttpRequest和ReactorServerHttpResponse。
spring-webflux當(dāng)然,這篇文章的主要內(nèi)容不是談?wù)搒pring-cloud-gateway了,因?yàn)橹耙恢庇玫膕pring4,所以對(duì)spring5當(dāng)中的反應(yīng)式編程范式和webflux不太了解,所以先寫個(gè)demo了解一下
第一步:引入相關(guān)pom,測(cè)試的相關(guān)pom根據(jù)自己的需要引入
org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE org.springframework.boot spring-boot-starter-webflux org.springframework.boot spring-boot-starter-test test io.projectreactor reactor-test test
第二步:創(chuàng)建一個(gè)HandlerFunction
public class TestFunction implements HandlerFunction{ @Override public Mono handle(ServerRequest serverRequest) { return ServerResponse.ok().body( Mono.just(parse(serverRequest, "args1") + parse(serverRequest, "args2")) , Integer.class); } private int parse(final ServerRequest request, final String param) { return Integer.parseInt(request.queryParam(param).orElse("0")); } }
第三步:注入一個(gè)RouterFunction
@Configuration public class TestRouteFunction { @Bean public RouterFunctionrouterFunction() { return RouterFunctions.route(RequestPredicates.GET("/add"), new TestFunction()); } }
第四步:在webflux中,也可以使用之前的java注解的編程方式,我們也創(chuàng)建一個(gè)controller
@RestController @RequestMapping("/api/test") public class HelloController { @RequestMapping("/hello") public Monohello() { return Mono.just("hello world"); } }
第五步:創(chuàng)建啟動(dòng)類
@SpringBootApplication public class Spring5DemoApplication { public static void main(String[] args) { SpringApplication.run(Spring5DemoApplication.class, args); } }
第六步:?jiǎn)?dòng)項(xiàng)目,訪問(wèn)如下兩個(gè)接口都可以
http://localhost:8080/api/test/hello http://localhost:8080/add?args1=2&args2=3和spring-boot結(jié)合
通過(guò)上面的例子,我們看到基本的兩個(gè)類:HandlerFunction和RouterFunction,同時(shí)webflux有如下特性:
異步非阻塞
響應(yīng)式(reactive)函數(shù)編程,純lambda表達(dá)式
不僅僅是在Servlet容器中tomcat/jetty中運(yùn)行,同時(shí)支持NIO的Netty和Undertow中,實(shí)際項(xiàng)目中,我們往往與spring-boot項(xiàng)目結(jié)合,我們跟進(jìn)代碼可以看看spring-boot是在什么時(shí)候創(chuàng)建的server
一、SpringApplication
public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; CollectionexceptionReporters = new ArrayList<>(); configureHeadlessProperty(); SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); Banner printedBanner = printBanner(environment); context = createApplicationContext(); exceptionReporters = getSpringFactoriesInstances( SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } listeners.started(context); callRunners(context, applicationArguments); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, listeners); throw new IllegalStateException(ex); } try { listeners.running(context); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, null); throw new IllegalStateException(ex); } return context; }
我們只分析入口,其它代碼暫時(shí)不管,找到refreshContext(context);這一行進(jìn)去
二、ReactiveWebServerApplicationContext的refresh()
@Override public final void refresh() throws BeansException, IllegalStateException { try { super.refresh(); } catch (RuntimeException ex) { stopAndReleaseReactiveWebServer(); throw ex; } }
三、ReactiveWebServerApplicationContext的onRefresh()
@Override protected void onRefresh() { super.onRefresh(); try { createWebServer(); } catch (Throwable ex) { throw new ApplicationContextException("Unable to start reactive web server", ex); } }
四、看到這里我們就找到入口方法了:createWebServer(),跟進(jìn)去,找到NettyReactiveWebServerFactory中創(chuàng)建webserver
@Override public WebServer getWebServer(HttpHandler httpHandler) { HttpServer httpServer = createHttpServer(); ReactorHttpHandlerAdapter handlerAdapter = new ReactorHttpHandlerAdapter( httpHandler); return new NettyWebServer(httpServer, handlerAdapter, this.lifecycleTimeout); }
看到ReactorHttpHandlerAdapter這個(gè)類想必特別親切,在開篇說(shuō)過(guò)是spring-cloud-gateway的入口,createHttpServer方法的細(xì)節(jié)暫時(shí)沒有去學(xué)習(xí)了,后續(xù)有時(shí)間去深入了解下
結(jié)語(yǔ)spring5的相關(guān)新特性也是在學(xué)習(xí)中,這一篇文章算是和springboot結(jié)合的入門吧,后續(xù)有時(shí)間再深入學(xué)習(xí)
更多文章可以訪問(wèn)博客:
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/74417.html
摘要:響應(yīng)式編程是基于異步和事件驅(qū)動(dòng)的非阻塞程序,只是垂直通過(guò)在內(nèi)啟動(dòng)少量線程擴(kuò)展,而不是水平通過(guò)集群擴(kuò)展。三特性常用的生產(chǎn)的特性如下響應(yīng)式編程模型適用性內(nèi)嵌容器組件還有對(duì)日志消息測(cè)試及擴(kuò)展等支持。 摘要: 原創(chuàng)出處 https://www.bysocket.com 「公眾號(hào):泥瓦匠BYSocket 」歡迎關(guān)注和轉(zhuǎn)載,保留摘要,謝謝! 02:WebFlux 快速入門實(shí)踐 文章工程: JDK...
摘要:在配置下上面啟動(dòng)的配置數(shù)據(jù)庫(kù)名為賬號(hào)密碼也為。突出點(diǎn)是,即非阻塞的。四對(duì)象修改包里面的城市實(shí)體對(duì)象類。修改城市對(duì)象,代碼如下城市實(shí)體類城市編號(hào)省份編號(hào)城市名稱描述注解標(biāo)記對(duì)應(yīng)庫(kù)表的主鍵或者唯一標(biāo)識(shí)符。 摘要: 原創(chuàng)出處 https://www.bysocket.com 「公眾號(hào):泥瓦匠BYSocket 」歡迎關(guān)注和轉(zhuǎn)載,保留摘要,謝謝! 這是泥瓦匠的第104篇原創(chuàng) 文章工程: JDK...
摘要:上一章我們提到過(guò)與,對(duì)于具體的介紹沒說(shuō)到,這一章我在這里簡(jiǎn)單介紹一下,既然提到和,那肯定得提到什么是響應(yīng)式編程,什么是。 showImg(https://segmentfault.com/img/remote/1460000018819338?w=1024&h=500); 上一章我們提到過(guò)Mono 與 Flux,對(duì)于具體的介紹沒說(shuō)到,這一章我在這里簡(jiǎn)單介紹一下,既然提到Mono和Flu...
摘要:數(shù)據(jù)和信息是不可分離的,數(shù)據(jù)是信息的表達(dá),信息是數(shù)據(jù)的內(nèi)涵。數(shù)據(jù)本身沒有意義,數(shù)據(jù)只有對(duì)實(shí)體行為產(chǎn)生影響時(shí)才成為信息。主要目標(biāo)是為開發(fā)提供天然的模板,并且能在里面準(zhǔn)確的顯示。目前是自然更加推薦。 這是泥瓦匠的第105篇原創(chuàng) 文章工程: JDK 1.8 Maven 3.5.2 Spring Boot 2.1.3.RELEASE 工程名:springboot-webflux-4-thym...
摘要:二結(jié)構(gòu)這個(gè)工程會(huì)對(duì)城市進(jìn)行管理實(shí)現(xiàn)操作。負(fù)責(zé)將持久層數(shù)據(jù)操作相關(guān)的封裝組織,完成新增查詢刪除等操作。原因是,直接使用和是非阻塞寫法,相當(dāng)于回調(diào)方式。反應(yīng)了是的好處集合了非阻塞異步。其實(shí)是的一個(gè)補(bǔ)充??梢园l(fā)布類型的元素。 摘要: 原創(chuàng)出處 https://www.bysocket.com 「公眾號(hào):泥瓦匠BYSocket 」歡迎關(guān)注和轉(zhuǎn)載,保留摘要,謝謝! 這是泥瓦匠的第102篇原創(chuàng) 0...
閱讀 3529·2021-10-08 10:04
閱讀 877·2019-08-30 15:54
閱讀 2191·2019-08-29 16:09
閱讀 1359·2019-08-29 15:41
閱讀 2287·2019-08-29 11:01
閱讀 1747·2019-08-26 13:51
閱讀 1037·2019-08-26 13:25
閱讀 1839·2019-08-26 13:24