欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot實現(xiàn)模塊日志入庫的項目實踐

 更新時間:2023年04月30日 09:32:05   作者:ACGkaka_  
本文主要介紹了SpringBoot實現(xiàn)模塊日志入庫的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

模塊調(diào)用之后,記錄模塊的相關日志,看似簡單,其實暗藏玄機。

1.簡述

模塊日志的實現(xiàn)方式大致有三種:

  • AOP + 自定義注解實現(xiàn)
  • 輸出指定格式日志 + 日志掃描實現(xiàn)
  • 在接口中通過代碼侵入的方式,在業(yè)務邏輯處理之后,調(diào)用方法記錄日志。

這里我們主要討論下第3種實現(xiàn)方式。

假設我們需要實現(xiàn)一個用戶登錄之后記錄登錄日志的操作。

調(diào)用關系如下:

這里的核心代碼是在 LoginService.login() 方法中設置了在事務結束后執(zhí)行:

// 指定事務提交后執(zhí)行
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
? ? // 不需要事務提交前的操作,可以不用重寫這個方法
? ? @Override
? ? public void beforeCommit(boolean readOnly) {
? ? ? ? System.out.println("事務提交前執(zhí)行");
? ? }
? ? @Override
? ? public void afterCommit() {
? ? ? ? System.out.println("事務提交后執(zhí)行");
? ? }
});

在這里,我們把這段代碼封裝成了工具類,參考:4.TransactionUtils。

如果在 LoginService.login() 方法中開啟了事務,不指定事務提交后指定的話,日志處理的方法做異步和做新事務都會有問題:

  • 做異步:由于主事務可能沒有執(zhí)行完畢,導致可能讀取不到主事務中新增或修改的數(shù)據(jù)信息;
  • 做新事物:可以通過 Propagation.REQUIRES_NEW 事務傳播行為來創(chuàng)建新事務,在新事務中執(zhí)行記錄日志的操作,可能會導致如下問題:
    • 由于數(shù)據(jù)庫默認事務隔離級別是可重復讀,意味著事物之間讀取不到未提交的內(nèi)容,所以也會導致讀取不到主事務中新增或修改的數(shù)據(jù)信息;
    • 如果開啟的新事務和之前的事務操作了同一個表,就會導致鎖表。
  • 什么都不做,直接同步調(diào)用:問題最多,可能導致如下幾個問題:
    • 不捕獲異常,直接導致接口所有操作回滾;
    • 捕獲異常,部分數(shù)據(jù)庫,如:PostgreSQL,同一事務中,只要有一次執(zhí)行失敗,就算捕獲異常,剩余的數(shù)據(jù)庫操作也會全部失敗,拋出異常;
    • 日志記錄耗時增加接口響應時間,影響用戶體驗。

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 事務同步工具類
?*
?* @author ACGkaka
?* @date 2023/4/26 13:45
?*/
public class TransactionUtils {
? ? /**
? ? ?* 提交事務前執(zhí)行
? ? ?*/
? ? public static void beforeTransactionCommit(Action action) {
? ? ? ? TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void beforeCommit(boolean readOnly) {
? ? ? ? ? ? ? ? // 異步執(zhí)行
? ? ? ? ? ? ? ? action.doSomething();
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? /**
? ? ?* 提交事務后異步執(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: 實現(xiàn)登錄邏輯..
? ? ? ? // 事務提交后執(zhí)行
? ? ? ? TransactionUtil.afterTransactionCommit(() -> {
? ? ? ? ? ? // 異步執(zhí)行
? ? ? ? ? ? taskExecutor.execute(() -> {
? ? ? ? ?? ??? ?// 記錄日志
? ? ? ? ?? ??? ?loginLogService.recordLog(username);
? ? ? ? ? ? });
? ? ? ? });
? ? }
}

6.LoginLogService

6.1 @Async實現(xiàn)異步

@Service
public class LoginLogService {
    /** 記錄日志 */
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void recordLog(String username) {
        // TODO: 實現(xiàn)記錄日志邏輯...
    }
}

注意:@Async 需要配合 @EnableAsync 使用,@EnableAsync 添加到啟動類、配置類、自定義線程池類上均可。

補充:由于 @Async 注解會動態(tài)創(chuàng)建一個繼承類來擴展方法的實現(xiàn),所以可能會導致當前類注入Bean容器失敗 BeanCurrentlyInCreationException,可以使用如下方式:自定義線程池 + @Autowired

6.2 自定義線程池實現(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ù)(線程池維護線程的最小數(shù)量)
? ? ?*/
? ? private int corePoolSize = 10;
? ? /**
? ? ?* 最大線程數(shù)(線程池維護線程的最大數(shù)量)
? ? ?*/
? ? private int maxPoolSize = 200;
? ? /**
? ? ?* 隊列最大長度
? ? ?*/
? ? 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 轉換請求范圍的上下文
? ? ? ? executor.setTaskDecorator(new ContextCopyingDecorator());
? ? ? ? // rejection-policy:當pool已經(jīng)達到max size的時候,如何處理新任務
? ? ? ? // CALLER_RUNS:不在新線程中執(zhí)行任務,而是有調(diào)用者所在的線程來執(zhí)行
? ? ? ? executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
? ? ? ? executor.setWaitForTasksToCompleteOnShutdown(true);
? ? ? ? executor.initialize();
? ? ? ? return executor;
? ? }
}

