zoukankan      html  css  js  c++  java
  • 使用Spring-data-redis操作Redis的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 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"/>

    程序

    写数据

             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。

  • 相关阅读:
    test deploy
    SpringBoot+Spring Session+Redis实现Session共享及踩坑记录
    登录接口取cookie
    pycharm设置自动调节字体大小
    ClickHouse高可用集群
    clickhouse 常用命令(三)
    clickHouse常用命令(一)
    ClickHouse表引擎
    clickhouse基本数据类型
    手写滚动条设计----直接粘贴
  • 原文地址:https://www.cnblogs.com/zhaofeng555/p/5729993.html
Copyright © 2011-2022 走看看