SpringBoot實(shí)現(xiàn)模塊日志入庫的項(xiàng)目實(shí)踐
模塊調(diào)用之后,記錄模塊的相關(guān)日志,看似簡單,其實(shí)暗藏玄機(jī)。
1.簡述
模塊日志的實(shí)現(xiàn)方式大致有三種:
- AOP + 自定義注解實(shí)現(xiàn)
- 輸出指定格式日志 + 日志掃描實(shí)現(xiàn)
- 在接口中通過代碼侵入的方式,在業(yè)務(wù)邏輯處理之后,調(diào)用方法記錄日志。
這里我們主要討論下第3種實(shí)現(xiàn)方式。
假設(shè)我們需要實(shí)現(xiàn)一個(gè)用戶登錄之后記錄登錄日志的操作。
調(diào)用關(guān)系如下:

這里的核心代碼是在 LoginService.login() 方法中設(shè)置了在事務(wù)結(jié)束后執(zhí)行:
// 指定事務(wù)提交后執(zhí)行
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
? ? // 不需要事務(wù)提交前的操作,可以不用重寫這個(gè)方法
? ? @Override
? ? public void beforeCommit(boolean readOnly) {
? ? ? ? System.out.println("事務(wù)提交前執(zhí)行");
? ? }
? ? @Override
? ? public void afterCommit() {
? ? ? ? System.out.println("事務(wù)提交后執(zhí)行");
? ? }
});在這里,我們把這段代碼封裝成了工具類,參考:4.TransactionUtils。
如果在 LoginService.login() 方法中開啟了事務(wù),不指定事務(wù)提交后指定的話,日志處理的方法做異步和做新事務(wù)都會有問題:
- 做異步:由于主事務(wù)可能沒有執(zhí)行完畢,導(dǎo)致可能讀取不到主事務(wù)中新增或修改的數(shù)據(jù)信息;
- 做新事物:可以通過 Propagation.REQUIRES_NEW 事務(wù)傳播行為來創(chuàng)建新事務(wù),在新事務(wù)中執(zhí)行記錄日志的操作,可能會導(dǎo)致如下問題:
- 由于數(shù)據(jù)庫默認(rèn)事務(wù)隔離級別是可重復(fù)讀,意味著事物之間讀取不到未提交的內(nèi)容,所以也會導(dǎo)致讀取不到主事務(wù)中新增或修改的數(shù)據(jù)信息;
- 如果開啟的新事務(wù)和之前的事務(wù)操作了同一個(gè)表,就會導(dǎo)致鎖表。
- 什么都不做,直接同步調(diào)用:問題最多,可能導(dǎo)致如下幾個(gè)問題:
- 不捕獲異常,直接導(dǎo)致接口所有操作回滾;
- 捕獲異常,部分?jǐn)?shù)據(jù)庫,如:PostgreSQL,同一事務(wù)中,只要有一次執(zhí)行失敗,就算捕獲異常,剩余的數(shù)據(jù)庫操作也會全部失敗,拋出異常;
- 日志記錄耗時(shí)增加接口響應(yīng)時(shí)間,影響用戶體驗(yàn)。
2.LoginController
@RestController
public class LoginController {
? ? @Autowired
? ? private LoginService loginService;
? ? @RequestMapping("/login")
? ? public String login(String username, String pwd) {
? ? ? ? loginService.login(username, pwd);
? ? ? ? return "succeed";
? ? }
}3.Action
/**
?* <p> @Title Action
?* <p> @Description 自定義動作函數(shù)式接口
?*
?* @author ACGkaka
?* @date 2023/4/26 13:55
?*/
public interface Action {
? ? ? ? /**
? ? ? ? * 執(zhí)行動作
? ? ? ? */
? ? ? ? void doSomething();
}4.TransactionUtils
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
/**
?* <p> @Title TransactionUtils
?* <p> @Description 事務(wù)同步工具類
?*
?* @author ACGkaka
?* @date 2023/4/26 13:45
?*/
public class TransactionUtils {
? ? /**
? ? ?* 提交事務(wù)前執(zhí)行
? ? ?*/
? ? public static void beforeTransactionCommit(Action action) {
? ? ? ? TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void beforeCommit(boolean readOnly) {
? ? ? ? ? ? ? ? // 異步執(zhí)行
? ? ? ? ? ? ? ? action.doSomething();
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? /**
? ? ?* 提交事務(wù)后異步執(zhí)行
? ? ?*/
? ? public static void afterTransactionCommit(Action action) {
? ? ? ? TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void afterCommit() {
? ? ? ? ? ? ? ? // 異步執(zhí)行
? ? ? ? ? ? ? ? action.doSomething();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}5.LoginService
@Service
public class LoginService {
? ? @Autowired
? ? private LoginLogService loginLogService;
? ? /** 登錄 */
? ? @Transactional(rollbackFor = Exception.class)
? ? public void login(String username, String pwd) {
? ? ? ? // 用戶登錄
? ? ? ? // TODO: 實(shí)現(xiàn)登錄邏輯..
? ? ? ? // 事務(wù)提交后執(zhí)行
? ? ? ? TransactionUtil.afterTransactionCommit(() -> {
? ? ? ? ? ? // 異步執(zhí)行
? ? ? ? ? ? taskExecutor.execute(() -> {
? ? ? ? ?? ??? ?// 記錄日志
? ? ? ? ?? ??? ?loginLogService.recordLog(username);
? ? ? ? ? ? });
? ? ? ? });
? ? }
}6.LoginLogService
6.1 @Async實(shí)現(xiàn)異步
@Service
public class LoginLogService {
/** 記錄日志 */
@Async
@Transactional(rollbackFor = Exception.class)
public void recordLog(String username) {
// TODO: 實(shí)現(xiàn)記錄日志邏輯...
}
}注意:@Async 需要配合 @EnableAsync 使用,@EnableAsync 添加到啟動類、配置類、自定義線程池類上均可。
補(bǔ)充:由于 @Async 注解會動態(tài)創(chuàng)建一個(gè)繼承類來擴(kuò)展方法的實(shí)現(xiàn),所以可能會導(dǎo)致當(dāng)前類注入Bean容器失敗 BeanCurrentlyInCreationException,可以使用如下方式:自定義線程池 + @Autowired
6.2 自定義線程池實(shí)現(xiàn)異步
1)自定義線程池
AsyncTaskExecutorConfig.java
import com.demo.async.ContextCopyingDecorator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
?* <p> @Title AsyncTaskExecutorConfig
?* <p> @Description 異步線程池配置
?*
?* @author ACGkaka
?* @date 2023/4/24 19:48
?*/
@EnableAsync
@Configuration
public class AsyncTaskExecutorConfig {
? ? /**
? ? ?* 核心線程數(shù)(線程池維護(hù)線程的最小數(shù)量)
? ? ?*/
? ? private int corePoolSize = 10;
? ? /**
? ? ?* 最大線程數(shù)(線程池維護(hù)線程的最大數(shù)量)
? ? ?*/
? ? private int maxPoolSize = 200;
? ? /**
? ? ?* 隊(duì)列最大長度
? ? ?*/
? ? private int queueCapacity = 10;
? ? @Bean
? ? public TaskExecutor taskExecutor() {
? ? ? ? ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
? ? ? ? executor.setCorePoolSize(corePoolSize);
? ? ? ? executor.setMaxPoolSize(maxPoolSize);
? ? ? ? executor.setQueueCapacity(queueCapacity);
? ? ? ? executor.setThreadNamePrefix("MyExecutor-");
? ? ? ? // for passing in request scope context 轉(zhuǎn)換請求范圍的上下文
? ? ? ? executor.setTaskDecorator(new ContextCopyingDecorator());
? ? ? ? // rejection-policy:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù)
? ? ? ? // CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是有調(diào)用者所在的線程來執(zhí)行
? ? ? ? executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
? ? ? ? executor.setWaitForTasksToCompleteOnShutdown(true);
? ? ? ? executor.initialize();
? ? ? ? return executor;
? ? }
}2)復(fù)制上下文請求
ContextCopyingDecorator.java
import org.slf4j.MDC;
import org.springframework.core.task.TaskDecorator;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import java.util.Map;
/**
?* <p> @Title ContextCopyingDecorator
?* <p> @Description 上下文拷貝裝飾者模式
?*
?* @author ACGkaka
?* @date 2023/4/24 20:20
?*/
public class ContextCopyingDecorator implements TaskDecorator {
? ? @Override
? ? public Runnable decorate(Runnable runnable) {
? ? ? ? try {
? ? ? ? ? ? // 從父線程中獲取上下文,然后應(yīng)用到子線程中
? ? ? ? ? ? RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
? ? ? ? ? ? Map<String, String> previous = MDC.getCopyOfContextMap();
? ? ? ? ? ? SecurityContext securityContext = SecurityContextHolder.getContext();
? ? ? ? ? ? return () -> {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? if (previous == null) {
? ? ? ? ? ? ? ? ? ? ? ? MDC.clear();
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? MDC.setContextMap(previous);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? RequestContextHolder.setRequestAttributes(requestAttributes);
? ? ? ? ? ? ? ? ? ? SecurityContextHolder.setContext(securityContext);
? ? ? ? ? ? ? ? ? ? runnable.run();
? ? ? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? ? ? // 清除請求數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? MDC.clear();
? ? ? ? ? ? ? ? ? ? RequestContextHolder.resetRequestAttributes();
? ? ? ? ? ? ? ? ? ? SecurityContextHolder.clearContext();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? } catch (IllegalStateException e) {
? ? ? ? ? ? return runnable;
? ? ? ? }
? ? }
}3)自定義線程池實(shí)現(xiàn)異步 LoginService
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@Service
public class LoginService {
? ? @Autowired
? ? private LoginLogService loginLogService;
? ? @Qualifier("taskExecutor")
? ? @Autowired
? ? private TaskExecutor taskExecutor;
? ? /** 登錄 */
? ? @Transactional(rollbackFor = Exception.class)
? ? public void login(String username, String pwd) {
? ? ? ? // 用戶登錄
? ? ? ? // TODO: 實(shí)現(xiàn)登錄邏輯..
? ? ? ? // 事務(wù)提交后執(zhí)行
? ? ? ? TransactionUtil.afterTransactionCommit(() -> {
? ? ? ? ? ? // 異步執(zhí)行
? ? ? ? ? ? taskExecutor.execute(() -> {
? ? ? ? ?? ??? ?// 記錄日志
? ? ? ? ?? ??? ?loginLogService.recordLog(username);
? ? ? ? ? ? });
? ? ? ? });
? ? }
}7.其他解決方案
7.1 使用編程式事務(wù)來代替@Transactional
我們還可以使用TransactionTemplate來代替 @Transactional 注解:
import org.springframework.transaction.support.TransactionTemplate;
@Service
public class LoginService {
? ? @Autowired
? ? private LoginLogService loginLogService;
? ? @Autowired
? ? private TransactionTemplate transactionTemplate;
? ? /** 登錄 */
? ? public void login(String username, String pwd) {
? ? ? ? // 用戶登錄
? ? ? ? transactionTemplate.execute(status->{
?? ??? ??? ?// TODO: 實(shí)現(xiàn)登錄邏輯..
? ? ? ? });
? ? ? ? // 事務(wù)提交后異步執(zhí)行
? ? ? ? taskExecutor.execute(() -> {
? ? ?? ??? ?// 記錄日志
? ? ?? ??? ?loginLogService.recordLog(username);
? ? ? ? });
? ? }
}經(jīng)測試:
這種實(shí)現(xiàn)方式拋出異常后,事務(wù)也可以正常回滾
正常執(zhí)行之后也可以讀取到事務(wù)執(zhí)行后的內(nèi)容,可行。
別看日志記錄好實(shí)現(xiàn),坑是真的多,這里記錄的只是目前遇到的問題。
參考地址:
1.SpringBoot 關(guān)于異步與事務(wù)一起使用的問題
到此這篇關(guān)于SpringBoot實(shí)現(xiàn)模塊日志入庫的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)SpringBoot 模塊日志入庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用MDC快速查詢應(yīng)用接口全部執(zhí)行日志
這篇文章主要為大家介紹了使用MDC快速查詢應(yīng)用接口全部執(zhí)行日志的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
關(guān)于spring中事務(wù)的傳播機(jī)制
這篇文章主要介紹了關(guān)于spring中事務(wù)的傳播機(jī)制,所謂事務(wù)傳播機(jī)制,也就是在事務(wù)在多個(gè)方法的調(diào)用中是如何傳遞的,是重新創(chuàng)建事務(wù)還是使用父方法的事務(wù),需要的朋友可以參考下2023-05-05
Java+OpenCV調(diào)用攝像頭實(shí)現(xiàn)拍照功能
隨著我們對環(huán)境、Mat基本使用越來越熟練、Java Swing也逐步熟悉了起來。本文將通過OpenCV驅(qū)動攝像頭實(shí)現(xiàn)識臉和拍照功能,需要的可以參考一下2022-03-03
SpringBoot使用Shiro實(shí)現(xiàn)動態(tài)加載權(quán)限詳解流程
本文小編將基于?SpringBoot?集成?Shiro?實(shí)現(xiàn)動態(tài)uri權(quán)限,由前端vue在頁面配置uri,Java后端動態(tài)刷新權(quán)限,不用重啟項(xiàng)目,以及在頁面分配給用戶?角色?、?按鈕?、uri?權(quán)限后,后端動態(tài)分配權(quán)限,用戶無需在頁面重新登錄才能獲取最新權(quán)限,一切權(quán)限動態(tài)加載,靈活配置2022-07-07

