
前言
最近呢xxx接到了一個任務,是需要把AOP列印出的請求日志,給儲存到資料庫。xxx一看這個簡單啊,不就是儲存到資料庫嘛。一頓操作猛如虎,過了20分鐘就把這個任務完成了。xxx作為一個優秀的程式員,發現這樣同步儲存會增加了接口的響應時間。這肯定難不倒xxx,當即決定使用多線程來處理這個問題。終于在臨近飯點完成了。準備邊吃邊欣賞自己的傑作時,外賣小哥臨時走來了一句,搞這樣麻煩幹啥,你加個
@Async
不就可以了。
實作一個精簡版的請求日志輸出。
LogAspect
@Slf4j
@Aspect
@Component
public class LogAspect {
@Pointcut("execution(* com.hxh.log.controller.*.*(..)))")
public void saveLog(){}
@Before("saveLog()")
public void saveLog(JoinPoint joinPoint) {
// 擷取HttpServletRequest
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//擷取請求參數
String[] argNames = signature.getParameterNames();
Object[] args = joinPoint.getArgs();
log.info("請求路徑:{},請求方式:{},請求參數:{},IP:{}",request.getRequestURI(),
request.getMethod(),
getRequestParam(argNames,args),
request.getRemoteAddr());
}
/**
* 組裝請求參數
* @param argNames 參數名稱
* @param args 參數值
* @return 傳回JSON串
*/
private String getRequestParam(String[] argNames, Object[] args){
HashMap<String,Object> params = new HashMap<>(argNames.length);
if(argNames.length > 0 && args.length > 0){
for (int i = 0; i < argNames.length; i++) {
params.put(argNames[i] , args[i]);
}
}
return JSON.toJSONString(params);
}
}
LoginController
@RestController
public class LoginController {
@PostMapping("/login")
public String login(@RequestBody LoginForm loginForm){
return loginForm.getUsername() + ":登入成功";
}
}
測試一下
将項目啟動然後測試一下。
控制台已經列印出了請求日志。
模拟入庫
将日志儲存到資料庫。
LogServiceImpl
@Slf4j
@Service
public class LogServiceImpl implements LogService {
@Override
public void saveLog(RequestLog requestLog) throws InterruptedException {
// 模拟入庫需要的時間
Thread.sleep(2000);
log.info("請求日志儲存成功:{}",requestLog);
}
}
改造一下LogAspect添加日志入庫
@Before("saveLog()")
public void saveLog(JoinPoint joinPoint) throws InterruptedException {
// 擷取HttpServletRequest
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//擷取請求參數
String[] argNames = signature.getParameterNames();
Object[] args = joinPoint.getArgs();
log.info("請求路徑:{},請求方式:{},請求參數:{},IP:{}",request.getRequestURI(),
request.getMethod(),
getRequestParam(argNames,args),
request.getRemoteAddr());
// 日志入庫
RequestLog requestLog = new RequestLog();
requestLog.setRequestUrl(request.getRequestURI());
requestLog.setRequestType(request.getMethod());
requestLog.setRequestParam(request.getRequestURI());
requestLog.setIp(request.getRemoteAddr());
logService.saveLog(requestLog);
}
使用 @Async
@Async
由于儲存日志消耗了2s,導緻接口的響應時間也增加了2s。這樣的結果顯然不是我想要的。是以我們就按外賣小哥的方法,在
LogServiceImpl.saveLog()
上加一個
@Async
試試。
@Slf4j
@Service
public class LogServiceImpl implements LogService {
@Async
@Override
public void saveLog(RequestLog requestLog) throws InterruptedException {
// 模拟入庫需要的時間
Thread.sleep(2000);
log.info("請求日志儲存成功:{}",requestLog);
}
}
重新啟動項目測試一下。
發現耗時還是2s多,這外賣小哥在瞎扯吧,于是轉身進入了
baidu
的知識海洋遨遊,發現要在啟動類加個
@EnableAsync
。
@EnableAsync
@SpringBootApplication
public class LogApplication {
public static void main(String[] args) {
SpringApplication.run(LogApplication.class, args);
}
}
啟動一下項目再來測試一下。
這下可好啟動都失敗了。
不要慌,先看一眼錯誤資訊。因為有些service使用了CGLib這種動态代理而不是JDK原生的代理,導緻問題的出現。是以我們需要給
@EnableAsync
加上
proxyTargetClass=true
@Slf4j
@EnableAsync(proxyTargetClass=true)
@SpringBootApplication
public class LogApplication {
public static void main(String[] args) {
SpringApplication.run(LogApplication.class, args);
}
}
重新啟動下再測試一下。
這下就成功了嘛,接口響應耗時變成了
324ms
,已經不像之前消耗
2s
那樣了。
有傳回值的方法
由于
saveLog()
是沒有傳回值,假如碰到有傳回值的情況該咋辦呢?使用
Future<T>
即可。
@Slf4j
@Service
public class LogServiceImpl implements LogService {
@Async
@Override
public Future<Boolean> saveLog(RequestLog requestLog) throws InterruptedException {
// 模拟入庫需要的時間
Thread.sleep(2000);
log.info("請求日志儲存成功:{}",requestLog);
return new AsyncResult<>(true);
}
}
配置線程池
既然是異步方法,肯定是用其他的線程執行的,當然可以配置相應的線程池了。
@Configuration
public class ThreadConfig {
/**
* 日志異步儲存輸出線程池
* @return 傳回線程池
*/
@Bean("logExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(200);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("logExecutor-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setAwaitTerminationSeconds(60);
return executor;
}
}
在使用
@Async
的時候指定對應的線程池就好了。
@Slf4j
@Service
public class LogServiceImpl implements LogService {
@Override
@Async("logExecutor")
public Future<Boolean> saveLog(RequestLog requestLog) throws InterruptedException {
// 模拟入庫需要的時間
Thread.sleep(2000);
log.info("請求日志儲存成功:{}",requestLog);
return new AsyncResult<>(true);
}
}
注意的點
- 使用之前需要在啟動類開啟
@EnableAsync
- 隻能在自身之外調用,在本類調用是無效的。
- 所有的類都需要交由Spring容器進行管理。
總結
@Async
标注的方法,稱之為異步方法;這些方法将在執行的時候,将會在獨立的線程中被執行,調用者無需等待它的完成,即可繼續其他的操作。
雖然自己維護線程池也是可以實作相應的功能,但是我還是推薦使用
SpringBoot
自帶的異步方法,簡單友善,隻需要
@Async
和
@EnableAsync
就可以了。
結尾
為什麼外賣小哥能看懂我寫的代碼?難道我以後也要去xxx?
如果覺得對你有幫助,可以多多評論,多多點贊哦,也可以到我的首頁看看,說不定有你喜歡的文章,也可以随手點個關注哦,謝謝。
我是不一樣的科技宅,每天進步一點點,體驗不一樣的生活。我們下期見!