拾贝

【Spring 5】响应式Web框架实战(下)

    backend     原创·Reactive·Spring

引子:被誉为“中国大数据第一人”的涂子沛先生在其成名作《数据之巅》里提到,摩尔定律、社交媒体、数据挖掘是大数据的三大成因。IBM的研究称,整个人类文明所获得的全部数据中,有90%是过去两年内产生的。在此背景下,包括NoSQL,Hadoop, Spark, Storm, Kylin在内的大批新技术应运而生。其中以RxJavaReactor为代表的响应式(Reactive)编程技术针对的就是经典的大数据4V定义(Volume,Variety,Velocity,Value)中的Velocity,即高并发问题,而在即将发布的Spring 5中,也引入了响应式编程的支持。在接下来的几周,我会围绕响应式编程分三期与你分享我的一些学习心得。本篇是第三篇(下),通过一个简单的Spring 5示例应用,探一探即将于下月底发布的Spring 5的究竟。

前情概要:

1 回顾

上篇介绍了如何使用Spring MVC注解实现一个响应式Web应用(以下简称RP应用),本篇接着介绍另一种实现方式——Router Functions。

2 实战

2.1 Router Functions

Router Functions是Spring 5新引入的一套Reactive风格(基于Flux和Mono)的函数式接口,主要包括RouterFunctionHandlerFunctionHandlerFilterFunction,分别对应Spring MVC中的@RequestMapping@ControllerHandlerInterceptor(或者Servlet规范中的Filter)。

和Router Functions搭配使用的是两个新的请求/响应模型,ServerRequestServerResponse,这两个模型同样提供了Reactive风格的接口。

2.2 示例代码

下面接着看我GitHub上的示例工程里的例子。

2.2.1 自定义RouterFunction和HandlerFilterFunction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@Configuration
public class RestaurantServer implements CommandLineRunner {

@Autowired
private RestaurantHandler restaurantHandler;

/**
* 注册自定义RouterFunction
*/
@Bean
public RouterFunction<ServerResponse> restaurantRouter() {
RouterFunction<ServerResponse> router = route(GET("/reactive/restaurants").and(accept(APPLICATION_JSON_UTF8)), restaurantHandler::findAll)
.andRoute(GET("/reactive/delay/restaurants").and(accept(APPLICATION_JSON_UTF8)), restaurantHandler::findAllDelay)
.andRoute(GET("/reactive/restaurants/{id}").and(accept(APPLICATION_JSON_UTF8)), restaurantHandler::get)
.andRoute(POST("/reactive/restaurants").and(accept(APPLICATION_JSON_UTF8)).and(contentType(APPLICATION_JSON_UTF8)), restaurantHandler::create)
.andRoute(DELETE("/reactive/restaurants/{id}").and(accept(APPLICATION_JSON_UTF8)), restaurantHandler::delete)
// 注册自定义HandlerFilterFunction
.filter((request, next) -> {
if (HttpMethod.PUT.equals(request.method())) {
return ServerResponse.status(HttpStatus.BAD_REQUEST).build();
}
return next.handle(request);
});
return router;
}

@Override
public void run(String... args) throws Exception {
RouterFunction<ServerResponse> router = restaurantRouter();
// 转化为通用的Reactive HttpHandler
HttpHandler httpHandler = toHttpHandler(router);
// 适配成Netty Server所需的Handler
ReactorHttpHandlerAdapter httpAdapter = new ReactorHttpHandlerAdapter(httpHandler);
// 创建Netty Server
HttpServer server = HttpServer.create("localhost", 9090);
// 注册Handler并启动Netty Server
server.newHandler(httpAdapter).block();
}
}

