|
| 1 | +# XXL-JOB模块 |
| 2 | + |
| 3 | +官方地址: https://www.xuxueli.com/xxl-job |
| 4 | + |
| 5 | +当前使用版本: 2.2.0 |
| 6 | + |
| 7 | +## 概述 |
| 8 | + |
| 9 | +基于分布式调度平台客户端进行二次封装。 |
| 10 | + |
| 11 | +核心功能点: |
| 12 | + |
| 13 | ++ 插拔式配置 |
| 14 | + |
| 15 | ++ 日志无感输出到xxl-admin控制台 |
| 16 | + |
| 17 | ++ 支持追踪式id,便捷问题排查。 |
| 18 | + |
| 19 | + traceId生成规则=jobId(任务id)-uuid(无符号) |
| 20 | + |
| 21 | +## 使用方式 |
| 22 | + |
| 23 | +### 1.引入pom |
| 24 | + |
| 25 | +```xml |
| 26 | + <!--xxl job相关--> |
| 27 | + <dependency> |
| 28 | + <groupId>com.lovecyy</groupId> |
| 29 | + <artifactId>relaxed-spring-boot-starter-job</artifactId> |
| 30 | + <version>${version}</version> |
| 31 | + </dependency> |
| 32 | +``` |
| 33 | + |
| 34 | +### 2.启用xxl |
| 35 | + |
| 36 | +```java |
| 37 | +@EnableXxlJob |
| 38 | +@MapperScan(basePackages = "org.example.**.mapper") |
| 39 | +@SpringBootApplication(scanBasePackages = {"org.example","com.relaxed"}) |
| 40 | +public class TaskApplication implements ApplicationRunner { |
| 41 | + public static void main(String[] args) { |
| 42 | + SpringApplication.run(TaskApplication.class,args); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void run(ApplicationArguments args) throws Exception { |
| 47 | + //因为采用非Web模式启动,1.0需加入如下代码,不然会导致启动后立马关闭,2.0可以忽略,若发现同样情况,打开即可 |
| 48 | + while (true){ |
| 49 | + Thread.sleep(100); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### 3.配置yml |
| 56 | + |
| 57 | +```yml |
| 58 | +#xxl job配置如下 |
| 59 | +xxl: |
| 60 | + job: |
| 61 | + admin: |
| 62 | + addresses: http://127.0.0.1:9190/xxl-job-admin |
| 63 | + executor: |
| 64 | + appname: xxl-job-test |
| 65 | + ip: |
| 66 | + port: 50013 |
| 67 | + # 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径; |
| 68 | + logpath: /data/applogs/xxl-job/jobhandler |
| 69 | + # 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能; |
| 70 | + logretentiondays: 30 |
| 71 | + #缺省配置 默认已经填充 |
| 72 | + log: |
| 73 | + #拦截日志前缀 |
| 74 | + log-prefix: XXL- |
| 75 | + #appender名称 |
| 76 | + name: logXXl |
| 77 | + #是否启用appender配置,默认开启 |
| 78 | + enabled: true |
| 79 | + #最小拦截级别 TRACE INFO WARN ERROR |
| 80 | + min-level: INFO |
| 81 | + #扫描包 |
| 82 | + includePackages: |
| 83 | + #忽略包 |
| 84 | + excludePackages: |
| 85 | + |
| 86 | +``` |
| 87 | + |
| 88 | +### 4.测试job |
| 89 | + |
| 90 | +```java |
| 91 | +@Slf4j |
| 92 | +@Component |
| 93 | +public class LogXxlJob { |
| 94 | + |
| 95 | +@XxlJob("testLogPrint") |
| 96 | +public ReturnT<String> testLogPrint(String params) { |
| 97 | + |
| 98 | +log.info("当前执行任务名称:testLogPrint"); |
| 99 | +log.info("XXL-这是一条双appender日志"); |
| 100 | +// 异常日志示例 |
| 101 | +try { |
| 102 | +throw new RuntimeException("测试异常"); |
| 103 | +} |
| 104 | +catch (Exception e) { |
| 105 | +log.error("XXL-发生错误", e); |
| 106 | +} |
| 107 | +return new ReturnT<>("执行成功"); |
| 108 | +} |
| 109 | +} |
| 110 | + |
| 111 | +``` |
| 112 | + |
| 113 | +启动项目,调度日志。 |
| 114 | + |
| 115 | +即可发现XXL-开头前缀日志被同步输出到xxl-admin的日志平台上。 |
| 116 | + |
| 117 | +且trace_id自动被输出。 |
| 118 | + |
| 119 | +## 附: |
| 120 | + |
| 121 | +### 多线程情况线程切换导致traceId丢失 |
| 122 | + |
| 123 | +问题:xxl-job任务内若涉及多线程执行任务,由于线程池策略,线程切换可能导致traceId丢失。 |
| 124 | + |
| 125 | +解决方案: |
| 126 | + |
| 127 | +#### 1.装饰线程 |
| 128 | + |
| 129 | +将任务线程通过`com.relaxed.admin.core.thread.MdcTaskDecorator`类进行装饰。 |
| 130 | + |
| 131 | +```java |
| 132 | +/** |
| 133 | + * mdc 任务解码器 |
| 134 | + * @author yakir |
| 135 | + */ |
| 136 | +@Bean |
| 137 | +public TaskDecorator mdcTaskDecorator() { |
| 138 | +return new MdcTaskDecorator(); |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +**MdcTaskDecorator.java** |
| 143 | + |
| 144 | +``` |
| 145 | +public class MdcTaskDecorator implements TaskDecorator { |
| 146 | +
|
| 147 | +@Override |
| 148 | +public Runnable decorate(Runnable runnable) { |
| 149 | +Map<String, String> contextMap = MDC.getCopyOfContextMap(); |
| 150 | +return () -> { |
| 151 | +Map<String, String> old = MDC.getCopyOfContextMap(); |
| 152 | +try { |
| 153 | +// 现在:@Async线程上下文! |
| 154 | +// 恢复Web线程上下文的MDC数据 |
| 155 | +if (MapUtil.isNotEmpty(contextMap)) { |
| 156 | +MDC.setContextMap(contextMap); |
| 157 | +} |
| 158 | +runnable.run(); |
| 159 | +} |
| 160 | +finally { |
| 161 | +if (old==null){ |
| 162 | +MDC.clear(); |
| 163 | +}else{ |
| 164 | +MDC.setContextMap(old); |
| 165 | +} |
| 166 | +
|
| 167 | +} |
| 168 | +}; |
| 169 | +} |
| 170 | +
|
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +#### 2.配置线程池 |
| 175 | + |
| 176 | +```java |
| 177 | +@Configuration(proxyBeanMethods = false) |
| 178 | +public class ThreadPoolConfig { |
| 179 | +// 核心线程池大小 |
| 180 | +private int corePoolSize = 12; |
| 181 | + |
| 182 | +// 最大可创建的线程数 |
| 183 | +private int maxPoolSize = 36; |
| 184 | + |
| 185 | +// 队列最大长度 |
| 186 | +private int queueCapacity = 1000; |
| 187 | + |
| 188 | +// 线程池维护线程所允许的空闲时间 |
| 189 | +private int keepAliveSeconds = 300; |
| 190 | + |
| 191 | +/** |
| 192 | + * 线程池自定义 |
| 193 | + * @author yakir |
| 194 | + * @return org.springframework.boot.task.TaskExecutorCustomizer |
| 195 | + */ |
| 196 | +@Bean |
| 197 | +public TaskExecutorCustomizer taskExecutorCustomizerExtend() { |
| 198 | +return (executor) -> { |
| 199 | +executor.setThreadNamePrefix("task-"); |
| 200 | +executor.setMaxPoolSize(maxPoolSize); |
| 201 | +executor.setCorePoolSize(corePoolSize); |
| 202 | +executor.setQueueCapacity(queueCapacity); |
| 203 | +executor.setKeepAliveSeconds(keepAliveSeconds); |
| 204 | +// 线程池对拒绝任务(无线程可用)的处理策略 |
| 205 | +executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); |
| 206 | +}; |
| 207 | +} |
| 208 | + |
| 209 | +} |
| 210 | + |
| 211 | +``` |
| 212 | + |
| 213 | +使用此线程池,执行的任务traceId会被同步传递。 |
| 214 | + |
| 215 | +### 配置方式添加appender |
| 216 | + |
| 217 | +**配置logback-spring.xml** |
| 218 | + |
| 219 | +> 标识当前XXL-开头,且日志级别INFO以上的日志将被拦截 |
| 220 | +
|
| 221 | +```xml |
| 222 | +<?xml version="1.0" encoding="UTF-8"?> |
| 223 | +<configuration> |
| 224 | + <!-- 其他已有配置 --> |
| 225 | + |
| 226 | + <!-- 添加XXL-JOB日志Appender --> |
| 227 | + <appender name="XXL-JOB" class="com.relaxed.common.job.XxlJobLogAppender"> |
| 228 | + <logPrefix>XXL-</logPrefix> |
| 229 | + <minLevel>INFO</minLevel> |
| 230 | + </appender> |
| 231 | + |
| 232 | + <root level="INFO"> |
| 233 | + |
| 234 | + <!-- 添加XXL-JOB appender --> |
| 235 | + <appender-ref ref="XXL-JOB"/> |
| 236 | + </root> |
| 237 | +</configuration> |
| 238 | + |
| 239 | +``` |
| 240 | + |
| 241 | +### 编程式添加appender |
| 242 | + |
| 243 | +实现`LogbackAppenderProvider`接口,注册成bean即可。替换默认appender的生成。 |
| 244 | + |
0 commit comments