第一步:配置隊列
第二步:確認隊列是否創建,登陸到rabbitMq的web管理界面
第三步:對show-cloud-users-service添加相應對依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
第四步:添加mq處理類,也就是隊列監聽
package org.go.show.users.rabitMqHandler;
import lombok.extern.slf4j.Slf4j;
import org.go.show.common.constants.LogQueue;
import org.go.show.users.i.AccountService;
import org.go.show.users.pojo.Account;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 從mq隊列消費日誌數據
*
*
*/
@Component
@Slf4j
public class RabitMqHandler {
@Value("${goshow.log.logServiceBean:logServiceDBImpl}")
String logServiceBean;
@Autowired
AccountService accountService;
/**
* 監聽帳套刪除隊列
*
* @param accountId
*/
@RabbitHandler
@RabbitListener(queues = LogQueue.DEL_ACCOUNT_QUEUE) // 監聽隊列
public void delAccountHandler(String accountId) {
try {
accountService.delAccountRelInfo(accountId);
} catch (Exception e) {
log.error("刪除帳套操作,日誌:{},異常:{}", accountId, e);
}
}
}
第五步:實現關聯信息的刪除操作。