Redis集群 之 Sentinel模式Windows篇

1.环境

  a.下载Redis:访问 https://github.com/MicrosoftArchive/redis/releases 下载Windows版Redis,并解压

 

 

2.哨兵模式配置

  a.复制3份Redis作为3个节点,分别命名为8081、8082、8083

 

  b.修改 Redis 各节点中的 redis.windows.conf 配置文件

    1)8081节点:

#端口号改为8081
port 8081

    2)8082节点:

#端口号改为8082
port 8082
#设置该slave的master服务器为8081
slaveof 127.0.0.1 8081

    3)8083节点:

#端口号改为8083
port 8083
#设置该slave的master服务器为8081
slaveof 127.0.0.1 8081

 

  c.在Redis 各节点中创建 sentinel.conf 哨兵配置文件

    1)8081节点:

#绑定0.0.0.0解读无法访问的问题
bind 0.0.0.0
#当前Sentinel服务运行的端口
port 8091
#去监视一个名为mymaster的主redis实例,这个主实例的IP地址为本机地址127.0.0.1,端口号为6379,而将这个主实例判断为失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行
sentinel monitor mymaster 127.0.0.1 8081 2
#指定了Sentinel认为Redis实例已经失效所需的毫秒数。当 实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。只有一个 Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线,这时自动故障迁移才会执行
sentinel down-after-milliseconds mymaster 5000
#指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长
sentinel parallel-syncs mymaster 1
#如果在该时间(ms)内未能完成failover操作,则认为该failover失败
sentinel failover-timeout mymaster 15000

    2)8082节点:

#绑定0.0.0.0解读无法访问的问题
bind 0.0.0.0
#当前Sentinel服务运行的端口
port 8092
#去监视一个名为mymaster的主redis实例,这个主实例的IP地址为本机地址127.0.0.1,端口号为6379,而将这个主实例判断为失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行
sentinel monitor mymaster 127.0.0.1 8081 2
#指定了Sentinel认为Redis实例已经失效所需的毫秒数。当 实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。只有一个 Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线,这时自动故障迁移才会执行
sentinel down-after-milliseconds mymaster 5000
#指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长
sentinel parallel-syncs mymaster 1
#如果在该时间(ms)内未能完成failover操作,则认为该failover失败
sentinel failover-timeout mymaster 15000

    3)8083节点:

#绑定0.0.0.0解读无法访问的问题
bind 0.0.0.0
#当前Sentinel服务运行的端口
port 8093
#去监视一个名为mymaster的主redis实例,这个主实例的IP地址为本机地址127.0.0.1,端口号为6379,而将这个主实例判断为失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行
sentinel monitor mymaster 127.0.0.1 8081 2
#指定了Sentinel认为Redis实例已经失效所需的毫秒数。当 实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。只有一个 Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线,这时自动故障迁移才会执行
sentinel down-after-milliseconds mymaster 5000
#指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长
sentinel parallel-syncs mymaster 1
#如果在该时间(ms)内未能完成failover操作,则认为该failover失败
sentinel failover-timeout mymaster 15000

 

  d.创建启动脚本

    1)创建 8081 节点 Redis 启动脚本 start8081.bat:

cd 8081
title redis-8081
redis-server.exe redis.windows.conf

    2)创建 8082 节点 Redis 启动脚本 start8082.bat:

cd 8082
title redis-8082
redis-server.exe redis.windows.conf

    3)创建 8083 节点 Redis 启动脚本 start8083.bat:

cd 8083
title redis-8083
redis-server.exe redis.windows.conf

    4)创建 8081 节点 哨兵Sentinel 启动脚本 startsentinel8081.bat:

cd 8081
title sentinel-8081
redis-server.exe sentinel.conf --sentinel

    5)创建 8082 节点 哨兵Sentinel 启动脚本 startsentinel8082.bat:

cd 8082
title sentinel-8082
redis-server.exe sentinel.conf --sentinel

    6)创建 8083 节点 哨兵Sentinel 启动脚本 startsentinel8083.bat:

cd 8083
title sentinel-8083
redis-server.exe sentinel.conf --sentinel

 

 

3.测试

  a.启动Reids和哨兵Sentinel:一次双击 start8081.bat、start8082.bat、start8083.bat、startsentinel8081.bat、startsentinel8082.bat、startsentinel8083.bat 启动服务

  b.关闭 8081 节点的Redis服务(由于没有开启后台运行,直接关闭窗口即可)

  c.查看 8081 节点哨兵Sentinel窗口的日志:8081节点 Redis服务关闭后,master由8081切换成了8083

 

 

4.Jedis操作Sentinel模式的Redis

  1)导入maven依赖

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.10.0</version>
    </dependency>

  2)使用

public class TestSentinel {

    private static JedisSentinelPool pool;

    static {
        init();
    }

