1. Reactor简介

Reactor 是 Spring 社区发布的基于事件驱动的异步框架,不仅解耦了程序之间的强调用关系,而且有效提升了系统的多线程并发处理能力。

2. Spring Boot集成Reactor的pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>org.springframework</groupId>
  7. <artifactId>spring-boot-reactor</artifactId>
  8. <version>0.1.0</version>
  9.  
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>0.5.0.M2</version>
  14. </parent>
  15.  
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.projectreactor</groupId>
  27. <artifactId>reactor-spring</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>com.fasterxml.jackson.core</groupId>
  31. <artifactId>jackson-databind</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-web</artifactId>
  36. </dependency>
  37. </dependencies>
  38.  
  39. <properties>
  40. <start-class>org.springboot.reactor.example.App</start-class>
  41. </properties>
  42.  
  43. <build>
  44. <plugins>
  45. <plugin>
  46. <artifactId>maven-compiler-plugin</artifactId>
  47. </plugin>
  48. <plugin>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-maven-plugin</artifactId>
  51. </plugin>
  52. </plugins>
  53. </build>
  54.  
  55. <repositories>
  56. <repository>
  57. <id>spring-snapshots2</id>
  58. <url>http://repo.springsource.org/libs-snapshot</url>
  59. <snapshots>
  60. <enabled>true</enabled>
  61. </snapshots>
  62. </repository>
  63. <repository>
  64. <id>spring-release</id>
  65. <url>http://repo.springsource.org/libs-release</url>
  66. <snapshots>
  67. <enabled>false</enabled>
  68. </snapshots>
  69. </repository>
  70. </repositories>
  71. <pluginRepositories>
  72. <pluginRepository>
  73. <id>spring-snapshots</id>
  74. <url>http://repo.springsource.org/libs-snapshot</url>
  75. <snapshots>
  76. <enabled>true</enabled>
  77. </snapshots>
  78. </pluginRepository>
  79. </pluginRepositories>
  80. </project>

3. Spring Boot的主程序

  1. package org.springboot.reactor.example;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.context.annotation.Configuration;
  7.  
  8. @Configuration
  9. @EnableAutoConfiguration
  10. @ComponentScan
  11. public class App{
  12. public static void main( String[] args ) {
  13. SpringApplication.run(App.class, args);
  14. }
  15. }

4. 领域数据User

  1. package org.springboot.reactor.example;
  2.  
  3. public class User {
  4.  
  5. String firstName;
  6.  
  7. String lastName;
  8.  
  9. String address;
  10.  
  11. String city;
  12.  
  13. }

5. 实例化Reactor Bean,这里采用内部 Bean 方式实现,其他方式也可以,保证能够供其它类注入即可

  1. package org.springboot.reactor.example;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5.  
  6. import reactor.core.Environment;
  7. import reactor.core.Reactor;
  8. import reactor.core.spec.Reactors;
  9.  
  10. @Configuration
  11. public class Configure {
  12. @Bean
  13. Environment env() {
  14. return new Environment();
  15. }
  16.  
  17. @Bean
  18. Reactor createReactor(Environment env) {
  19. return Reactors.reactor()
  20. .env(env)
  21. .dispatcher(Environment.THREAD_POOL)
  22. .get();
  23. }
  24. }

6. Controller层用于通过reactor.notify发送事件

  1. package org.springboot.reactor.example;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6.  
  7. import reactor.core.Reactor;
  8. import reactor.event.Event;
  9.  
  10. @RestController
  11. @RequestMapping("/api")
  12. public class ReactorController {
  13.  
  14. @Autowired
  15. private Reactor reactor;
  16.  
  17. @RequestMapping("/test")
  18. public void test() throws InterruptedException{
  19. User user = new User();
  20. user.firstName = "Chetan";
  21. user.lastName = "Birajdar";
  22. user.address = "410 S Hauser";
  23. user.city = "Los Angeles";
  24. reactor.notify("eventHandler", Event.wrap(user));
  25. System.out.println("y0, I sent something for you!!");
  26. }
  27. }

7. 事件的监听器,以便于接收发送的事件并处理。需要实现 Consumer<Event<T>> 接口,其中 T 是处理程序接收的数据类型,要根据具体业务设置

  1. package org.springboot.reactor.example;
  2.  
  3. import org.springframework.stereotype.Service;
  4.  
  5. import reactor.event.Event;
  6. import reactor.function.Consumer;
  7.  
  8. @Service
  9. public class AppListener implements Consumer<Event<User>>{
  10.  
  11. public void accept(Event<User> event) {
  12. System.out.println("Received user object with "
  13. + "first name:"
  14. + event.getData().firstName
  15. + ", last name:"
  16. + event.getData().lastName
  17. + ", address:"
  18. + event.getData().address
  19. + ", city:"
  20. + event.getData().city
  21. + "");
  22. }
  23. }

8. ReactorService,实现InitializingBean接口,以便将发送的事件绑定到指定的监听器

  1. package org.springboot.reactor.example;
  2.  
  3. import static reactor.event.selector.Selectors.$;
  4.  
  5. import org.springframework.beans.factory.InitializingBean;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8.  
  9. import reactor.core.Reactor;
  10.  
  11. @Service
  12. public class ReactorService implements InitializingBean{
  13.  
  14. @Autowired
  15. private Reactor reactor;
  16.  
  17. @Autowired
  18. private AppListener appListener;
  19.  
  20. @Override
  21. public void afterPropertiesSet() throws Exception {
  22. reactor.on($("eventHandler"), appListener);
  23. }
  24. }

