本文共 8795 字,大约阅读时间需要 29 分钟。
不久前, spring进行了较大的改动, 主要目的是为了增加对响应式编程的支持.
spring 默认是采用了reactor项目作为响应式编程(reactive programming)的支持, 我也以此作为基础来谈. reactor项目地址:总的来说, reactor也是一个用于编写异步代码的库, 众所周知, 对于同步程序来说, 有IO耗时长之类的开销. 所以人们不断的推崇使用异步的方式来编写一些代码, 而java也提供了编写异步程序的方法给开发者, 那么我们为什么需要reactor. 就我短时间的使用体验来说, reactor使我们编写异步代码变得更加简单快捷, 让某项工作更加简单或让其更有效率, 我觉得就是一个库应该解决的问题, 显然reactor做到了, 在使用了reactor后, 你就再也不用写callback那种又臭又长的面条代码了, 代码的可读性与可维护性大大加强了. 相比于future, reactor又提供了更多功能齐全的操作, 编程复杂的也大大降低
好了, 我们并不是来介绍reactor的, 更多有关reactor的资料以及它与jvm其他异步方式的对比请参考reactor文档:webmvc | webflux |
---|---|
controller | handler |
request mapping | router |
* pom.xml
4.0.0 cn.edu.ncu reactive-demo 0.0.1-SNAPSHOT jar reactive-demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.0.M7 UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-webflux org.springframework.boot spring-boot-starter-data-redis redis.clients jedis 2.9.0 org.mindrot jbcrypt 0.4 com.alibaba fastjson 1.2.44 org.springframework.boot spring-boot-maven-plugin spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone false spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone false
package cn.edu.ncu.reactivedemo.handlers;@Servicepublic class HelloWorldHandler { public MonohelloWorld(ServerRequest request){ return ServerResponse.ok() .contentType(MediaType.TEXT_PLAIN) .body(BodyInserters.fromObject("hello world")); }}
package cn.edu.ncu.reactivedemo;@Configurationpublic class Router { @Autowired private HelloWorldHandler helloWorldHandler; @Autowired private UserHandler userHandler; @Bean public RouterFunction routerFunction(){ return RouterFunctions.route(RequestPredicates.GET("/hello"), helloWorldHandler::helloWorld); }}
package cn.edu.ncu.reactivedemo;@SpringBootApplicationpublic class ReactiveDemoApplication { public static void main(String[] args) { SpringApplication.run(ReactiveDemoApplication.class, args); }}
访问
返回hello world表示成功暂时支持reactive编程的数据库只有MongoDB, redis, Cassandra, Couchbase
我们直接采用redis作为测试, 做一个简陋的注册登录的接口就行了 * 配置redispackage cn.edu.ncu.reactivedemo.config;@Configurationpublic class RedisConfig { @Autowired private RedisConnectionFactory factory; @Bean public ReactiveRedisTemplatereactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory){ return new ReactiveRedisTemplate (connectionFactory, RedisSerializationContext.string()); } @Bean public ReactiveRedisConnection connection(ReactiveRedisConnectionFactory connectionFactory){ return connectionFactory.getReactiveConnection(); } public @PreDestroy void flushDb(){ factory.getConnection().flushDb(); }}
package cn.edu.ncu.reactivedemo;@RunWith(SpringRunner.class)@SpringBootTest(classes = ReactiveDemoApplication.class)public class RedisTests { @Autowired private ReactiveRedisConnection connection; @Test public void testRedis(){ connection .stringCommands().set(ByteBuffer.wrap("h".getBytes()), ByteBuffer.wrap("w".getBytes())) .subscribe(System.out::println); }}
package cn.edu.ncu.reactivedemo.handlers;@Servicepublic class UserHandler { @Autowired private ReactiveRedisConnection connection; public Monoregister(ServerRequest request) { Mono
package cn.edu.ncu.reactivedemo;@Configurationpublic class Router { @Autowired private HelloWorldHandler helloWorldHandler; @Autowired private UserHandler userHandler; @Bean public RouterFunction routerFunction(){ return RouterFunctions.route(RequestPredicates.GET("/hello"), helloWorldHandler::helloWorld) .andRoute(RequestPredicates.POST("/register"), userHandler::register) .andRoute(RequestPredicates.POST("/login"), userHandler::login); }}
接口很粗糙,没有写model层, 也没有数据验证, 测试也直接用http requester进行测试了
参考:
demo地址:
转载地址:http://sigmi.baihongyu.com/