使用Spring-data-redis操作Redis的Sentinel
2015-12-21 17:09
721 查看
/article/8346512.html
目录(?)[+]
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群集是能够自我管理的。
在一台机器上启动3个redis,一个做master,两个做slave。
Master 端口:6380
Slave1 端口:6381
Slave2端口:6382
port 6380
sentinel.conf
port 26380
sentinel monitor mymaster 192.168.0.100 6380 2
port 6381
slaveof 192.168.0.100 6380
sentinel.conf
port 26381
sentinel monitor mymaster 192.168.0.100 6380 2
port 6382
slaveof 192.168.0.100 6380
sentinel.conf
port 26382
sentinel monitor mymaster 192.168.0.100 6380 2
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"/>
void set(final
byte[] key, finalbyte[]value,finallongliveTime)
{
redisTemplate.execute(new RedisCallback() {
public LongdoInRedis(RedisConnectionconnection)
throws DataAccessException {
connection.set(key,value);
if (liveTime > 0) {
connection.expire(key,liveTime);
}
return 1L;
}
});
}
byte[] get(finalbyte[]
key) {
returnredisTemplate.execute(new RedisCallback() {
public
byte[]doInRedis(RedisConnection connection)
throws DataAccessException {
returnconnection.get(key);
}
});
}
long del(final String...keys) {
returnredisTemplate.execute(new RedisCallback() {
public LongdoInRedis(RedisConnectionconnection)
throws DataAccessException {
longresult = 0;
for (inti = 0;i <keys.length;i++)
{
result =connection.del(keys[i].getBytes());
}
returnresult;
}
});
}
2) 发生master迁移后,如果遇到运维需要,想重启所有redis,必须最先重启“新的”master节点,否则sentinel会一直找不到master。
/article/8346512.html
目录(?)[+]
介绍
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,finallongliveTime)
{
redisTemplate.execute(new RedisCallback() {
public LongdoInRedis(RedisConnectionconnection)
throws DataAccessException {
connection.set(key,value);
if (liveTime > 0) {
connection.expire(key,liveTime);
}
return 1L;
}
});
}
读数据
publicbyte[] get(finalbyte[]
key) {
returnredisTemplate.execute(new RedisCallback() {
public
byte[]doInRedis(RedisConnection connection)
throws DataAccessException {
returnconnection.get(key);
}
});
}
删数据
publiclong del(final String...keys) {
returnredisTemplate.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。
相关文章推荐
- ASP.NET Redis 开发 [转]
- Jedis操作redis入门学习
- Ubuntu 15.10 下Redis Cluster使用
- redis持久化
- Redis set操作
- Redis学习手册(List数据类型)
- redis string 命令
- 超强、超详细Redis数据库入门教程
- redis keys 命令
- CentOS 上面安装redis3.0.5
- redis 五种数据类型的使用场景
- 利用Canal完成Mysql数据同步Redis
- redis之列表类型(list)——队列和栈简单实现
- Redis学习笔记(一) Redis安装与基本使用(windows)
- 红帽redis安装和介绍
- Redis入门之 redis set集合的操作
- redis之哈希类型(hash)
- Redis 入门之 redis 列表操作
- redis 简单应用
- Windows下Redis的安装使用