SpringBoot集成Redisson操作Redis的實(shí)現(xiàn)方法
一、前言
Redisson 是一個(gè)在 Redis 的基礎(chǔ)上實(shí)現(xiàn)的 Java 駐內(nèi)存數(shù)據(jù)網(wǎng)格,Redisson相比較與Jedis和Lettuce來(lái)說(shuō)最大的區(qū)別就是,Redisson提供了很多分布式相關(guān)操作服務(wù),例如,分布式鎖,分布式集合,可通過(guò)Redis支持延遲隊(duì)列等,一般建議Lettuce + Redisson一起使用,需要使用Redis高級(jí)功能就使用Redisson,如果不需要使用高級(jí)功能優(yōu)先推薦使用Lettuce。
二、基礎(chǔ)集成配置(redis單節(jié)點(diǎn))
工程結(jié)構(gòu)
2.1、POM
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.17.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2.2、添加配置文件
import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.codec.JsonJacksonCodec; import org.redisson.config.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RedissonConfig { private String redissonUrl = "redis://172.16.8.169:6379"; private String password = "123456"; private Integer datebase = 0; @Bean public RedissonClient redisson() { Config config = new Config(); config.useSingleServer() .setAddress(redissonUrl) .setPassword((password == null || "".equals(password)) ? null : password) .setDatabase(datebase) // 連接空閑超時(shí),如果當(dāng)前連接池里的連接數(shù)量超過(guò)了最小空閑連接數(shù),而同時(shí)有連接空閑時(shí)間超過(guò)了該數(shù)值,那么這些連接將會(huì)自動(dòng)被關(guān)閉,并從連接池里去掉。時(shí)間單位是毫秒。 .setIdleConnectionTimeout(10000) // 連接超時(shí),同節(jié)點(diǎn)建立連接時(shí)的等待超時(shí)。時(shí)間單位是毫秒。 .setConnectTimeout(10000) // 命令等待超時(shí),等待節(jié)點(diǎn)回復(fù)命令的時(shí)間。該時(shí)間從命令發(fā)送成功時(shí)開(kāi)始計(jì)時(shí)。 .setTimeout(1000) // 命令失敗重試次數(shù),如果嘗試達(dá)到 retryAttempts(命令失敗重試次數(shù)) 仍然不能將命令發(fā)送至某個(gè)指定的節(jié)點(diǎn)時(shí),將拋出錯(cuò)誤。如果嘗試在此限制之內(nèi)發(fā)送成功,則開(kāi)始啟用 timeout(命令等待超時(shí)) 計(jì)時(shí) .setRetryAttempts(3) // 命令重試發(fā)送時(shí)間間隔,在一條命令發(fā)送失敗以后,等待重試發(fā)送的時(shí)間間隔。時(shí)間單位是毫秒。 .setRetryInterval(1500) // 每個(gè)連接的最大訂閱數(shù)量。 .setSubscriptionsPerConnection(5) // 用于發(fā)布和訂閱連接的最小保持連接數(shù)(長(zhǎng)連接)。Redisson內(nèi)部經(jīng)常通過(guò)發(fā)布和訂閱來(lái)實(shí)現(xiàn)許多功能。長(zhǎng)期保持一定數(shù)量的發(fā)布訂閱連接是必須的。 .setSubscriptionConnectionMinimumIdleSize(1) // 用于發(fā)布和訂閱連接的連接池最大容量。連接池的連接數(shù)量自動(dòng)彈性伸縮。 .setSubscriptionConnectionPoolSize(50) // 最小保持連接數(shù)(長(zhǎng)連接)。長(zhǎng)期保持一定數(shù)量的連接有利于提高瞬時(shí)寫(xiě)入反應(yīng)速度。 .setConnectionMinimumIdleSize(50) // 連接池最大容量。連接池的連接數(shù)量自動(dòng)彈性伸縮。 .setConnectionPoolSize(100) // 監(jiān)測(cè)DNS的變化情況的時(shí)間間隔。時(shí)間單位是毫秒。 .setDnsMonitoringInterval(5000) // PING 心跳時(shí)間,單位毫秒。 .setPingConnectionInterval(10000); // 0 cpu * 2 config.setThreads(0); // 0 cpu * 2 config.setNettyThreads(0); // 使用json序列化方式 config.setCodec(new JsonJacksonCodec()); //創(chuàng)建客戶端(發(fā)現(xiàn)創(chuàng)建RedissonClient非常耗時(shí),基本在2秒-4秒左右) return Redisson.create(config); } }
2.3、添加啟動(dòng)類
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RedissonApplication { public static void main(String[] args) { SpringApplication.run(RedissonApplication.class); } }
2.4、添加測(cè)試類測(cè)試redisson操作redis
import org.junit.Test; import org.junit.runner.RunWith; import org.redisson.api.RBucket; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = RedissonApplication.class) public class RedissonTest { @Autowired private RedissonClient redissonClient; @Test public void t1(){ String key = "key1"; System.out.println("獲取Bucket"); RBucket<Object> bucket = redissonClient.getBucket(key); System.out.println("插入數(shù)據(jù)到redis"); bucket.set("value1"); Object value = bucket.get(); System.out.println("從redis中獲取到值為 "+value); Boolean delete = bucket.delete(); System.out.println("刪除redis中值 "+delete); } }
到此這篇關(guān)于SpringBoot集成Redisson操作Redis的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)SpringBoot Redisson操作Redis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot統(tǒng)一數(shù)據(jù)返回格式的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot統(tǒng)一數(shù)據(jù)返回格式,它提高了代碼的可維護(hù)性和一致性,并改善了客戶端與服務(wù)端之間的通信,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05Spring中使用Async進(jìn)行異步功能開(kāi)發(fā)實(shí)戰(zhàn)示例(大文件上傳為例)
本文以大文件上傳為例,首先講解在未進(jìn)行程序異步化時(shí),程序的運(yùn)行機(jī)制和具體表現(xiàn),然后講解如何進(jìn)行異步化的改造,讓程序進(jìn)行異步執(zhí)行,通過(guò)本文不僅能讓你掌握如何進(jìn)行Event的事件開(kāi)發(fā),同時(shí)還能掌握在Spring中如何進(jìn)行異步開(kāi)發(fā),熟悉@Async的具體用法,感興趣的朋友一起看看吧2024-08-08Mybatis的SqlRunner執(zhí)行流程實(shí)現(xiàn)
MyBatis提供了一個(gè)用于操作數(shù)據(jù)庫(kù)的SqlRunner工具類,對(duì)JDBC做了很好的封裝,本文主要介紹了Mybatis的SqlRunner執(zhí)行流程實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10Spring擴(kuò)展之基于HandlerMapping實(shí)現(xiàn)接口灰度發(fā)布實(shí)例
這篇文章主要介紹了Spring擴(kuò)展之基于HandlerMapping實(shí)現(xiàn)接口灰度發(fā)布實(shí)例,灰度發(fā)布是指在黑與白之間,能夠平滑過(guò)渡的一種發(fā)布方式,灰度發(fā)布可以保證整體系統(tǒng)的穩(wěn)定,在初始灰度的時(shí)候就可以發(fā)現(xiàn)、調(diào)整問(wèn)題,以保證其影響度,需要的朋友可以參考下2023-08-08一文帶你認(rèn)識(shí)Java中的Object類和深淺拷貝
任何變成語(yǔ)言中,其實(shí)都有淺拷貝和深拷貝的概念,Java 中也不例外,下面這篇文章主要給大家介紹了關(guān)于Java中Object類和深淺拷貝的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04MyBatis緩存實(shí)現(xiàn)原理及代碼實(shí)例解析
這篇文章主要介紹了MyBatis緩存實(shí)現(xiàn)原理及代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Java?中?hashCode()?與?equals()?的關(guān)系(面試)
這篇文章主要介紹了Java中hashCode()與equals()的關(guān)系,ava中hashCode()和equals()的關(guān)系是面試中的??键c(diǎn),文章對(duì)hashCode與equals的關(guān)系做出詳解,需要的小伙伴可以參考一下2022-09-09Java Redis Template批量查詢指定鍵值對(duì)的實(shí)現(xiàn)
本文主要介紹了Java Redis Template批量查詢指定鍵值對(duì)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07