redis集群+sentinel配置+Jedis连接

  1. 实验目的

    redis集群(主从方式),redis sentinel 哨兵的监控,java程序的jedis的连接。

  2. 实验环境

    • 三台centos6虚拟系统ip地址分别为192.168.0.157,192.168.0.158,192.168.0.159
    • redis3.2.3
    • j2ee的结构为struts2+spring+hibernate
  3. 实验准备

    • 分别在三台虚拟机(192.168.0.157,192.168.0.158,192.168.0.159)下载好redis
      $ wget http://download.redis.io/releases/redis-3.2.3.tar.gz
      
    • 升级yum源
      yum update
      
    • 永久关闭cents防火墙并查看其状态
      service iptables stop
      chkconfig iptables off
      service iptables status
      
    • 实验过程
      • 分别解压三台机子的redis并安装

        $ tar xzf redis-3.2.3.tar.gz
        $ cd redis-3.2.3
        $ make
        
      • 分别配置redis.conf文件并启动redis

        • 主redis(192.168.0.157)

          #不绑定ip地址
          #bind 0.0.0.0
          #设置不是保护模式
          protected-mode no
          #其他设置默认就可以
          
        • 从redis(192.168.0.158,192.168.0.159)

          #不绑定ip地址
          #bind 0.0.0.0
          #设置不是保护模式
          protected-mode no
          #设置监控的主redis
          slaveof 192.168.0.157 6379
          #其他设置默认就可以
          
        • 分别启动三台机子的redis

          ./src/redis-server redis.conf
          
      • 分别配置sentinel.conf文件并启动sentinel

        • 配置三台机子sentinel.conf
          #删除原本配置文件所有配置,黏贴以下配置
          port 26379
          #这是非保护获取,这样其他远程端就可访问sentinel
          protected-mode no
          #设置要监控的master的名字,ip地址,端口,与失去多少sentinel连接就切换其他redis作为master
          sentinel monitor mymaster 192.168.0.157 6379 2
          sentinel down-after-milliseconds mymaster 5000
          sentinel failover-timeout mymaster 60000
          sentinel parallel-syncs mymaster 1
          
        • 启动三台机子的sentinel
          ./src/redis-sentinel sentinel.conf
          
      • 编写jedis

        • redis.properties配置文件
          #redis config
          redis.pool.maxTotal=1000
          redis.pool.maxIdle=50
          redis.pool.maxWaitMillis=50000
          redis.pool.testOnBorrow=true
          sentinel1.ip=192.168.0.157
          sentinel1.port=26379
          sentinel2.ip=192.168.0.158
          sentinel2.port=26379
          sentinel3.ip=192.168.0.159
          sentinel3.port=26379 
          
        • 导入配置文件
          <context:property-placeholder location="classpath:redis.properties"/> 
          
        • 在spring配置文件添加
              <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
                  <property name="maxTotal" value="${redis.pool.maxTotal}"/>
                  <property name="maxIdle" value="${redis.pool.maxIdle}"/>
                  <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
                  <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
              </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="mymaster"></property>
                      </bean>
                  </property>
                  <property name="sentinels">
                      <set>
                          <bean class="org.springframework.data.redis.connection.RedisNode">
                              <constructor-arg name="host" value="${sentinel1.ip}"></constructor-arg>
                              <constructor-arg name="port" value="${sentinel1.port}"></constructor-arg>
                          </bean>
                          <bean class="org.springframework.data.redis.connection.RedisNode">
                              <constructor-arg name="host" value="${sentinel2.ip}"></constructor-arg>
                              <constructor-arg name="port" value="${sentinel2.port}"></constructor-arg>
                          </bean>
                          <bean class="org.springframework.data.redis.connection.RedisNode">
                              <constructor-arg name="host" value="${sentinel3.ip}"></constructor-arg>
                              <constructor-arg name="port" value="${sentinel3.port}"></constructor-arg>
                          </bean>
                      </set>
                  </property>
              </bean>
              <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
                  <property name="poolConfig">
                      <ref bean="jedisPoolConfig"/>
                  </property>
                  <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>
              </bean>
              <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
                  <property name="connectionFactory" ref="jedisConnectionFactory"/>
              </bean>  
          
    • 编写java代码
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.springframework.data.redis.core.RedisCallback;
      import org.springframework.data.redis.core.RedisTemplate;
      import org.springframework.test.context.ContextConfiguration;
      import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
      import javax.annotation.Resource;
      
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(locations = {"classpath:spring.xml"})
      public class RedisTest {
          @Resource
          private RedisTemplate redisTemplate;
      
          @Test
          public void add() {
              boolean result = (boolean) redisTemplate.execute((RedisCallback<Boolean>) connection -> {
                  connection.set("2".getBytes(), "2b".getBytes());
                  return true;
              });
              System.out.println(result);
          }
      } 
      
  4. 实验结论
    实现redis的一主二从的配置方案,三个哨兵监控redis,一旦主redis挂掉,哨兵就会把slave的redis升级为master,java 程序配置redis连接池,结合spring方式使用redis。

转载于:https://my.oschina.net/winchell/blog/812114

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值