Skip to content

Commit

Permalink
✨ (xxljob) 定时任务执行时在 MDC 中添加 traceId,方便追踪上下文日志
Browse files Browse the repository at this point in the history
  • Loading branch information
Hccake committed Feb 28, 2024
1 parent 151b6b4 commit b29c36a
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
4 changes: 4 additions & 0 deletions job/ballcat-spring-boot-starter-xxljob/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
import lombok.extern.slf4j.Slf4j;
import org.ballcat.autoconfigure.xxljob.properties.XxlExecutorProperties;
import org.ballcat.autoconfigure.xxljob.properties.XxlJobProperties;
import org.ballcat.autoconfigure.xxljob.trace.TraceXxlJobAnnotationAdvisor;
import org.ballcat.autoconfigure.xxljob.trace.TraceXxlJobAnnotationInterceptor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -108,4 +112,22 @@ private String getLogPath(XxlExecutorProperties properties) {
.concat("/jobs");
}

@Configuration(proxyBeanMethods = false)
static class TraceConfiguration {

@Bean
@ConditionalOnMissingBean
public TraceXxlJobAnnotationInterceptor traceXxlJobAnnotationInterceptor() {
return new TraceXxlJobAnnotationInterceptor();
}

@Bean
@ConditionalOnMissingBean
public TraceXxlJobAnnotationAdvisor traceXxlJobAnnotationAdvisor(
TraceXxlJobAnnotationInterceptor xxlJobAnnotationInterceptor) {
return new TraceXxlJobAnnotationAdvisor(xxlJobAnnotationInterceptor);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ballcat.autoconfigure.xxljob.trace;

import com.xxl.job.core.handler.annotation.XxlJob;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

/**
* XxlJob Trace 增强器,切点为添加了 {@link XxlJob} 注解的方法。
*
* @author Hccake
* @see TraceXxlJobAnnotationInterceptor
*/
public class TraceXxlJobAnnotationAdvisor extends AbstractPointcutAdvisor {

private final Advice advice;

private final Pointcut pointcut;

public TraceXxlJobAnnotationAdvisor(Advice advice) {
this.advice = advice;
this.pointcut = new AnnotationMatchingPointcut(null, XxlJob.class, true);
}

@Override
public Pointcut getPointcut() {
return this.pointcut;
}

@Override
public Advice getAdvice() {
return this.advice;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ballcat.autoconfigure.xxljob.trace;

import java.util.UUID;

import com.xxl.job.core.context.XxlJobHelper;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.MDC;

/**
* 增强 XxlJob Trace 能力的拦截器,在 jobHandler execute 前在 MDC 中置入 traceId, 方便追踪上下文日志。
*
* @author Hccake
*/
public class TraceXxlJobAnnotationInterceptor implements MethodInterceptor {

private final static String TRACE_ID = "traceId";

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
MDC.put(TRACE_ID, generateTraceId());
try {
return invocation.proceed();
}
finally {
MDC.remove(TRACE_ID);
}
}

/**
* 生成 traceId, 默认使用 jobId 拼接无下划线的 UUID
* @return traceId
*/
protected String generateTraceId() {
long jobId = XxlJobHelper.getJobId();
String simpleUUID = UUID.randomUUID().toString().replace("-", "");
return jobId + "-" + simpleUUID;
}

}

0 comments on commit b29c36a

Please sign in to comment.