通过使用spring-session-data-redis处理session共享sentinel模式

介绍
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.conf

      port 6380

sentinel.conf

      port 26380

      sentinel monitor mymaster 192.168.0.100 6380 2

Slave1
redis.conf

      port 6381

      slaveof 192.168.0.100 6380

sentinel.conf

      port 26381

      sentinel monitor mymaster 192.168.0.100 6380 2



Slave2
redis.conf

      port 6382

      slaveof 192.168.0.100 6380

sentinel.conf

      port 26382

            sentinel monitor mymaster 192.168.0.100 6380 2Spring配置
        
 <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-arg name="host" value="192.168.0.100"></constructor-arg> 

                <constructor-arg name="port" value="26380"></constructor-arg>                   

                </bean>

                <bean class="org.springframework.data.redis.connection.RedisNode">

                    <constructor-arg name="host" value="192.168.0.100"/>

                    <constructor-arg name="port" value="26381"/>               

                </bean>

                <bean class="org.springframework.data.redis.connection.RedisNode">                   

                    <constructor-arg name="host" value="192.168.0.100"/>

                    <constructor-arg name="port" value="26382"/>               

                </bean>

            </set>

        </property>

   </bean>



   <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
      <constructor-arg ref="redisSentinelConfiguration"/>

   </bean>



   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"

      <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。

猜你喜欢

转载自blog.csdn.net/liaonanfeng88/article/details/84840339
今日推荐