9. 当启动应用时,调用/api/test接口,向eventHandler的topic发送User事件,监听器即可接收到相关的事件并处理

Spring Boot集成Reactor事件处理框架的简单示例的更多相关文章

  1. Spring Boot集成Jasypt安全框架

    Jasypt安全框架提供了Spring的集成,主要是实现 PlaceholderConfigurerSupport类或者其子类. 在Sring 3.1之后,则推荐使用PropertySourcesPl ...

  2. Spring Boot 集成 Swagger,生成接口文档就这么简单!

    之前的文章介绍了<推荐一款接口 API 设计神器!>,今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单. 你所需具备的基础 告诉你,Spring Bo ...

  3. Spring Boot集成Springfox Swagger3和简单应用

    摘要:Springfox Swagger可以动态生成 API 接口供前后端进行交互和在线调试接口,Spring Boot 框架是目前非常流行的微服务框架,所以,在Spring Boot 项目中集成Sp ...

  4. Spring boot集成swagger2

    一.Swagger2是什么? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格 ...

  5. Quartz与Spring Boot集成使用

    上次自己搭建Quartz已经是几年前的事了,这次项目中需要定时任务,需要支持集群部署,想到比较轻量级的定时任务框架就是Quartz,于是来一波. 版本说明 通过搜索引擎很容易找到其官网,来到Docum ...

  6. Spring boot 集成Dubbox(山东数漫江湖)

    前言 因为工作原因,需要在项目中集成dubbo,所以去查询dubbo相关文档,发现dubbo目前已经不更新了,所以把目光投向了dubbox,dubbox是当当网基于dubbo二次开发的一个项目,dub ...

  7. Spring Boot 2.X(六):Spring Boot 集成Redis

    Redis 简介 什么是 Redis Redis 是目前使用的非常广泛的免费开源内存数据库,是一个高性能的 key-value 数据库. Redis 与其他 key-value 缓存(如 Memcac ...

  8. Spring Boot 快速入门 史上最简单

    1.Spring Boot 概述 Spring Boot 是所有基于 Spring 开发的项目的起点.Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的 ...

  9. spring boot 集成 sitemesh

    一.Sitemesh简介 Sitemesh是由一个基于Web页面布局.装饰及与现存Web应用整合的框架,是一个装饰器.它能帮助我们在由大量页面工程的项目中创建一致的页面布局和外观,如一致的导航条.一致 ...

随机推荐

  1. CENTOS 7 升级安装 Python 3.5

    写在前面的话 本文采取源码的方式安装 Python 3.5.2,如果是其它版本会有或多或少的差异,且写这篇的时候官网最新的是 Python 3.7,个人使用 3.5 就足够了,没必要更新到最新,否则出 ...

  2. 如何在页面中使用svg图标

    1.svg图标长啥样 注意:图标的宽高无所谓,使用时可以根据需求修改,fill后面是颜色的填充,可修改图标颜色. <svg viewBox="0 0 1024 1024" v ...

  3. BS总结篇­

    学习Web开发差不多三个月了,这个阶段的学习给自己带来的更多的是视觉的盛宴.从CS的世界一下子来到了BS的地盘,心中除了惊喜还是惊喜.在这里还是希望自己对这三月所学的东西做一个阶段性的总结. 话不多说 ...

  4. 智能合约安全事故回顾(2)-BEC溢出攻击

    讲溢出攻击之前,先给大家讲个故事:2014年的时候,美国的宾夕法尼亚州的某个小镇上发生了一个乌龙事件,征兵系统对一万多名1893年到1897出生的男子发去信函,要求他们注册参军,否则面临罚款和监禁.收 ...

  5. .NET 反射

    反射是.NET很强大的一个机制. 它就像照妖镜一般的存在.它能调用你的任意私有成员,如:私有构造函数.私有方法.私有字段. 类的构造函数声明为了private,别人无法实例化对象出来?No,No,No ...

  6. 21. sessionStorage和localStorage的使用

    sessionStorage和localStorage的使用   前言 这是学习笔记,把从别人博客里转载的https://www.cnblogs.com/wangyue99599/p/9088904. ...

  7. 圆环进度css

    看效果先:http://sandbox.runjs.cn/show/b6bmksvn 参考: jquery圆环百分比进度条制作 CSS clip:rect矩形剪裁功能及一些应用介绍 CSS clip: ...

  8. PHPExcel 报 Allowed memory size of 8388608 byte

    使用 phpExcel 报 Allowed memory size of 8388608 bytes exhausted 错误,原因是php页面消耗的最大内存默认是为 8M (在PHP的ini件里可以 ...

  9. Unity 动画系统 StateMachineBehaviour 动画状态机

  10. java程序员的从0到1:@Resource与@Autowired的比较

    目录: 1.@Resource与@Autowired的源码分析 2.@Resource与@Autowired的相同点 3.@Resource与@Autowired的不同点 正文: 1.@Resourc ...