资讯专栏INFORMATION COLUMN

Spring webflux 函数式编程web框架

Eastboat / 1306人阅读

摘要:是一个全新的非堵塞的函数式框架,可以用来构建异步的非堵塞的事件驱动的服务。上面是一个简单的只相应了一个字符串上面是对应的对应的是匹配一个方式的请求,然后调用中的方法向浏览器输出一个文本类型的字符串再来一个例子账号或密码错误无效

Spring webflux

Spring 5.0 Spring webflux 是一个全新的非堵塞的函数式 Reactive Web 框架,可以用来构建异步的、非堵塞的、事件驱动的服务。
springboot2.0发布不久,最近研究了一下springboot2.0的新特性,其中就发现了webflux。

下面是spring-flux的一个demo话不多少上代码

使用webflux和MVC的区别就是在artifacId后面加上flux

</>复制代码

  1. org.springframework.boot
  2. spring-boot-starter-parent
  3. 2.0.0.RELEASE
  4. org.springframework.boot
  5. spring-boot-starter-webflux

</>复制代码

  1. @RestController
  2. public class HelloController {
  3. @GetMapping("/hello")
  4. public String hello() {
  5. return "hello world";
  6. }
  7. }

</>复制代码

  1. 在webflux中有Handler和Router 的概念,分别与springmvc中的controllerr和equest mapper相对应,通俗的将就是handler就是真正处理请求的bean,可以在handler中编写处理请求的逻辑,而Router就是如何让请求找到对应的handler中的方法处理,下面我们来实现一个简单的handler和router。

</>复制代码

  1. @Component
  2. public class HelloWorldHandler {
  3. public Mono helloWorld(ServerRequest request){
  4. return ServerResponse.ok()
  5. .contentType(MediaType.TEXT_PLAIN)
  6. .body(BodyInserters.fromObject("hello flux"));
  7. }
  8. }

</>复制代码

  1. 上面是一个简单的handler只相应了一个“hello flux” 字符串!

</>复制代码

  1. @Configuration
  2. public class RouterConfig {
  3. @Autowired
  4. private HelloWorldHandler helloWorldHandler;
  5. @Bean
  6. public RouterFunction helloRouter() {
  7. return RouterFunctions.route(RequestPredicates.GET("/hello"), helloWorldHandler::helloWorld);
  8. }
  9. }

</>复制代码

  1. 上面是对应的router对应的是匹配一个get方式的/hello请求,然后调用helloWorldHandler中的helloWorld方法向浏览器输出一个文本类型的字符串

</>复制代码

  1. 再来一个例子

</>复制代码

  1. @Component
  2. public class UserHandler {
  3. @Autowired
  4. private ReactiveRedisConnection connection;
  5. public Mono getTime(ServerRequest request) {
  6. return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)
  7. .body(Mono.just("Now is " + new SimpleDateFormat("HH:mm:ss").format(new Date())), String.class);
  8. }
  9. public Mono getDate(ServerRequest request) {
  10. return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)
  11. .body(Mono.just("Today is " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())), String.class);
  12. }
  13. public Mono sendTimePerSec(ServerRequest request) {
  14. return ServerResponse.ok().contentType(MediaType.TEXT_EVENT_STREAM)
  15. .body(Flux.interval(Duration.ofSeconds(1)).map(l -> new SimpleDateFormat("HH:mm:ss").format(new Date())), String.class);
  16. }
  17. public Mono register(ServerRequest request) {
  18. Mono body = request.bodyToMono(Map.class);
  19. return body.flatMap(map -> {
  20. String username = (String) map.get("username");
  21. String password = (String) map.get("password");
  22. String hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());
  23. return connection.stringCommands()
  24. .set(ByteBuffer.wrap(username.getBytes()), ByteBuffer.wrap(hashedPassword.getBytes()));
  25. }).flatMap(aBoolean -> {
  26. Map result = new HashMap<>();
  27. ServerResponse serverResponse = null;
  28. if (aBoolean){
  29. result.put("message", "successful");
  30. return ServerResponse.ok()
  31. .contentType(MediaType.APPLICATION_JSON_UTF8)
  32. .body(BodyInserters.fromObject(result));
  33. }else {
  34. result.put("message", "failed");
  35. return ServerResponse.status(HttpStatus.BAD_REQUEST)
  36. .contentType(MediaType.APPLICATION_JSON_UTF8)
  37. .body(BodyInserters.fromObject(request));
  38. }
  39. });
  40. }
  41. public Mono login(ServerRequest request) {
  42. Mono body = request.bodyToMono(Map.class);
  43. return body.flatMap(map -> {
  44. String username = (String) map.get("username");
  45. String password = (String) map.get("password");
  46. return connection.stringCommands().get(ByteBuffer.wrap(username.getBytes())).flatMap(byteBuffer -> {
  47. byte[] bytes = new byte[byteBuffer.remaining()];
  48. byteBuffer.get(bytes, 0, bytes.length);
  49. String hashedPassword = null;
  50. try {
  51. hashedPassword = new String(bytes, "UTF-8");
  52. } catch (UnsupportedEncodingException e) {
  53. e.printStackTrace();
  54. }
  55. Map result = new HashMap<>();
  56. if (hashedPassword == null || !BCrypt.checkpw(password, hashedPassword)) {
  57. result.put("message", "账号或密码错误");
  58. return ServerResponse.status(HttpStatus.UNAUTHORIZED)
  59. .contentType(MediaType.APPLICATION_JSON_UTF8)
  60. .body(BodyInserters.fromObject(result));
  61. } else {
  62. result.put("token", "无效token");
  63. return ServerResponse.ok()
  64. .contentType(MediaType.APPLICATION_JSON_UTF8)
  65. .body(BodyInserters.fromObject(result));
  66. }
  67. });
  68. });
  69. }
  70. }

