JavaWeb專案架構之Redis分散式日誌佇列

Annie6510發表於2018-01-26

架構、分散式、日誌佇列,標題自己都看著唬人,其實就是一個日誌收集的功能,只不過中間加了一個Redis做訊息佇列罷了。 為什麼需要訊息佇列? 當系統中出現“生產“和“消費“的速度或穩定性等因素不一致的時候,就需要訊息佇列,作為抽象層,彌合雙方的差異。

架構、分散式、日誌佇列,標題自己都看著唬人,其實就是一個日誌收集的功能,只不過中間加了一個Redis做訊息佇列罷了。

_

為什麼需要訊息佇列?

當系統中出現“生產“和“消費“的速度或穩定性等因素不一致的時候,就需要訊息佇列,作為抽象層,彌合雙方的差異。

比如我們系統中常見的郵件、簡訊傳送,把這些不需要及時響應的功能寫入佇列,非同步處理請求,減少響應時間。

如何實現?

成熟的JMS訊息佇列中介軟體產品市面上有很多,但是基於目前專案的架構以及部署情況,我們採用Redis做訊息佇列。

為什麼用Redis?

Redis中list資料結構,具有“雙端佇列”的特性,同時redis具有持久資料的能力,因此redis實現分散式佇列是非常安全可靠的。

它類似於JMS中的“Queue”,只不過功能和可靠性(事務性)並沒有JMS嚴格。Redis本身的高效能和"便捷的"分散式設計(replicas,sharding),可以為實現"分散式佇列"提供了良好的基礎。

提供者端

專案採用第三方redis外掛spring-data-redis,不清楚如何使用的請自行谷歌或者百度。

redis.properties:

#redis 配置中心 
redis.host=192.168.1.180
redis.port=6379
redis.password=123456
redis.maxIdle=100 
redis.maxActive=300 
redis.maxWait=1000 
redis.testOnBorrow=true 
redis.timeout=100000複製程式碼

redis配置:

    <!-- redis 配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" />
    <bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}" />
        <property name="port" value="${redis.port}" />
        <property name="password" value="${redis.password}" />
        <property name="timeout" value="${redis.timeout}" />
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="usePool" value="true" />
    </bean>
    <bean id="redisTemplate"  class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>複製程式碼

切面日誌配置(虛擬碼):

/**
 * 系統日誌,切面處理類
 * 建立者 小柒2012
 * 建立時間    2018年1月15日
 */
@Component
@Scope
@Aspect
public class SysLogAspect {
    
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    //註解是基於swagger的API,也可以自行定義
    @Pointcut("@annotation(io.swagger.annotations.ApiOperation)")
    public void logPointCut() { 

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Object result = point.proceed();
        //把日誌訊息寫入itstyle_log頻道
        redisTemplate.convertAndSend("itstyle_log","日誌資料,自行處理");
        return result;
    }
}複製程式碼

消費者端

Redis配置:

    <!-- redis 配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" />

    <bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}" />
        <property name="port" value="${redis.port}" />
        <property name="password" value="${redis.password}" />
        <property name="timeout" value="${redis.timeout}" />
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="usePool" value="true" />
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"  
                    p:connection-factory-ref="jedisConnectionFactory">  
        <property name="keySerializer">  
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
        </property>  
        <property name="hashKeySerializer">  
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
        </property>  
    </bean>
    
    <!-- 監聽實現類 -->
    <bean id="listener" class="com.itstyle.market.common.listener.MessageDelegateListenerImpl"/>
    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    <redis:listener-container connection-factory="jedisConnectionFactory">
        <!-- topic代表監聽的頻道,是一個正規匹配  其實就是你要訂閱的頻道-->
        <redis:listener ref="listener" serializer="stringRedisSerializer" method="handleLog" topic="itstyle_log"/>
    </redis:listener-container> 複製程式碼

監聽介面:

public interface MessageDelegateListener {
    public void handleLog(Serializable message);
}複製程式碼

監聽實現:

public class MessageDelegateListenerImpl implements MessageDelegateListener {
        @Override
        public void handleLog(Serializable message) {
            if(message == null){
                System.out.println("null");
            }else {
                //處理日誌資料
            }
        }
}複製程式碼

Q&A

開源專案原始碼(參考):https://gitee.com/52itstyle/spring-boot-mail

作者: 小柒

出處: https://blog.52itstyle.com

分享是快樂的,也見證了個人成長曆程,文章大多都是工作經驗總結以及平時學習積累,基於自身認知不足之處在所難免,也請大家指正,共同進步。


原文連結


相關文章