SkyWalking?自定義插件(Spring?RabbitMQ)具體分析過程
SkyWalking 自定義插件(Spring RabbitMQ) 官方
RabbitMQ插件問題
skywalking官方提供的RabbitMQ插件存在缺陷,其只針對RabbitMQ官方原生Client實(shí)現(xiàn)擴(kuò)展,但我們在項(xiàng)目中一般不直接使用原生Client,而是使用Spring RabitMQ Client,因Spring RabitMQ Consumer中存在跨線程操作,導(dǎo)致跟蹤ID斷鏈。
具體分析過程
1.官方插件源碼的攔截點(diǎn)是原生Consumer的handleDelivery方法,源碼如下:

2.而Spring RabbitMQ消費(fèi)者的默認(rèn)實(shí)現(xiàn)是BlockingQueueConsumer, handleDelivery核心邏輯是把消息放到內(nèi)部的BlockingQueue隊(duì)列,不做真正的消費(fèi)處理,因此攔截此處無法關(guān)聯(lián)到消費(fèi)者邏輯,源碼如下
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) {
...
try {
if (BlockingQueueConsumer.this.abortStarted > 0) {
if (!BlockingQueueConsumer.this.queue.offer(
new Delivery(consumerTag, envelope, properties, body, this.queueName),
BlockingQueueConsumer.this.shutdownTimeout, TimeUnit.MILLISECONDS)) {
Channel channelToClose = super.getChannel();
RabbitUtils.setPhysicalCloseRequired(channelToClose, true);
// Defensive - should never happen
BlockingQueueConsumer.this.queue.clear();
if (!this.canceled) {
RabbitUtils.cancel(channelToClose, consumerTag);
}
try {
channelToClose.close();
catch (@SuppressWarnings("unused") TimeoutException e) {
// no-op
}
}
else {
BlockingQueueConsumer.this.queue
.put(new Delivery(consumerTag, envelope, properties, body, this.queueName));
}
catch (@SuppressWarnings("unused") InterruptedException e) {
Thread.currentThread().interrupt();
catch (Exception e) {
BlockingQueueConsumer.logger.warn("Unexpected exception during delivery", e);
}3.真正的消費(fèi)處理在SimpleMessageListenerContainer,SimpleMessageListenerContainer繼承Runnable接口,在其run方法中while循環(huán)調(diào)用mainLoop方法,整體調(diào)用鏈路為
4.SimpleMessageListenerContainer.run() -> SimpleMessageListenerContainer.mainLoop() -> SimpleMessageListenerContainer.receiveAndExecute() -> SimpleMessageListenerContainer.doReceiveAndExecute() -> AbstractMessageListenerContainer.executeListener()。最終在executeListener中執(zhí)行消費(fèi)邏輯
protected void executeListener(Channel channel, Object data) {
...
try {
// 執(zhí)行消費(fèi)邏輯
doExecuteListener(channel, data);
if (sample != null) {
this.micrometerHolder.success(sample, data instanceof Message
? ((Message) data).getMessageProperties().getConsumerQueue()
: queuesAsListString());
}
}
catch (RuntimeException ex) {
....
}
}實(shí)現(xiàn)自定義插件
從上面可以分析出,AbstractMessageListenerContainer.executeListener()是最佳的攔截點(diǎn)
實(shí)現(xiàn)源碼已放到碼云倉庫:https://gitee.com/eureka-gitee/apm-sniffer-pro/tree/v7.0.0.0/
效果展示
SkyWalking調(diào)用鏈路

logback日志

到此這篇關(guān)于SkyWalking 自定義插件(Spring RabbitMQ)的文章就介紹到這了,更多相關(guān)SkyWalking 自定義插件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解SpringBoot中5種類型參數(shù)傳遞和json數(shù)據(jù)傳參的操作
當(dāng)涉及到參數(shù)傳遞時(shí),Spring?Boot遵循HTTP協(xié)議,并支持多種參數(shù)傳遞方式,這些參數(shù)傳遞方式可以根據(jù)請求的不同部分進(jìn)行分類,2023-12-12
MyBatisPlus?大數(shù)據(jù)量查詢慢的問題解決
本文主要介紹了MyBatis?Plus?解決大數(shù)據(jù)量查詢慢問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
SpringMVC中@RequestMapping注解的實(shí)現(xiàn)
RequestMapping是一個(gè)用來處理請求地址映射的注解,本文主要介紹了SpringMVC中@RequestMapping注解的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
解決因jdk版本引起的TypeNotPresentExceptionProxy異常
這篇文章介紹了解決因jdk版本引起的TypeNotPresentExceptionProxy異常的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12

