redis是目前業界使用最廣泛的記憶體資料儲存,我們常用作資料快取。相比memcached,redis支援更豐富的資料結構,例如hash, list, set等,同時它也支援資料持久化。而且在乙個spring boot專案中,因為spring boot封裝自動化的特性,redis連線池的配置也相當簡單。
配置步驟如下:
1、在pom檔案中新增依賴
org.springframework.bootgroupid>
spring-boot-starter-redisartifactid>
dependency>
引入spring-boot-starter-redis後,我們就可以通過配置redisconnectionfactory中的相關引數去實現連線redis service。redisconnectionfactory是乙個介面,有4個具體的實現類。
2、在配置檔案中新增配置資料
這裡配置了生產環境和開發環境,專案啟動後根據環境變數自動選擇連線。
# 埠號
redis.port
.production =16379
redis.port
.develop =16379
redis.port=$}
# ip
redis.ip
.production =192.168
.100
.66redis.ip
.develop =119.29
.162
.88redis.ip=$}
# 密碼
redis.pass
.production =123456
redis.pass
.develop =123456
redis.pass=$}
# redis 配置(在配置類中使用)
# redis 資料庫索引(預設為零)
spring.redis
.database=0
# redis伺服器位址
spring.redis
.host=$
# redis伺服器連線埠
spring.redis
.port=$
# redis伺服器連線密碼
spring.redis
.password=$
# 連線池最大連線數(使用負值表示沒有限制)
spring.redis
.pool
.max-active=-1
# 連線池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis
.pool
.max-wait=-1
# 連線池中的最大空閒連線(使用負值表示沒有限制)
spring.redis
.pool
.max-idle=-1
# 連線池中的最小空閒連線
spring.redis
.pool
.min-idle=0
# 連線超時時間(毫秒)
spring.redis
.timeout=0
3、建立redis配置類
在配置檔案中配置好資料後,還需要建立乙個redis的配置類,主要用來載入配置檔案中的相關引數(具體配什麼資料按實際需求)。
/**
* *** redis資料庫連線池配置
*
*
*
*@author chendengzhu [email protected]*/* 修改記錄
*
@configuration
@enablecaching
public
class
redisconfiguration ")
private string host;
//埠號
@value("$")
private
int port;
//超時時間
@value("$")
private
int timeout;
//最大連線數
@value("$")
private
int maxidle;
//最大等待時間
@value("$")
private
long maxwaitmillis;
//密碼
@value("$")
private string password;
@bean
public jedispool redispoolfactory()
}
做完這步,redis連線池已配置完成,下面是乙個使用例子:
import redis.clients.jedis.jedis;
import redis.clients.util.pool;
@service
public
class
robotrestserviceimpl
implements
robotrestservice
catch (exception e)
finally
}}
拜拜qaq! Spring Boot系列筆記 整合Redis
autowired stringredistemplate stringredistemplate autowired redistemplate redistemplate redistemplate類中提供了redis常見的五種資料型別 stringredistemplate.opsforval...
springboot2 x基礎 整合redis
在springboot中一般使用redistemplate提供的方法來操作redis。那麼使用springboot整合redis 需要那些步驟呢。環境安裝 任選 centos7 搭建redis 5單機服務 centos7 搭建 redis 5 cluster 集群服務 在專案中新增 spring b...
Redi中Lua指令碼拓展
redis中提供了multi和exec兩個命令來使一組命令的執行能夠保證事務特性,在事務中執行的命令不會馬上執行,而是返回queued,當執行 現命令錯誤的操作時,已執行的命令不會成功。當在某些情況下,操作某個鍵時,需要該鍵在事務過程中沒有被修改才執行成功時,通過watch命令來實現,如果事務過程中...