首页 > 编程知识 正文

completablefuture 详解,响应式编程方案

时间:2023-05-03 20:36:35 阅读:21411 作者:3259

本文介绍了响应编程对Redis的访问,以及Spring Boot和Lettuce的集成使用。

Lettuce是一种同步、异步、可扩展、线程安全的Redis客户端。 如果有多个线程避免阻止或事务处理操作,如BLPOP或MULTI/EXEC,则可以共享连接。 Lettuce是基于Netty构建的。 支持许多高级Redis特性。

根据Spring Data Redis 2.0更新,Spring Data Redis不再支持JRedis驱动程序,并且使用Lettuce支持响应连接,因此需要了解Lettuce的使用。

使用Reactive驱动程序连接到Redis

无论使用哪种库连接,都必须使用ReactiveRedisConnection和ReactiveRedisConnectionFactory来操作Redis或查询生存的连接。

Lettuce支持独立、Redis Sentinel和redis群集模式

ReactiveRedisConnection是与Redis通信的核心组件,ReactiveRedisConnectionFactory用于创建ReativeRedisConnection实例。

Spring Boot与Lettuce集成使用

增加依赖

? XML版本=' 1.0 '编码=' utf-8 '? project xmlns=' http://maven.Apache.org/POM/4.0.0 ' xmlns : xsi=' http://www.w3.org/2001/XML方案ins ttins maven-4.0.0.xsd ' model版本4.0.0/modelversionparentgroupidorder groupidartifactidspring-boot-starter-parararer --- lookupparentfromrepository---- parentgroupidcom.example/groupidartifactidspring-reactive-demo/artifactidved namedescriptiondemoprojectforspringboot/descriptionpropertiesjava.version 11/Java.propertiesdependenciesdependencygroupava groupidartifactidspring-boot-starter-data-redis ependencydependencygroupidorg.spring framework.boot/groupidartidartifactior artifact id/ependencydependencygroupidorg.spring framework.boot/groupidartifactidspring-boot-starter-test/artifactidscopetesest ependencydependencygroupidio.projectreactor/groupidartifactidreactor-test/artifact id/从属关系组组组址列表框/Artifactidoptionaltrue /选项/从属关系/dependenciesbuildpluginsplugingroupidorg.springfroupid.spring groupidartifactidspring-boot-maven-plugin/artifact id

@ configurationpublicclasslettuceconfig { @ beanreactiveredistemplatestring, stringreactiveredistemplate (reactiveredisconnectionfactoryreactiveredisconnectionfactory (returnnewreactiveredistemplate ) )

@ restcontrollerpublicclasslettucecontroller { @ autowiredprivatereactiveredistemplatereactiveredistemplate; @getmapping(/put ) ) publicmonoput (request param ) ) key ) String key, @requestparam ) (val ) ) String val ) ) retet }@getmapping(/get ) ) publicmonostringget ) @requestparam () ) key ) stry @getmapping((/addlist ) ) publicmonolongaddlist ) ) requestparam ) ) key ) String key, @requestparam ) ) val ) ) strring }@getmapping(/getlist ) publicfluxstringgetlist ) requestparam (密钥) String key ) Rey sethash ) ) publicmonobooleansethash ) )请求参数)密钥) string密钥,@requestparam ) ) hash密钥@请求参数(val ) }@getmapping(/gethash ) ) public FluxMap.EntryString,stringgethash ) (requestparam ) (key ) String key ) ) return reacy 还有很多其他操作类型,可以在官方文章中自己查。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。