Redis集羣 之 Sentinel模式Windows篇

1.環境html

  a.下載Redis:訪問 https://github.com/MicrosoftArchive/redis/releases 下載Windows版Redis,並解壓git

 

 

2.哨兵模式配置github

  a.複製3份Redis做爲3個節點,分別命名爲808一、808二、8083web

 

  b.修改 Redis 各節點中的 redis.windows.conf 配置文件redis

    1)8081節點:spring

#端口號改成8081
port 8081

    2)8082節點:json

#端口號改成8082
port 8082
#設置該slave的master服務器爲8081
slaveof 127.0.0.1 8081

    3)8083節點:windows

#端口號改成8083
port 8083
#設置該slave的master服務器爲8081
slaveof 127.0.0.1 8081

 

  c.在Redis 各節點中建立 sentinel.conf 哨兵配置文件api

    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

相關文章
相關標籤/搜索