可以看到,使用Router Functions实现RP应用时,你需要自己创建和管理容器,也就是说Spring 5并没有针对Router Functions提供IoC支持,这是Router Functions和Spring MVC相比最大的不同。除此之外,你需要通过RouterFunction的API(而不是注解)来配置路由表和过滤器。对于简单的应用,这样做问题不大,但对于上规模的应用,就会导致两个问题:1)Router的定义越来越庞大;2)由于URI和Handler分开定义,路由表的维护成本越来越高。那为什么Spring 5会选择这种方式定义Router呢?接着往下看。

2.2.2 自定义HandlerFunction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@Component
public class RestaurantHandler {

/**
* 扩展ReactiveCrudRepository接口,提供基本的CRUD操作
*/
private final RestaurantRepository restaurantRepository;

/**
* spring-boot-starter-data-mongodb-reactive提供的通用模板
*/
private final ReactiveMongoTemplate reactiveMongoTemplate;

public RestaurantHandler(RestaurantRepository restaurantRepository, ReactiveMongoTemplate reactiveMongoTemplate) {
this.restaurantRepository = restaurantRepository;
this.reactiveMongoTemplate = reactiveMongoTemplate;
}

public Mono<ServerResponse> findAll(ServerRequest request) {
Flux<Restaurant> result = restaurantRepository.findAll();
return ok().contentType(APPLICATION_JSON_UTF8).body(result, Restaurant.class);
}

public Mono<ServerResponse> findAllDelay(ServerRequest request) {
Flux<Restaurant> result = restaurantRepository.findAll().delayElements(Duration.ofSeconds(1));
return ok().contentType(APPLICATION_JSON_UTF8).body(result, Restaurant.class);
}

public Mono<ServerResponse> get(ServerRequest request) {
String id = request.pathVariable("id");
Mono<Restaurant> result = restaurantRepository.findById(id);
return ok().contentType(APPLICATION_JSON_UTF8).body(result, Restaurant.class);
}

public Mono<ServerResponse> create(ServerRequest request) {
Flux<Restaurant> restaurants = request.bodyToFlux(Restaurant.class);
Flux<Restaurant> result = restaurants
.buffer(10000)
.flatMap(rs -> reactiveMongoTemplate.insert(rs, Restaurant.class));
return ok().contentType(APPLICATION_JSON_UTF8).body(result, Restaurant.class);
}

public Mono<ServerResponse> delete(ServerRequest request) {
String id = request.pathVariable("id");
Mono<Void> result = restaurantRepository.deleteById(id);
return ok().contentType(APPLICATION_JSON_UTF8).build(result);
}
}

对比上篇RestaurantController,主要有两点区别:

  • 所有方法的参数和返回值类型固定为ServerRequest和Mono以符合HandlerFunction的定义,所有请求相关的对象(queryParam, pathVariable,header, session等)都通过ServerRequest获取。
  • 由于去除了路由信息,RestaurantHandler变得非常函数化,可以说就是一组相关的HandlerFunction的集合,同时各个方法的可复用性也大为提升。这就回答了上一小节提出的疑问,即以牺牲可维护性为代价,换取更好的函数特性。

2.3 单元测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RunWith(SpringRunner.class)
@SpringBootTest
public class RestaurantHandlerTests extends BaseUnitTests {

@Autowired
private RouterFunction<ServerResponse> restaurantRouter;

@Override
protected WebTestClient prepareClient() {
WebTestClient webClient = WebTestClient.bindToRouterFunction(restaurantRouter)
.configureClient().baseUrl("http://localhost:9090").responseTimeout(Duration.ofMinutes(1)).build();
return webClient;
}
}

和针对Controller的单元测试相比,编写Handler的单元测试的主要区别在于初始化WebTestClient方式的不同,测试方法的主体可以完全复用。

3 小结

到此,有关响应式编程的介绍就暂且告一段落。回顾这四篇文章,我先是从响应式宣言说起,然后介绍了响应式编程的基本概念和关键特性,并且详解了Spring 5中和响应式编程相关的新特性,最后以一个示例应用结尾。希望读完这些文章,对你理解响应式编程能有所帮助。欢迎你到我的留言板分享,和大家一起过过招。

4 参考