spring 中 Spring-data-redis的配置与使用
2017-04-10 13:39
363 查看
介绍
spring-Data-Redis项目(简称SDR)是对Redis的Key-Value数据存储操作提供了更高层次的抽象,提供了一个对几种主要的redis的Java客户端(例如:jedis,jredis,jdbc-redis等)的抽象,使开发中可以几乎完全屏蔽具体使用客户端的影响,使业务代码保持较强的稳定性。Spring-Data-Redis提供了一个基础的泛型RedisTemplate供开发者快速的利用代码完成基础的crud工作。而StringRedisTemplate则提供了最常用的String类型的实现。在实践中可以考虑完全省去dao层的设计,直接在service层注入相应的template实例。
Redis Sentinel是Redis官方提供的集群管理工具,使用一个或多个sentinel和Redis的master/slave可以组成一个群集,可以检测master实例是否存活,并在master实例发生故障时,将slave提升为master,并在老的master重新加入到sentinel的群集之后,会被重新配置,作为新master的slave。这意味着基于Redis
sentinel的HA群集是能够自我管理的。
环境
本文基于redis-2.8.19和jedis2.4.2版本。在一台机器上启动3个redis,一个做master,两个做slave。
Master 端口:6380
Slave1 端口:6381
Slave2端口:6382
Sentinel配置
Master
redis.confport 6380
sentinel.conf
port 26380
sentinel monitor mymaster 192.168.0.100 6380 2
Slave1
redis.confport 6381
slaveof 192.168.0.100 6380
sentinel.conf
port 26381
sentinel monitor mymaster 192.168.0.100 6380 2
Slave2
redis.confport 6382
slaveof 192.168.0.100 6380
sentinel.conf
port 26382
sentinel monitor mymaster 192.168.0.100 6380 2
Spring配置
<bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"><property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="mymaster"> </property>
</bean>
</property>
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host" value="192.168.0.100"/>
<constructor-argname="port" value="26380"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host" value="192.168.0.100"/>
<constructor-argname="port" value="26381"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host" value="192.168.0.100"/>
<constructor-argname="port" value="26382"/>
</bean>
</set>
</property>
</bean>
<bean id="jeidsConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-argref="redisSentinelConfiguration"/>
</bean>
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jeidsConnectionFactory"/>
</bean>
程序
写数据
public void set(final byte[] key, finalbyte[] value,final longliveTime){
redisTemplate.execute(new RedisCallback() {
public LongdoInRedis(RedisConnectionconnection)
throws DataAccessException {
connection.set(key,value);
if (liveTime > 0) {
connection.expire(key,liveTime);
}
return 1L;
}
});
}
读数据
public byte[] get(final byte[] key){
return redisTemplate.execute(new RedisCallback() {
public byte[]doInRedis(RedisConnection connection)
throws DataAccessException {
returnconnection.get(key);
}
});
}
删数据
public long del(final String...keys){
return redisTemplate.execute(new RedisCallback() {
public LongdoInRedis(RedisConnectionconnection)
throws DataAccessException {
longresult = 0;
for (inti = 0; i < keys.length; i++)
{
result = connection.del(keys[i].getBytes());
}
returnresult;
}
});
}
注意
1) 在配置Redis的sentinel.conf文件时注意使用外部可以访问的ip地址,因为当redis-sentinel服务和redis-server在同一台机器的时候,主服务发生变化时配置文件中将主服务ip变为127.0.0.1,这样外部就无法访问了。2) 发生master迁移后,如果遇到运维需要,想重启所有redis,必须最先重启“新的”master节点,否则sentinel会一直找不到master。
另外也可以用下面的例子来配置redis
<context:component-scan base-package="com.letv.ims.dao"/> <bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <constructor-arg name="master" value="mymaster"/> <constructor-arg name="sentinelHostAndPorts"> <set> <value>10.114.180.109:26379</value> <value>10.114.180.107:26379</value> </set> </constructor-arg> </bean> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="100"/> <property name="maxTotal" value="1000"/> <property name="maxWaitMillis" value="20000"/> <property name="testOnBorrow" value="true"/> <property name="testOnReturn" value="true"/> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg ref="sentinelConfig"/> <property name="timeout" value="5000"/> <property name="usePool" value="true"/> <property name="password" value="test@test"/> <property name="database" value="7"/> <property name="poolConfig" ref="poolConfig"/> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> </bean>
相关文章推荐
- 使用spring-data-redis配置集群错误
- 使用 Spring data redis 结合 Spring cache 缓存数据配置
- Spring-data-redis配置及使用示例
- spring-data-redis使用哨兵配置一主多从
- spring-data-redis使用哨兵配置一主多从
- 关于spring data redis使用javaConfig的基本配置
- 使用Spring-data-redis操作Redis的Sentinel
- 使用Spring-Data-Redis存储对象(redisTemplate)
- 使用Spring Data +Redis实现缓存
- 使用spring-data-redis做缓存
- spring data redis使用示例
- 使用spring-data-redis操作Redis的Sentinel
- 使用spring-data-redis操作redis
- spring-data-redis使用自定义序列化数据 使用 protobuf
- 【Redis基础】SSH 中Spring-data-redis使用体验
- spring 使用redis集群配置
- 6、redis之使用spring-data-redis的Template
- 配置使用Spring Data MongoDB
- jedis-2.4.2 和 spring-data-redis-1.2.0.RELEASE 配置
- Spring+redis,spring-data-redis使用