摘要:是一個全新的非堵塞的函數(shù)式框架,可以用來構(gòu)建異步的非堵塞的事件驅(qū)動的服務(wù)。上面是一個簡單的只相應(yīng)了一個字符串上面是對應(yīng)的對應(yīng)的是匹配一個方式的請求,然后調(diào)用中的方法向瀏覽器輸出一個文本類型的字符串再來一個例子賬號或密碼錯誤無效
Spring webflux
Spring 5.0 Spring webflux 是一個全新的非堵塞的函數(shù)式 Reactive Web 框架,可以用來構(gòu)建異步的、非堵塞的、事件驅(qū)動的服務(wù)。
springboot2.0發(fā)布不久,最近研究了一下springboot2.0的新特性,其中就發(fā)現(xiàn)了webflux。
使用webflux和MVC的區(qū)別就是在artifacId后面加上flux
org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE org.springframework.boot spring-boot-starter-webflux
@RestController public class HelloController { @GetMapping("/hello") public String hello() { return "hello world"; } }
在webflux中有Handler和Router 的概念,分別與springmvc中的controllerr和equest mapper相對應(yīng),通俗的將就是handler就是真正處理請求的bean,可以在handler中編寫處理請求的邏輯,而Router就是如何讓請求找到對應(yīng)的handler中的方法處理,下面我們來實現(xiàn)一個簡單的handler和router。
@Component public class HelloWorldHandler { public MonohelloWorld(ServerRequest request){ return ServerResponse.ok() .contentType(MediaType.TEXT_PLAIN) .body(BodyInserters.fromObject("hello flux")); } }
上面是一個簡單的handler只相應(yīng)了一個“hello flux” 字符串!
@Configuration public class RouterConfig { @Autowired private HelloWorldHandler helloWorldHandler; @Bean public RouterFunction> helloRouter() { return RouterFunctions.route(RequestPredicates.GET("/hello"), helloWorldHandler::helloWorld); } }
上面是對應(yīng)的router對應(yīng)的是匹配一個get方式的/hello請求,然后調(diào)用helloWorldHandler中的helloWorld方法向瀏覽器輸出一個文本類型的字符串
再來一個例子
@Component public class UserHandler { @Autowired private ReactiveRedisConnection connection; public MonogetTime(ServerRequest request) { return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN) .body(Mono.just("Now is " + new SimpleDateFormat("HH:mm:ss").format(new Date())), String.class); } public Mono getDate(ServerRequest request) { return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN) .body(Mono.just("Today is " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())), String.class); } public Mono sendTimePerSec(ServerRequest request) { return ServerResponse.ok().contentType(MediaType.TEXT_EVENT_STREAM) .body(Flux.interval(Duration.ofSeconds(1)).map(l -> new SimpleDateFormat("HH:mm:ss").format(new Date())), String.class); } public Mono register(ServerRequest request) { Mono
@Configuration public class RouterConfig { @Autowired private HelloWorldHandler helloWorldHandler; @Bean public RouterFunction> helloRouter() { return RouterFunctions.route(RequestPredicates.GET("/hello"), helloWorldHandler::helloWorld); } @Autowired private UserHandler userHandler; @Bean public RouterFunctiontimerRouter() { return RouterFunctions.route(RequestPredicates.GET("/time"), userHandler::getTime) .andRoute(RequestPredicates.GET("/date"), userHandler::getDate); } @Bean public RouterFunction> routerFunction() { return RouterFunctions.route(RequestPredicates.GET("/hello"), helloWorldHandler::helloWorld) .andRoute(RequestPredicates.POST("/register"), userHandler::register) .andRoute(RequestPredicates.POST("/login"), userHandler::login) .andRoute(RequestPredicates.GET("/times"), userHandler::sendTimePerSec); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/74228.html
摘要:響應(yīng)式編程是基于異步和事件驅(qū)動的非阻塞程序,只是垂直通過在內(nèi)啟動少量線程擴展,而不是水平通過集群擴展。三特性常用的生產(chǎn)的特性如下響應(yīng)式編程模型適用性內(nèi)嵌容器組件還有對日志消息測試及擴展等支持。 摘要: 原創(chuàng)出處 https://www.bysocket.com 「公眾號:泥瓦匠BYSocket 」歡迎關(guān)注和轉(zhuǎn)載,保留摘要,謝謝! 02:WebFlux 快速入門實踐 文章工程: JDK...
摘要:使用則需要及以上版本。開發(fā)使用框架七系列教程目錄系列教程大綱快速入門實踐實踐整合整合中和實踐整合中實現(xiàn)緩存中實現(xiàn)通信集成測試及部署實戰(zhàn)圖書管理系統(tǒng) WebFlux 系列教程大綱 一、背景 大家都知道,Spring Framework 是 Java/Spring 應(yīng)用程序跨平臺開發(fā)框架,也是 Java EE(Java Enterprise Edition) 輕量級框架,其 Spring ...
摘要:原文鏈接編程方法論響應(yīng)式與代碼設(shè)計實戰(zhàn)序,來自于微信公眾號次靈均閣正文內(nèi)容在一月的架構(gòu)和設(shè)計趨勢報告中,響應(yīng)式編程和函數(shù)式仍舊編列在第一季度的早期采納者中。 原文鏈接:《Java編程方法論:響應(yīng)式RxJava與代碼設(shè)計實戰(zhàn)》序,來自于微信公眾號:次靈均閣 正文內(nèi)容 在《2019 一月的InfoQ 架構(gòu)和設(shè)計趨勢報告》1中,響應(yīng)式編程(Reactive Programming)和函數(shù)式...
摘要:上一章我們提到過與,對于具體的介紹沒說到,這一章我在這里簡單介紹一下,既然提到和,那肯定得提到什么是響應(yīng)式編程,什么是。 showImg(https://segmentfault.com/img/remote/1460000018819338?w=1024&h=500); 上一章我們提到過Mono 與 Flux,對于具體的介紹沒說到,這一章我在這里簡單介紹一下,既然提到Mono和Flu...
摘要:今天小編就來分享一份華為剛剛首發(fā)的響應(yīng)式微服務(wù)實戰(zhàn)這份主要包含響應(yīng)式微服務(wù)架構(gòu)實現(xiàn)過程中所應(yīng)具備的技術(shù)體系和工程實踐,在組織結(jié)構(gòu)上分如下篇。 今天小編就來分享一份華為剛剛首發(fā)的Spring響應(yīng)式微服務(wù)(Spring Boot 2+Spring 5+Spring Cloud實戰(zhàn))! 這份PDF...
閱讀 3209·2021-09-30 09:47
閱讀 2080·2021-09-22 16:04
閱讀 2349·2021-09-22 15:44
閱讀 2612·2021-08-25 09:38
閱讀 605·2019-08-26 13:23
閱讀 1291·2019-08-26 12:20
閱讀 2872·2019-08-26 11:59
閱讀 1143·2019-08-23 18:40