天天看點

Redis進擊(四)Java中配置和使用Redis高可用叢集的哨兵模式(Redis-Sentinel)【Spring&SpringBoot環境】 1. 前置任務2. Spring 項目測試3. SpringBoot 項目測試

楔子:某個時間,由于不清不楚的某些原因,導緻了一次嚴重的線上事故。後來,開發不清不楚的配合把項目更新到了 Redis 高可用叢集的哨兵模式(Redis-Sentinel),再後來,我們逐漸的又不清不楚的淡忘了這件事。節點化的工作很容易導緻一定程度上隻知其然而不知其是以然,這是項目開發中的一個衆相。回想起來,我還是想記點什麼。

該篇可以為 Redis 容災+高可用 應用場景提供解決方案。

1. 前置任務

本文為 上一擊 的代碼驗證部分,是以 Redis進擊(三)搭建Redis高可用叢集的哨兵模式(Redis-Sentinel)【Windows環境】為基礎進行的測試和驗證。是以在進行測試或驗證前,請確定以下前置任務正常:

  1. Redis 主節點(master )的服務端正常開啟;所有分節點(slave)的服務端正常開啟
  2. Redis 主節點的 Sentinel(哨兵)正常開啟;所有分節點的 Sentinel 正常開啟

注:如果主節點/從節點的服務端、Sentinel 沒有全部正常開啟的話,會出現異常:JedisConnectionException: All sentinels down, cannot determine where is 127.0.0.1 master is running...

2. Spring 項目測試

場景一:Spring 項目中配置和使用 Redis 高可用叢集的哨兵模式(Redis-Sentinel) 

2.1. 配置

2.1.1. 添加POM依賴

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

2.1.2. 配置檔案

(1)classpath:redis.properties

redis.host=127.0.0.1
redis.port=6380
redis.timeout=3000
redis.pool.maxIdle=50
redis.pool.testOnBorrow=true
redis.pool.maxWaitMillis=3000
redis.sentinel.master=mymaster
redis.sentinel.node1=127.0.0.1:26379
redis.sentinel.node2=127.0.0.1:26380
redis.sentinel.node3=127.0.0.1:26381
           

注意:redis.sentinel.master 配置項的值必須與主節點/主伺服器 sentinel.conf 配置檔案中的 sentinel monitor mymaster 127.0.0.1 6379 3 的 主節點/主伺服器名稱 mymaster 保持一緻。

否則,會出現異常:JedisException: Can connect to sentinel, but 127.0.0.1:6380 seems to be not monitored...

(2)classpath:spring-redis.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:util="http://www.springframework.org/schema/util"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!--向資源池借用連接配接時是否做連接配接有效性檢測(ping)。檢測到的無效連接配接将會被移除。如果為true,則得到的jedis執行個體均是可用的-->
        <property name="testOnBorrow" value="true"/>
        <!--資源池中的最大連接配接數,預設8個-->
        <property name="maxTotal" value="50" />
        <!--資源池允許的最大空閑連接配接數,預設8個-->
        <property name="maxIdle" value="50" />
        <!--當資源池連接配接用盡後,調用者的最大等待時間(機關為毫秒)。-->
        <property name="maxWaitMillis" value="3000" />
    </bean>

    <util:set id="sentinels">
        <value>${redis.sentinel.node1}</value>
        <value>${redis.sentinel.node2}</value>
        <value>${redis.sentinel.node3}</value>
    </util:set>

    <bean id="jedisPool" class="redis.clients.jedis.JedisSentinelPool" destroy-method="destroy"
          c:poolConfig-ref="jedisPoolConfig" c:sentinels-ref="sentinels"
          c:masterName="${redis.sentinel.master}"/>

    <!--這裡沒有配置密碼,是以不需求使用此項配置-->
    <!--<bean id="jedisPool" class="redis.clients.jedis.JedisSentinelPool" destroy-method="destroy"-->
          <!--c:poolConfig-ref="jedisPoolConfig" c:sentinels-ref="sentinels"-->
          <!--c:masterName="${redis.sentinel.master}" c:password="${redis.password}"/>-->
</beans>
           

2.2 單元測試

2.2.1. JUnit 測試

配置 testApplicationContext.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: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/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.mwei.remote.redis"/>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:redis/redis.properties</value>
            </list>
        </property>
    </bean>

    <import resource="classpath:redis/spring-redis.xml"/>

</beans>
           

測試類:

package com.mwei.remote.redis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:testApplicationContext.xml")
public class JedisPoolTest {

    @Autowired
    private JedisSentinelPool jedisPool;

