使用Spring-data-redis操作Redis的Sentinel
2015-04-10 15:18
701 查看
介绍
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配置
<beanid="redisSentinelConfiguration"class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<propertyname="master">
<beanclass="org.springframework.data.redis.connection.RedisNode">
<propertyname="name"value="mymaster"></property>
</bean>
</property>
<propertyname="sentinels">
<set>
<beanclass="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host"value="192.168.0.100"></constructor-arg>
<constructor-argname="port"value="26380"></constructor-arg>
</bean>
<beanclass="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host"value="192.168.0.100"/>
<constructor-argname="port"value="26381"/>
</bean>
<beanclass="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host"value="192.168.0.100"/>
<constructor-argname="port"value="26382"/>
</bean>
</set>
</property>
</bean>
<beanid="jeidsConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-argref="redisSentinelConfiguration"/>
</bean>
<beanid="redisTemplate"class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jeidsConnectionFactory"/>
程序
写数据
publicvoid 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;
}
});
}
读数据
publicbyte[] get(final
byte[] key) {
return
redisTemplate.execute(new RedisCallback() {
public
byte[]doInRedis(RedisConnection connection)
throws DataAccessException {
returnconnection.get(key);
}
});
}
删数据
publiclong 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。
相关文章推荐
- 使用Spring-data-redis操作Redis的Sentinel
- 使用spring-data-redis操作Redis的Sentinel
- 使用Spring-data-redis操作Redis的Sentinel
- 使用Spring-data-redis操作Redis的Sentinel
- 使用spring-data-redis操作redis
- 使用spring-data-redis进行对redis的操作,封装的一些操作方法
- Spring-data-redis操作Redis的Sentinel
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
- 使用spring-data-redis兼容redis单机和集群操作
- 使用spring-data-redis进行对redis的操作,封装的一些操作方法
- 使用spring-data-redis操作redis
- 使用Spring-data进行Redis操作
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
- spring data redis 集群(sentinel实现)和simple spring memcached分布式初使用
- 使用springdata操作mongodb的例子
- 6、redis之使用spring-data-redis的Template
- spring-data-redis 使用
- Spring Data Redis简单操作
- spring data redis使用示例
- 使用spring-data-redis做缓存