前言

Redis默认有16个库,默认连接的是index=0的那一个。这16个库直接是相互独立的。

一、在命令行中切换

select 1;

二、在Spring中如何切换

1、在RedisConnectionCommands中使用redisConnection.select(1);

2、在配置文件中设置(JedisConnectionFactory)

<!-- jedis的连接工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="database" value="${redis.database}"/>
<property name="password" value="${redis.pass}"/>
<property name="timeout" value="2000"/>
<property name="poolConfig" ref="poolConfig"/>
</bean>
/**
* Sets the index of the database used by this connection factory. Default is 0.
*
* @param index database index
*/
public void setDatabase(int index) {
Assert.isTrue(index >= 0, "invalid DB index (a positive index required)");
this.dbIndex = index;
}

spring-redis.xml完整配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
"> <!--jedis的连接池配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲连接数量 -->
<property name="maxIdle" value="${redis.maxIdle}"/>
<!-- 最小空闲连接数量, 处理间隔时间为 timeBetweenEvictionRunsMillis -->
<property name="minIdle" value="${redis.minIdle}"/>
<!-- 池中持有的最大连接数量 -->
<property name="maxTotal" value="${redis.maxTotal}"/>
<!-- borrowObject 方法的最大等待时间 -->
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<!-- 池中可用资源耗尽时, borrow 方法是否阻塞等待 maxWaitMillis 毫秒 -->
<property name="blockWhenExhausted" value="true"/>
<!-- borrowObject 时是否执行检测 -->
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
<!-- 是否检测空闲连接链接的有效性, 间隔时间为 timeBetweenEvictionRunsMillis -->
<property name="testWhileIdle" value="true"/>
<!-- 空闲对象被清除需要达到的最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/>
<!-- 空闲检测线程,sleep 间隔多长时间,去处理与idle相关的事情 -->
<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/>
</bean>
<!-- jedis的连接工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="database" value="${redis.database}"/>
<property name="password" value="${redis.pass}"/>
<property name="timeout" value="2000"/>
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!--redis实际使用的template-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
<!--spring session . 禁用 To disable the automatic configuration -->
<util:constant
static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
<!--spring session 的redis配置-->
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="3600"/>
</bean> </beans>

【Spring Boot && Spring Cloud系列】在spring-data-Redis中如何使用切换库的更多相关文章

  1. Spring Boot 2.x 系列教程:WebFlux 系列教程大纲(一)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! WebFlux 系列教程大纲 一.背景 大家都知道,Sprin ...

  2. Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...

  3. Spring Boot 2.0系列文章(七):SpringApplication 深入探索

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...

  4. Spring Boot 2.x 系列教程:WebFlux REST API 全局异常处理 Error Handling

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本文内容 为什么要全局异常处理? WebFlux REST 全 ...

  5. spring boot、cloud v2.1.0.RELEASE 使用及技术整理

    2018年10月30日 springboot v2.1.0.RELEASE 发布: https://github.com/spring-projects/spring-boot/releases/ta ...

  6. Spring Boot入门篇(基于Spring Boot 2.0系列)

    1:概述: Spring Boot是用来简化Spring应用的初始化开发过程. 2:特性: 创建独立的应用(jar|war形式); 需要用到spring-boot-maven-plugin插件 直接嵌 ...

  7. Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用

    Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用 1. 引言 在上一篇文章<Spring Boot (九): 微服务应用监控 Spring ...

  8. Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置

    Spring Boot 公共配置,配置 application.properties/application.yml 文件中. 摘自:http://docs.spring.io/spring-boot ...

  9. 47. Spring Boot发送邮件【从零开始学Spring Boot】

    (提供源代码) Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Bo ...

  10. Spring Boot 2.X(六):Spring Boot 集成Redis

    Redis 简介 什么是 Redis Redis 是目前使用的非常广泛的免费开源内存数据库,是一个高性能的 key-value 数据库. Redis 与其他 key-value 缓存(如 Memcac ...

随机推荐

  1. Git -- 基本操作 之 版本回退

    现在,你已经学会了修改文件,然后把修改提交到Git版本库,现在,再练习一次,修改readme.txt文件如下: Git is a distributed version control system. ...

  2. ASP.NET EntityFrameworkCore code first 多对多设计

    摘要:参考网址:https://docs.microsoft.com/zh-cn/ef/core/get-started/full-dotnet/new-db场景:使用ASP.NETEntityFra ...

  3. MyBatis通用Mapper技巧

    一.排序 错误代码:example.orderBy(BaseEntity.Field.GMTUpdate + " desc"); 正确方式: 一是:通过注解 @OrderBy(va ...

  4. AsmTools

    前言 https://wiki.openjdk.java.net/display/CodeTools/asmtools 在OpenJDK里有一个AsmTools项目,用来生成正确的或者不正确的java ...

  5. 【机翻】Deep Plug-and-Play Super-Resolution for Arbitrary

    论文 (PDF) Deep Plug-and-Play Super-Resolution for Arbitrary Blur Kernels https://www.researchgate.net ...

  6. docker dockerfile构建自己的tomcat镜像

    文件 1.apache-tomcat-8.5.24.tar.gz, jdk-8u151-linux-x64.tar.gz Dockerfile文件: FROM centosWORKDIR /usrCO ...

  7. redis配置文件相关

    1. 默认情况下,redis不是在后台运行的,如果需要在后台运行,把该项的值更改为yes daemonize no 2. 当Redis在后台运行时,Redis默认会把pid写入/var/run/red ...

  8. xcode 5.1打包iOS 7.1应用问题笔记

    XCODE 5.1默认情况下是要求应用都通过64位编译.但是往往有些第三方的类库还是32位.还木有更新64位类库.使得项目编译出错. 解决办法: BuildSetting 的Valid Archite ...

  9. Linux下修改当前用户的最大线程数和 open files

    1 查看当前用户的线程 ulimit -a 2 修改配置文件 vi /etc/security/limits.d/90-nproc.conf 3 改完即可生效 4 修改可打开的最大文件数 vi  /e ...

  10. Echarts调整图表上下左右的间距,Echarts调整柱状图左右的间距

    Echarts调整图表上下左右的间距,Echarts调整柱状图左右的间距 >>>>>>>>>>>>>>>> ...