    @Test
    public void testJedisPool4Get() {
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();
            jedis.set("uid_010_name", "美味物語");
            System.out.println("uid_010_name : " + jedis.get("uid_010_name"));
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
           

輸出結果,驗證OK:

uid_010_name : 美味物語

2.2.2. 工具類測試

與 JUnit 測試方式不一樣的是,下面的測試方法省去了諸多配置項,隻需要進行測試類編寫和運作:

package com.weix.service.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

public class JedisUtil {

    private static JedisSentinelPool jedisPool = null;

    // 自帶的哨兵模式 JedisSentinelPool, 并在一開始初始化連接配接池
    static {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            // 資源池中的最大連接配接數
            // 如果指派為-1,則表示不限制;如果pool已經配置設定了maxActive個jedis執行個體,則此時pool的狀态為exhausted(耗盡)
            config.setMaxTotal(Integer.valueOf(50));
            // 控制一個pool最多有多少個狀态為idle(空閑的)的jedis執行個體
            config.setMaxIdle(Integer.valueOf(50));
            // 表示當borrow(引入)一個jedis執行個體時,最大的等待時間,如果超過等待時間,則直接抛出JedisConnectionException
            config.setMinEvictableIdleTimeMillis(Integer.valueOf(-1));
            // 在borrow一個jedis執行個體時,是否提前進行validate操作;如果為true,則得到的jedis執行個體均是可用的
            config.setTestOnBorrow(Boolean.valueOf(true));

            // Setinel哨兵群
            // Setinel用戶端提供了master自動發現功能
            Set<String> sentinels = new HashSet<>();
            sentinels.add("127.0.0.1:26379");
            sentinels.add("127.0.0.1:26380");
            sentinels.add("127.0.0.1:26381");

            // master名稱必須要和配置檔案sentinel.conf中配置保持一緻
            jedisPool = new JedisSentinelPool("mymaster", sentinels, config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 建構redis連接配接池
     *
     * @return JedisPool
     */
    public static JedisSentinelPool getJedisPool() {
        return jedisPool;
    }

    /**
     * 測試redis線程池是否正常
     *
     * @param args
     */
    public static void main(String[] args) {
        JedisSentinelPool jedisSentinelPool = JedisUtil.getJedisPool();
        Jedis jedis = jedisSentinelPool.getResource();
        System.out.println("jedis = " + jedis);

        jedis.set("uid_020_name", "美味進擊");
        System.out.println("uid_020_name : " + jedis.get("uid_020_name"));

        // Jedis 2.9.0 版本及以上的 JedisPool 的 returnBrokenResource() 和 returnResource() 方法被标注廢棄了,取而代之的是 Jedis 的 close()
        if (jedis != null) {
            jedis.close();
        }
    }
}
           

輸出結果,驗證OK:

jedis = [email protected]

uid_020_name : 美味進擊

3. SpringBoot 項目測試

場景二:Spring 項目中配置和使用 Redis 高可用叢集的哨兵模式(Redis-Sentinel) 

3.1 配置

3.1.1. 添加POM依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.0.4.RELEASE</version>
</dependency>
           

3.1.2. 配置檔案

(1)classpath:application.yml

spring:
  redis:
    jedis:
      pool:
        max-idle: 8
        max-wait: 3000
    sentinel:
      master: mymaster
      nodes:
        - 127.0.0.1:26379
        - 127.0.0.1:26380
        - 127.0.0.1:26381
           

啟動類:

package com.mwei;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 注解@SpringBootApplication是Sprnig Boot項目的核心注解,主要目的是開啟自動配置
 */
@SpringBootApplication
//@PropertySource("file:/etc/dbprivatekey.properties") // Redis配置。因為沒有配置也沒有用到密碼,是以這裡先注釋掉
public class MWeiSpringBootApplication {

    /**
     * 标準的Java應用的main的方法,主要作用是作為項目啟動的入口
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(MWeiSpringBootApplication.class, args);
    }
}
           

測試類:

package com.mwei.utils;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JedisUtilTest extends SpringBootServletInitializer {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void testRedisTemplate4Get() {
        this.redisTemplate.opsForValue().set("uid_030_name", "美味書簽");
        System.out.println("uid_030_name : " + this.redisTemplate.opsForValue().get("uid_030_name"));
    }
}
           

輸出結果,驗證OK:

uid_030_name : 美味書簽

好了,到此,Sping 和 SpringBoot 兩種項目下的 Redis 高可用叢集的哨兵模式(Redis-Sentinel) 已經驗證完成,且結果完美。

Redis 進擊物語:

Redis進擊(一)從0到1,Redis的安裝與使用

Redis進擊(二)搭建Redis主從複制服務叢集(一主兩從、反客為主)【Windows環境】

Redis進擊(三)搭建Redis高可用叢集的哨兵模式(Redis-Sentinel)【Windows環境】

Redis進擊(四)Java中配置和使用Redis高可用叢集的哨兵模式(Redis-Sentinel)【Spring&SpringBoot環境】

Redis進擊(五)redis.conf配置檔案說明備注手冊

Redis異常:Creating Server TCP listening socket *:26379: bind : No such file or directory

Redis異常:JedisException: Can connect to sentinel, but 127.0.0.1:6379 seems to be not monitored...

Redis異常:JedisConnectionException: All sentinels down, cannot determine where is mymaster master is