①执行数据库脚本tables_xxl_job.sql,生成库表
②通过nginx为调度中心集群做负载均衡,分别启动8001和8002端口的xxl-job-admin实例,建议修改xxl-job-admin的打包方式,将配置文件application.properties从jar包中剥离出来,方便修改配置
application.properties
### web
server.port=8001
server.context-path=/xxl-job-admin
### resources
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/
### freemarker
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.suffix=.ftl
spring.freemarker.charset=UTF-8
spring.freemarker.request-context-attribute=request
spring.freemarker.settings.number_format=0.##########
### mybatis
mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://192.168.124.129:3306/xxl-job?Unicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=30
spring.datasource.tomcat.test-on-borrow=true
### xxl-job email
spring.mail.host=smtp.qq.com
spring.mail.port=25
[email protected]
###填的是SMTP授权码,不是邮箱密码
spring.mail.password=xxxxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
### xxl-job login
xxl.job.login.username=admin
xxl.job.login.password=123456
### xxl-job, access token
xxl.job.accessToken=myAccessToken
### xxl-job, i18n (default empty as chinese, "en" as english)
xxl.job.i18n=
③配置并启动Nginx
http {
upstream xxl{
server 192.168.124.129:8001;
server 192.168.124.129:8002;
}
server{
listen 8088;
location / {
proxy_pass http://xxl;
}
}
}
③成功后,我们可以通过nginx来访问xxl-job-admin集群了
http://192.168.124.129:8088/xxl-job-admin
配置执行器和任务
路由策略置为轮询,cron表达式设置为每五秒调度一次
新建执行器项目XXL-Task,pom.xml文件引入依赖,似乎存在与SpringBoot版本不兼容的情况,启动会报异常,此处采用的SpringBoot版本为1.5.18.RELEASE
org.springframework.boot
spring-boot-starter-parent
1.5.18.RELEASE
com.xuxueli
xxl-job-core
2.0.1
application.properties文件(Appname类比MQ的Group)
server.port=9001
#xxl-job调度中心工程的地址
xxl.job.admin.addresses=http://192.168.124.129:8088/xxl-job-admin
#xxl-job executor
xxl.job.executor.appname=testAppName
xxl.job.executor.ip=
xxl.job.executor.port=9999
#xxl-job log path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
#xxl-job, access token
xxl.job.accessToken=myAccessToken
#执行器Log文件定期清理功能,指定日志保存天数,日志文件过期自动删除。限制至少保持3天,否则功能不生效
xxl.job.executor.logretentiondays=-1
XxlJobConfig
package com.yj.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
@Configuration
@ComponentScan(basePackages = "com.yj.handler")
public class XxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.executor.appname}")
private String appName;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppName(appName);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
MyJobHandler
package com.yj.handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
@JobHandler(value = "myJobHandler")
@Component
public class MyJobHandler extends IJobHandler {
private static final Logger log = LoggerFactory.getLogger(MyJobHandler.class);
@Value("${server.port}")
private String port;
@Override
public ReturnT execute(String param) throws Exception {
XxlJobLogger.log("Hello World,port:"+port);
log.info("Hello World,port:"+port);
return SUCCESS;
}
}
分别启动server.port=9001,9002端口的执行器实例,其中,xxl.job.executor.port=9999,9998,xxl.job.executor.appname都为testAppName
启动后在xxl-job-admin控制台(有点类似Eureka)已经能看到注册成功的执行器实例了
查看执行器控制台日志,发现xxl-job-admin对9001和9002的XXL-Task项目进行了负载均衡的调度(类比MQ集群模式)
15:00:53.254 logback [Thread-28] INFO com.yj.handler.MyJobHandler - Hello World,port:9002
15:00:58.284 logback [Thread-16] INFO com.yj.handler.MyJobHandler - Hello World,port:9001
15:01:03.261 logback [Thread-28] INFO com.yj.handler.MyJobHandler - Hello World,port:9002
我们再将路由策略置为分片广播的模式(类比MQ广播模式)
15:14:43.277 logback [Thread-28] INFO com.yj.handler.MyJobHandler - Hello World,port:9002
15:14:48.281 logback [Thread-28] INFO com.yj.handler.MyJobHandler - Hello World,port:9002
15:14:53.264 logback [Thread-28] INFO com.yj.handler.MyJobHandler - Hello World,port:9002
15:14:43.343 logback [Thread-26] INFO com.yj.handler.MyJobHandler - Hello World,port:9001
15:14:48.299 logback [Thread-26] INFO com.yj.handler.MyJobHandler - Hello World,port:9001
15:14:53.340 logback [Thread-26] INFO com.yj.handler.MyJobHandler - Hello World,port:9001
我们再验证下邮件的报警功能,在执行器任务中抛出一个异常int i=1/0,会收到告警邮件