    /**
     * 初始化redis连接池
     */
    private static void init(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(5);   //最大空闲数
        config.setMaxWaitMillis(60000); //连接时最大的等待时间(毫秒)
        config.setMaxTotal(500);    //最大连接数
        config.setTestOnBorrow(true);   //在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的

        //与sentinel.conf中的名称相对应
        String masterName = "mymaster";
        //哨兵ip及端口
        Set<String> sentinels = new HashSet<>();
        sentinels.add("127.0.0.1:8091");
        sentinels.add("127.0.0.1:8092");
        sentinels.add("127.0.0.1:8093");
        pool = new JedisSentinelPool(masterName, sentinels, config);
    }

    public static Jedis getJedis() {
        return pool.getResource();
    }

    public static void returnJedis(Jedis jedis){
        if(jedis != null){
            jedis.close();
        }
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        Jedis jedis = TestSentinel.getJedis();
        jedis.set("name", "zwin");
        jedis.set("age", "29");
        returnJedis(jedis);

        jedis = TestSentinel.getJedis();
        String name = jedis.get("name");
        String age = jedis.get("age");
        System.out.println("name[" + name + "], age[" + age + "]");
        returnJedis(jedis);
    }

}

 

 

5.Spring操作Sentinel模式的Redis集群

  a.导入maven依赖

  <properties>

    .....

    <!-- spring -->
    <spring.version>5.1.1.RELEASE</spring.version>
    <!-- jackson-json -->
    <jackson.version>2.9.4</jackson.version>
    <!-- log4j -->
    <slf4j.version>1.7.18</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
  </properties>

  <dependencies>
    <!-- spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- Jackson -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>${jackson.version}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <!-- AOP -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.6</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.6</version>
    </dependency>

    <!-- 日志相关 -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <!-- redis -->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.1.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.10.0</version>
    </dependency>
  </dependencies>

 

  b.创建spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 自动扫描的包名 -->
    <context:component-scan base-package="com.wode" />

    <!-- 开启AOP代理 -->
    <aop:aspectj-autoproxy proxy-target-class="true" />

    <!--开启注解处理器 -->
    <context:annotation-config>
    </context:annotation-config>

    <context:property-placeholder location="classpath:redis.properties"/>
    <!-- Spring中引入其他配置文件 -->
    <import resource="classpath*:/spring-redis.xml" />

</beans>

 

  c.创建spring-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <!-- jedis配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大空闲数:空闲连接数大于maxIdle时,将进行回收 -->
        <property name="maxIdle" value="${redis.maxIdle}" />
        <!-- 最大连接数:能够同时建立的最大连接个数 -->
        <property name="maxTotal" value="${redis.maxTotal}" />
        <!-- 最大等待时间:单位ms -->
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <!-- 使用连接时,检测连接是否成功 -->
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

    <!--redis哨兵 -->
    <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"/>
            </bean>
        </property>
        <property name="sentinels">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="127.0.0.1"/>
                    <constructor-arg name="port" value="8091"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="127.0.0.1"/>
                    <constructor-arg name="port" value="8092"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="127.0.0.1"/>
                    <constructor-arg name="port" value="8093"/>
                </bean>
            </set>
        </property>
    </bean>

    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:password="${redis.password}">
        <constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"></constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory" />
        <!-- 配置Serializer -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
    </bean>
</beans>

 

  d.创建redis.properties

redis.password=
redis.maxIdle=5
redis.maxWait=60000
redis.maxTotal=500
redis.testOnBorrow=true

 

  e.创建实体类

public class TestBean {

    private String name;
    private int age;

    public TestBean(){}

    public TestBean(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "[TestBean]: name[" + name + "], age[" + age + "]";
    }
}

 

  f.使用

@Component
public class TestSpringSentinel {

    @Resource
    private RedisTemplate redisTemplate;

    public void set(String key, Object value){
//        redisTemplate.boundValueOps(key).set(value);
        redisTemplate.opsForValue().set(key, value);
    }

    public Object get(String key){
//        return redisTemplate.boundValueOps(key).get();
        return redisTemplate.opsForValue().get(key);
    }


    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        TestSpringSentinel testSpringSentinel = (TestSpringSentinel) context.getBean("testSpringSentinel");

        testSpringSentinel.set("name", "zwj");
        testSpringSentinel.set("age", "28");
        testSpringSentinel.set("bean", new TestBean("xf", 26));

        String name = (String) testSpringSentinel.get("name");
        String age = (String) testSpringSentinel.get("age");
        System.out.println("name[" + name + "], age[" + age + "]");

        TestBean bean = (TestBean) testSpringSentinel.get("bean");
        System.out.println(bean);
    }
}

 

 

6.参考文档:

  https://www.cnblogs.com/justdoyou/p/10253668.html

  https://www.cnblogs.com/yu421/p/8081544.html

 

posted @ 2019-11-13 11:50  晨M风  阅读(698)  评论(0编辑  收藏  举报