架構、分散式、日誌佇列,標題自己都看著唬人,其實就是一個日誌收集的功能,只不過中間加了一個Redis做訊息佇列罷了。
為什麼需要訊息佇列?
當系統中出現“生產“和“消費“的速度或穩定性等因素不一致的時候,就需要訊息佇列,作為抽象層,彌合雙方的差異。
比如我們系統中常見的郵件、簡訊傳送,把這些不需要及時響應的功能寫入佇列,非同步處理請求,減少響應時間。
如何實現?
成熟的JMS訊息佇列中介軟體產品市面上有很多,但是基於目前專案的架構以及部署情況,我們採用Redis做訊息佇列。
為什麼用Redis?
Redis中list資料結構,具有“雙端佇列”的特性,同時redis具有持久資料的能力,因此redis實現分散式佇列是非常安全可靠的。
提供者端
專案採用第三方redis外掛spring-data-redis,不清楚如何使用的請自行谷歌或者百度。
#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 配置 -->
<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 配置 -->
<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
- 【問題一】為什麼使用Redis?
上面其實已經有做說明,儘管市面上有許多很穩定的產品,比如可能大家會想到的Kafka、RabbitMQ以及RocketMQ。但是由於專案本身使用了Redis做分散式快取,基於省事可行的原則就選定了Redis。 - 【問題二】日誌資料如何儲存?
原則上是不建議儲存到關聯式資料庫的,比如MySql,畢竟產生的日誌數量是巨大的,建議儲存到Elasticsearch等非關係型資料庫。 - 【問題三】切面日誌收集是如何實現的?
切面日誌需要引入spring-aspects相關Jar包,並且配置使Spring採用CGLIB代理 。
開源專案原始碼(參考):https://gitee.com/52itstyle/spring-boot-mail
出處: https://blog.52itstyle.com
分享是快樂的,也見證了個人成長曆程,文章大多都是工作經驗總結以及平時學習積累,基於自身認知不足之處在所難免,也請大家指正,共同進步。