单机配置
redis.properties配置
#redis的服务器地址
redis.host=127.0.0.1
#redis的服务端口
redis.port=6379
#客户端超时时间单位是毫秒
redis.timeout=100000
#最大建立连接等待时间
redis.maxWaitMillis=1000
#最小空闲数
redis.minIdle=5
#最大空闲数
redis.maxIdle=20
#最大连接数
redis.maxTotal=100
xml配置
<!-- 读取properties文件 -->
<bean id="propertyConfigurerForProject1"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath*:redis.properties</value>
</list>
</property>
</bean>
<!-- Jedis连接池的配置对象 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大空闲数 -->
<property name="maxIdle" value="${redis.maxIdle}" />
<!--最小空闲数 -->
<property name="minIdle" value="${redis.minIdle}" />
<!--最大连接数 -->
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="testOnBorrow" value="true" />
<!--最大建立连接等待时间 -->
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
</bean>
<!--jedis服务器信息 -->
<bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.host}" />
<constructor-arg index="1" value="${redis.port}" type="int" />
<constructor-arg index="2" value="${redis.timeout}"
type="int" />
</bean>
<!--jedis连接池 -->
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<ref bean="jedisShardInfo" />
</list>
</constructor-arg>
</bean>
<!-- Redis连接工厂 -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="shardInfo" ref="jedisShardInfo" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<!-- 缓存序列化方式 -->
<!--对key的默认序列化器。默认值是StringSerializer -->
<bean id="keySerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<!--是对value的默认序列化器,默认值是取自DefaultSerializer的JdkSerializationRedisSerializer。 -->
<bean id="valueSerializer"
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
<!-- redis操作模板,对Jedis进行的通用API操作 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" ref="keySerializer" />
<property name="valueSerializer" ref="valueSerializer" />
<property name="hashKeySerializer" ref="keySerializer" />
<property name="hashValueSerializer" ref="valueSerializer" />
</bean>
自2.0开始,针对单机配置引入RedisStandaloneConfiguration
并且废弃了JedisConnectionFactory
的HostName
,Port
,Password
等属性的配置。
/**
* 连接工厂类
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory(){
// 单机配置
RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration();
standaloneConfig.setHostName("127.0.01");
standaloneConfig.setPort(6379);
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(standaloneConfig);
return connectionFactory;
}
哨兵配置
为了处理高可用性的Redis
,可以使用RedisSentinelConfiguration
支持Redis Sentinel
。
/**
* jedis
*/
@Bean
public RedisConnectionFactory jedisConnectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("mymaster")
.sentinel("127.0.0.1", 26379)
.sentinel("127.0.0.1", 26380);
return new JedisConnectionFactory(sentinelConfig);
}
可以配置的属性:
spring.redis.sentinel.master:master节点的名字
spring.redis.sentinel.nodes:主机:端口的逗号分隔列表
集群配置
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
// 集群配置
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
RedisNode redisNode7000 = new RedisNode("10.1.21.4", 7000);
RedisNode redisNode7001 = new RedisNode("10.1.21.4", 7001);
RedisNode redisNode7002 = new RedisNode("10.1.21.4", 7002);
RedisNode redisNode7003 = new RedisNode("10.1.21.4", 7003);
RedisNode redisNode7004 = new RedisNode("10.1.21.4", 7004);
RedisNode redisNode7005 = new RedisNode("10.1.21.4", 7005);
redisClusterConfiguration.addClusterNode(redisNode7000);
redisClusterConfiguration.addClusterNode(redisNode7001);
redisClusterConfiguration.addClusterNode(redisNode7002);
redisClusterConfiguration.addClusterNode(redisNode7003);
redisClusterConfiguration.addClusterNode(redisNode7004);
redisClusterConfiguration.addClusterNode(redisNode7005);
connectionFactory = new JedisConnectionFactory(redisClusterConfiguration);
// 设置连接池配置
connectionFactory.setPoolConfig(jedisConfig());
// 设置数据库,默认0
connectionFactory.setDatabase(2);
// 设置使用连接池
connectionFactory.setUsePool(true);
return connectionFactory;
}
配置说明
1. 以上配置是基于Jedis客户端的
2. 使用javaCode或者xml的配置方式依据个人喜好