</>复制代码

  1. @Configuration
  2. public class RouterConfig {
  3. @Autowired
  4. private HelloWorldHandler helloWorldHandler;
  5. @Bean
  6. public RouterFunction helloRouter() {
  7. return RouterFunctions.route(RequestPredicates.GET("/hello"), helloWorldHandler::helloWorld);
  8. }
  9. @Autowired
  10. private UserHandler userHandler;
  11. @Bean
  12. public RouterFunction timerRouter() {
  13. return RouterFunctions.route(RequestPredicates.GET("/time"), userHandler::getTime)
  14. .andRoute(RequestPredicates.GET("/date"), userHandler::getDate);
  15. }
  16. @Bean
  17. public RouterFunction routerFunction() {
  18. return RouterFunctions.route(RequestPredicates.GET("/hello"), helloWorldHandler::helloWorld)
  19. .andRoute(RequestPredicates.POST("/register"), userHandler::register)
  20. .andRoute(RequestPredicates.POST("/login"), userHandler::login)
  21. .andRoute(RequestPredicates.GET("/times"), userHandler::sendTimePerSec);
  22. }
  23. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/74228.html

相关文章

  • Spring Boot 2 快速教程:WebFlux 快速入门(二)

    摘要:响应式编程是基于异步和事件驱动的非阻塞程序,只是垂直通过在内启动少量线程扩展,而不是水平通过集群扩展。三特性常用的生产的特性如下响应式编程模型适用性内嵌容器组件还有对日志消息测试及扩展等支持。 摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 02:WebFlux 快速入门实践 文章工程: JDK...

    gaara 评论0 收藏0
  • Spring Boot 2.x 系列教程:WebFlux 系列教程大纲(一)

    摘要:使用则需要及以上版本。开发使用框架七系列教程目录系列教程大纲快速入门实践实践整合整合中和实践整合中实现缓存中实现通信集成测试及部署实战图书管理系统 WebFlux 系列教程大纲 一、背景 大家都知道,Spring Framework 是 Java/Spring 应用程序跨平台开发框架,也是 Java EE(Java Enterprise Edition) 轻量级框架,其 Spring ...

    jone5679 评论0 收藏0
  • 《Java编程方法论:响应RxJava与代码设计实战》序

    摘要:原文链接编程方法论响应式与代码设计实战序,来自于微信公众号次灵均阁正文内容在一月的架构和设计趋势报告中,响应式编程和函数式仍旧编列在第一季度的早期采纳者中。 原文链接:《Java编程方法论:响应式RxJava与代码设计实战》序,来自于微信公众号:次灵均阁 正文内容 在《2019 一月的InfoQ 架构和设计趋势报告》1中,响应式编程(Reactive Programming)和函数式...

    PAMPANG 评论0 收藏0
  • SpringBoot Kotlin 系列之HTML与WebFlux

    摘要:上一章我们提到过与,对于具体的介绍没说到,这一章我在这里简单介绍一下,既然提到和,那肯定得提到什么是响应式编程,什么是。 showImg(https://segmentfault.com/img/remote/1460000018819338?w=1024&h=500); 上一章我们提到过Mono 与 Flux,对于具体的介绍没说到,这一章我在这里简单介绍一下,既然提到Mono和Flu...

    crossoverJie 评论0 收藏0
  • 华为官方首发Spring响应微服务,Spring+Boot+Cloud三管齐下

    摘要:今天小编就来分享一份华为刚刚首发的响应式微服务实战这份主要包含响应式微服务架构实现过程中所应具备的技术体系和工程实践,在组织结构上分如下篇。 今天小编就来分享一份华为刚刚首发的Spring响应式微服务(Spring Boot 2+Spring 5+Spring Cloud实战)! 这份PDF...

    cangck_X 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<