2)復制上下文請求

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 {
? ? ? ? ? ? // 從父線程中獲取上下文,然后應用到子線程中
? ? ? ? ? ? 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)自定義線程池實現(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: 實現(xiàn)登錄邏輯..
? ? ? ? // 事務提交后執(zhí)行
? ? ? ? TransactionUtil.afterTransactionCommit(() -> {
? ? ? ? ? ? // 異步執(zhí)行
? ? ? ? ? ? taskExecutor.execute(() -> {
? ? ? ? ?? ??? ?// 記錄日志
? ? ? ? ?? ??? ?loginLogService.recordLog(username);
? ? ? ? ? ? });
? ? ? ? });
? ? }
}

7.其他解決方案

7.1 使用編程式事務來代替@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: 實現(xiàn)登錄邏輯..
? ? ? ? });
? ? ? ? // 事務提交后異步執(zhí)行
? ? ? ? taskExecutor.execute(() -> {
? ? ?? ??? ?// 記錄日志
? ? ?? ??? ?loginLogService.recordLog(username);
? ? ? ? });
? ? }
}

經(jīng)測試:

這種實現(xiàn)方式拋出異常后,事務也可以正?;貪L

正常執(zhí)行之后也可以讀取到事務執(zhí)行后的內(nèi)容,可行。

別看日志記錄好實現(xiàn),坑是真的多,這里記錄的只是目前遇到的問題。

參考地址:

1.SpringBoot 關于異步與事務一起使用的問題

到此這篇關于SpringBoot實現(xiàn)模塊日志入庫的項目實踐的文章就介紹到這了,更多相關SpringBoot 模塊日志入庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 將原生JDBC封裝實現(xiàn)CRUD的案例

    將原生JDBC封裝實現(xiàn)CRUD的案例

    這篇文章主要介紹了將原生JDBC封裝實現(xiàn)CRUD的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Java模擬死鎖發(fā)生之演繹哲學家進餐問題案例詳解

    Java模擬死鎖發(fā)生之演繹哲學家進餐問題案例詳解

    這篇文章主要介紹了Java模擬死鎖發(fā)生之演繹哲學家進餐問題,結合具體演繹哲學家進餐問題的案例形式詳細分析了死鎖機制與原理,需要的朋友可以參考下
    2019-10-10
  • SpringBoot導入導出數(shù)據(jù)實現(xiàn)方法詳解

    SpringBoot導入導出數(shù)據(jù)實現(xiàn)方法詳解

    這篇文章主要介紹了SpringBoot導入導出數(shù)據(jù)實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-12-12
  • SpringCloud Config連接git與數(shù)據(jù)庫流程分析講解

    SpringCloud Config連接git與數(shù)據(jù)庫流程分析講解

    springcloud config是一個解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個部分,server端提供配置文件的存儲、以接口的形式將配置文件的內(nèi)容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應用
    2022-12-12
  • 詳解JAVA中接口的定義和接口的實現(xiàn)

    詳解JAVA中接口的定義和接口的實現(xiàn)

    這篇文章主要介紹了JAVA中接口的定義和接口的實現(xiàn),文中講解非常細致,配合代碼更好的幫大家學習參考,感興趣的朋友可以了解下
    2020-06-06
  • java反射機制根據(jù)屬性名獲取屬性值的操作

    java反射機制根據(jù)屬性名獲取屬性值的操作

    這篇文章主要介紹了java反射機制根據(jù)屬性名獲取屬性值的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • JDBC實現(xiàn)Mysql自動重連機制的方法詳解

    JDBC實現(xiàn)Mysql自動重連機制的方法詳解

    最近在工作中發(fā)現(xiàn)了一個問題,通過查找相關的資料終于解決了,下面這篇文章主要給大家介紹了關于JDBC實現(xiàn)Mysql自動重連機制的相關資料,文中給出多種解決的方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-07-07
  • Java實現(xiàn)經(jīng)典游戲復雜迷宮

    Java實現(xiàn)經(jīng)典游戲復雜迷宮

    這篇文章主要介紹了如何利用java語言實現(xiàn)經(jīng)典《復雜迷宮》游戲,文中采用了swing技術進行了界面化處理,感興趣的小伙伴可以動手試一試
    2022-02-02
  • 解讀Java中char類型相加的問題

    解讀Java中char類型相加的問題

    這篇文章主要介紹了解讀Java中char類型相加的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • java模擬客戶端向服務器上傳文件

    java模擬客戶端向服務器上傳文件

    這篇文章主要為大家詳細介紹了java模擬客戶端向服務器上傳文件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10

最新評論