springboot連接redis并動(dòng)態(tài)切換database的實(shí)現(xiàn)方法
眾所周知,redis多有個(gè)db,在jedis中可以使用select方法去動(dòng)態(tài)的選擇redis的database,但在springboot提供的StringRedisTemplate中確,沒有該方法,好在StringRedisTemplate預(yù)留了一個(gè)setConnectionFactory方法,本文主為通過修改ConnectionFactory從而達(dá)到動(dòng)態(tài)切換database的效果。
springboot連接redis
pom.xml文件中引入spring-boot-starter-redis,版本可自行選擇
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>application.properties
#redis配置 spring.redis.database=0 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=pwd spring.redis.timeout=0 spring.redis.pool.max-active=8 spring.redis.pool.max-idle=8 spring.redis.pool.max-wait=-1 spring.redis.pool.min-idle=0
TestCRedis.java
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class TestCRedis{
protected static Logger LOGGER = LoggerFactory.getLogger(TestCRedis.class);
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void t1(){
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
stringStringValueOperations.set("testkey","testvalue");
String testkey = stringStringValueOperations.get("testkey");
LOGGER.info(testkey);
}
}運(yùn)行TestCRedis.t1(),控制臺(tái)打印“testvalue”redis連接成功
redis動(dòng)態(tài)切換database
首先使用redis-cli,在redis的0、1、2三個(gè)庫中,分別設(shè)置test 的值,分別為;0、1、2
TestCRedis.java
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class TestCRedis{
protected static Logger LOGGER = LoggerFactory.getLogger(TestCRedis.class);
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void t1(){
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
stringStringValueOperations.set("testkey","testvalue");
String testkey = stringStringValueOperations.get("testkey");
LOGGER.info(testkey);
}
public void t2() {
for (int i = 0; i <= 2; i++) {
JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) stringRedisTemplate.getConnectionFactory();
jedisConnectionFactory.setDatabase(i);
stringRedisTemplate.setConnectionFactory(jedisConnectionFactory);
ValueOperations valueOperations = stringRedisTemplate.opsForValue();
String test = (String) valueOperations.get("test");
LOGGER.info(test);
}
}運(yùn)行TestCRedis.t2(),控制臺(tái)分別打印 “0、1、2”,database切換成功
到此這篇關(guān)于springboot連接redis并動(dòng)態(tài)切換database的文章就介紹到這了,更多相關(guān)springboot連接redis動(dòng)態(tài)切換database內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)自定義Redis的連接的流程步驟
- SpringBoot無法連接redis的解決方案
- springBoot連接遠(yuǎn)程Redis連接失敗的問題解決
- 關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例
- springboot連接不上redis的三種解決辦法
- springboot 如何使用jedis連接Redis數(shù)據(jù)庫
- springboot連接Redis的教程詳解
- springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池?zé)o效問題)
- 基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作
- Springboot2.X集成redis集群(Lettuce)連接的方法
- Spring?Boot2?整合連接?Redis的操作方法
相關(guān)文章
詳解如何全注解方式構(gòu)建SpringMVC項(xiàng)目
這篇文章主要介紹了詳解如何全注解方式構(gòu)建SpringMVC項(xiàng)目,利用Eclipse構(gòu)建SpringMVC項(xiàng)目,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10
簡(jiǎn)單說說JVM堆區(qū)的相關(guān)知識(shí)
今天給大家?guī)淼氖顷P(guān)于Java虛擬機(jī)的相關(guān)知識(shí),文章圍繞著JVM堆區(qū)展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Springboot+Mybatis中typeAliasesPackage正則掃描實(shí)現(xiàn)方式
這篇文章主要介紹了Springboot+Mybatis中typeAliasesPackage正則掃描實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Idea2020.2創(chuàng)建JavaWeb項(xiàng)目(部署Tomcat)方法詳解
這篇文章主要介紹了Idea2020.2創(chuàng)建JavaWeb項(xiàng)目(部署Tomcat)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Springboot 實(shí)現(xiàn)跨域訪問無需使用jsonp的實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot 實(shí)現(xiàn)跨域訪問 無需使用jsonp的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09

