1. Reactor简介
Reactor 是 Spring 社区发布的基于事件驱动的异步框架,不仅解耦了程序之间的强调用关系,而且有效提升了系统的多线程并发处理能力。
2. Spring Boot集成Reactor的pom.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4.0.0
5
6 org.springframework
7 spring-boot-reactor
8 0.1.0
9
10
11 org.springframework.boot
12 spring-boot-starter-parent
13 0.5.0.M2
14
15
16
17
18 org.springframework.boot
19 spring-boot-starter
20
21
22 org.springframework.boot
23 spring-boot-starter-web
24
25
26 org.projectreactor
27 reactor-spring
28
29
30 com.fasterxml.jackson.core
31 jackson-databind
32
33
34 org.springframework
35 spring-web
36
37
38
39
40 org.springboot.reactor.example.App
41
42
43
44
45
46 maven-compiler-plugin
47
48
49 org.springframework.boot
50 spring-boot-maven-plugin
51
52
53
54
55
56
57 spring-snapshots2
58 http://repo.springsource.org/libs-snapshot
59
60 true
61
62
63
64 spring-release
65 http://repo.springsource.org/libs-release
66
67 false
68
69
70
71
72
73 spring-snapshots
74 http://repo.springsource.org/libs-snapshot
75
76 true
77
78
79
80
View Code
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 }
View Code
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 }
14
View Code
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 }
View Code
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 }
28
View Code
7. 事件的监听器,以便于接收发送的事件并处理。需要实现 Consumer> 接口,其中 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>{
10
11 public void accept(Event 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 }
24
View Code
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 }
View Code
9. 当启动应用时,调用/api/test接口,向eventHandler的topic发送User事件,监听器即可接收到相关的事件并处理