Springboot2.X中怎么切換redis庫,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比灞橋網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式灞橋網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋灞橋地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
#redis spring.redis.database=0 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.timeout=10000ms spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-wait=-1ms spring.redis.jedis.pool.min-idle=0 spring.redis.jedis.pool.max-idle=8
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>com.google.collections</groupId> <artifactId>google-collections</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * @Author:MuJiuTian * @Description:redis的配置,這里默認(rèn)設(shè)置使用redis:0 數(shù)據(jù)庫 * @Date: Created in 下午11:27 2019/7/4 */ @Configuration @EnableCaching public class RedisCacheConfiguration extends CachingConfigurerSupport { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private String timeout; @Value("${spring.redis.jedis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.jedis.pool.max-wait}") private String maxWaitMillis; @Value("${spring.redis.password}") private String password; @Bean public JedisPool redisPoolFactory() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(Long.valueOf(maxWaitMillis.substring(0,maxWaitMillis.length()-2))); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port,Integer.valueOf(timeout.substring(0,timeout.length()-2))); return jedisPool; } }
import java.util.List; import java.util.Map; public interface RedisService { String setex(String key, int seconds,String value,int index); String set(String key, String value,int index); String get(String key,int index); }
import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @Component public class CacheSingleService implements RedisService{ @Autowired private JedisPool jedisPool; public void returnResource(Jedis jedis) { jedis.close(); } public Jedis getResource(int index) { Jedis jedis = jedisPool.getResource(); jedis.select(index); return jedis; } public String setex(String key, int seconds, String value,int index) { Jedis jedis = null; try { jedis = getResource(index); return jedis.setex(key, seconds, value); } catch (Exception e) { e.printStackTrace(); } finally { returnResource(jedis); } return null; } public String set(String key, String value,int index){ Jedis jedis = null; try { jedis = getResource(index); return jedis.set(key, value); } finally { returnResource(jedis); } } public String get(String key,int index) { String value = null; Jedis jedis = null; try { jedis = getResource(index); value = jedis.get(key); } catch (Exception e) { e.printStackTrace(); } finally { returnResource(jedis); } return value; } }
/** * @Author:MuJiuTian * @Description:通過枚舉類選擇redis使用的數(shù)據(jù)庫,一般公司的redis會(huì)分不同的數(shù)據(jù)到不同的庫,比如用戶信息到0庫 * 商品數(shù)據(jù)放在第二個(gè)庫,運(yùn)動(dòng)數(shù)據(jù)放在第三庫等等跟隨項(xiàng)目分配而分配 * @Date: Created in 下午10:13 2019/7/29 */ public enum RedisPartition { //以下分別代表redis 0-15庫 INFO(0), One(1), Two(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), ELEVEN(11), TWELVE(12), THIRTEEN(13), FOURTEEN(14), FIFTEEN(15); private int ordinal; //構(gòu)造方法 RedisPartition(int ordinal) { this.ordinal = ordinal; } public void setOrdinal(int dbNum) { this.ordinal = dbNum; } public int getOrdinal() { return ordinal; } @Override public String toString() { return ""+ordinal; } }
import lombok.extern.slf4j.Slf4j; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; @RestController @Slf4j public class TestController { @Autowired CacheSingleService singleService; @GetMapping("/test3") public String test3() { String key = "osc"; String str = "i love osc"; //存放在5數(shù)據(jù)庫,默認(rèn)是0 singleService.set(key,"iloveyou",RedisPartition.SIX.getOrdinal()); //存放在4數(shù)據(jù)庫,默認(rèn)是0 singleService.set(str+"ii","iloveyou",RedisPartition.FIVE.getOrdinal()); return "SUCCESS"; } @GetMapping(value = "/test4") public String test4(){ String key = "osc"; String str = "i love osc"; //查詢5數(shù)據(jù)庫內(nèi)容 String str1 = singleService.get(key,RedisPartition.SIX.getOrdinal()); //查詢4數(shù)據(jù)庫內(nèi)容 String str2 = singleService.get(str,RedisPartition.FIVE.getOrdinal()); return str1+str2; } public static void main(String[] args) { System.out.println(RedisPartition.FIFTEEN.getOrdinal()); } }
使用Redis自帶方法同時(shí)使用16個(gè)數(shù)據(jù)庫,繼續(xù)使用上面的pom文件依賴
切記:接下來這個(gè)適用于Springboot2.1.X的版本,包括2.0.X,2.2.X的版本目前通過以下不能實(shí)現(xiàn)數(shù)據(jù)庫的切換。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisUtil { @Autowired private StringRedisTemplate redisTemplate; public void setRedisTemplate(StringRedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } public void setDataBase(int num) { LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory(); if (connectionFactory != null && num != connectionFactory.getDatabase()) { connectionFactory.setDatabase(num); this.redisTemplate.setConnectionFactory(connectionFactory); connectionFactory.resetConnection(); } } public StringRedisTemplate getRedisTemplate() { return this.redisTemplate; } public void set(String key, String value) { redisTemplate.opsForValue().set(key, value); } //獲取指定key的值 public String get(String key) { return redisTemplate.opsForValue().get(key); } }
@Autowired RedisUtil redisUtil; @GetMapping("/test") public String test(String key) { for (int i = 1; i < 3; i++) { redisUtil.setDataBase(i); redisUtil.set(key, "" + i + key); } //開發(fā)環(huán)境用下面的代碼,上面純簡單測(cè)試去不同的redis數(shù)據(jù)庫而已 //redisUtil.setDataBase(RedisPartition.EIGHT.getOrdinal()); return redisUtil.get(key); }
兩種方式j(luò)edis和StringRedisTemplate切換數(shù)據(jù)庫,我更喜歡用jedis,使用SringRedisTemplate我就沒有過多的把所有redis方法復(fù)制出來。
下面的是ssm框架切換同時(shí)使用redis數(shù)據(jù)庫的配置,至于redis.xml和redis.properties從網(wǎng)上copy一下吧
@Resource(name="stringRedisTemplate") private StringRedisTemplate stringRedisTemplate; private StringRedisTemplate choseConnection(RedisConnectionEnum redisEnum){ JedisConnectionFactory factory = (JedisConnectionFactory) stringRedisTemplate.getConnectionFactory(); factory.setDatabase(redisEnum.getDbNum()); stringRedisTemplate.setConnectionFactory(factory); return stringRedisTemplate; }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
網(wǎng)站欄目:Springboot2.X中怎么切換redis庫
URL地址:http://www.rwnh.cn/article4/gposoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、企業(yè)網(wǎng)站制作、網(wǎng)站制作、小程序開發(fā)、面包屑導(dǎo)航、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)