Spring整合Redis分布式-哨兵

3 篇文章 0 订阅
2 篇文章 0 订阅

配置主从关系

1. 下载redis安装包 wget http://download.redis.io/releases/redis-3.2.6.tar.gz

2. 解压文件 tar zxf redis-3.2.6.tar.gz

3. 进入文件夹并编译 cd redis-3.2.6 , make

4. 复制edis.conf文件, 并更改主要配置 cp redis.conf redis_6379.conf

protected-mode no # 允许其他主机访问
port 6379
requirepass foobared # 设置密码, 避免主从切换失败

5. 再复制一份配置文件 cp redis.conf redis_6380.conf

protected-mode no
port 6380
requirepass foobared
masterauth foobared
slaveof 192.168.64.131 6379

6. 启动服务

src/redis-server redis_6379.conf >> redis_6379.log &

src/redis-server redis_6380.conf >> redis_6380.log &

7. 进入客户端, 配置主从关系 src/redis-cli -p 6379 查看主从关系 src/redis-cli -p 6380

127.0.0.1:6380> auth foobared
OK
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:192.168.64.131
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:29
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

8. 查看master状态

[root@localhost redis-3.2.6]# src/redis-cli -p 6379
127.0.0.1:6379> auth foobared
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.64.131,port=6380,state=online,offset=407,lag=1
master_repl_offset:407
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:406

启用哨兵模式

1. 复制sentinel.conf, 并修改配置 cp sentinel.conf sentinel_6379.conf

protected-mode no
port 26379
sentinel monitor mymaster 127.0.0.1 6379 1 # 此处1表示有一个sentinel没能成功连接master就切换
sentinel auth-pass mymaster foobared

2. 复制sentinel.conf, 并修改配置 cp sentinel.conf sentinel_6380.conf

protected-mode no
port 26380
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel auth-pass mymaster foobared

3. 启动服务

src/redis-sentinel sentinel_6379.conf >> logs/sentinel_6379.log &

src/redis-sentinel sentinel_6380.conf >> logs/sentinel_6380.log &

4. 查看哨兵关系 cat sentinel_6379.conf

sentinel known-slave mymaster 192.168.64.131 6379
sentinel known-slave mymaster 127.0.0.1 6379
sentinel known-sentinel mymaster 127.0.0.1 26380 4db74101d663140b8815b136081137c0127193d9
sentinel current-epoch 1

5. 查看redis进程

[root@localhost redis-3.2.6]# ps -ef|grep redis
root      62285  62010  0 18:39 pts/1    00:01:10 src/redis-sentinel *:26380 [sentinel]
root      62417  62010  0 19:21 pts/1    00:01:00 src/redis-sentinel *:26379 [sentinel]
root      62941  62010  0 22:36 pts/1    00:00:01 src/redis-server *:6379         
root      62955  62010  0 22:40 pts/1    00:00:00 src/redis-server *:6380         
root      62978  62010  0 22:47 pts/1    00:00:00 grep redis

 

整合spring

1. 配置pom.xml文件

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.6.4.RELEASE</version>
</dependency>

2. 配置spring.xml文件

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
</bean>
<bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
    <property name="master">
        <bean class="org.springframework.data.redis.connection.RedisNode">
            <property name="name" value="${redis.sentinel.masterName}"/>
        </bean>
    </property>
    <property name="sentinels">
        <set>
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <constructor-arg name="host" value="${redis.sentinel1.host}"/>
                <constructor-arg name="port" value="${redis.sentinel1.port}"/>
            </bean>
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <constructor-arg name="host" value="${redis.sentinel2.host}"/>
                <constructor-arg name="port" value="${redis.sentinel2.port}"/>
            </bean>
        </set>
    </property>
</bean>
<!-- redis服务器中心 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"/>
    <constructor-arg name="poolConfig" ref="poolConfig"/>
    <property name="password" value="foobared"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="jedisConnectionFactory"/>
    <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>

3. 测试

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:spring/spring.xml")
public class AppTests {

    @Autowired
    private RedisTemplate redisTemplate;


    @Test
    public void testRedisSentinel() {
        redisTemplate.opsForZSet().add("test", "A", System.currentTimeMillis());
    }

}

4. 调用成功

 

当主节点异常无法访问时, 哨兵自动切换主从, 原主机修复后自动变成从机

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值