简介

XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码可以根据自己需要进行修改,开箱即用,非常爽;

部署xxl-job项目

首先去github上下载:https://github.com/xuxueli/xxl-job/
下载源码,然后在本地进行编译
修改配置xxl-job-admin中的application.properties

server.port=8080
server.servlet.context-path=/xxl-job-admin
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=xxx
spring.datasource.password=xxxxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
### xxl-job, email
spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=xxx@qq.com
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory### xxl-job, access token
xxl.job.accessToken=xxxxxx

配置很多,但是只需要修改上面这几个,最好将xxl.job.accessToken添加上进行token验证
修改完打包部署
部署完可以通过地址查看管理页面:http://127.0.0.1:8080/xxl-job-admin/

默认账号密码admin 123456,可以登录进去看看

引入项目

创建网关项目pom文件中引入jar包

<dependency><groupId>com.xuxueli</groupId><artifactId>xxl-job-core</artifactId><version>2.2.0</version>
</dependency>

在配置文件中增肌配置

xxl:job:accessToken: xxxxxx  #修改的token秘钥admin:addresses: http://10.1.11.60:8080/xxl-job-admin/   #上面部署xxl-job-admin地址executor:appname: dayunmotor-tsp-gateway-xxl-job  #本项目名称ip: 10.1.5.27 #这个是本项目部署地址和端口port: 30003   #注意这里不是本项目的端口是xxl-job执行请求的端口logpath: /data/applogs/xxl-job/jobhandlerlogretentiondays: 30

在项目中增加配置类

@Slf4j
@Configuration
public class XxlJobConfig {@Value("${xxl.job.admin.addresses}")private String adminAddresses;@Value("${xxl.job.accessToken}")private String accessToken;@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.executor.logpath}")private String logPath;@Value("${xxl.job.executor.logretentiondays}")private int logRetentionDays;@Beanpublic XxlJobSpringExecutor xxlJobExecutor() {log.info(">>>>>>>>>>> xxl-job config init.");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;}
}

增加定时任务

@Slf4j
@Component
public class RemoteUpgradeTask extends IJobHandler {@XxlJob(value = "remoteUpgradeTask")public ReturnT<String> execute(String s) throws Exception {try {log.info("执行任务");return IJobHandler.SUCCESS;} catch (Exception e) {log.error("执行任务异常", e);return new ReturnT<>(IJobHandler.FAIL.getCode(), "执行任务失败");}}//执行初始化方法@Overridepublic void init() throws InvocationTargetException, IllegalAccessException {super.init();}//执行销毁方法@Overridepublic void destroy() throws InvocationTargetException, IllegalAccessException {super.destroy();}
}

然后去xxl-job-admin中增加执行器,增加完启动项目,可以看到启动的执行器

然后创建定时任务:注意这里的JobHandler就是代码里的 @XxlJob(value = “remoteUpgradeTask”)

创建完启动任务,就可以执行了,这个就完成了
不过如果使用windows的时候注意关闭防火墙,不然没法注册上去
如果本地有多网络需要指定IP,而服务器只有一个网络,为了保证本地可以进行调试,可以进行兼容;
获取当前环境,根据环境进行设置IP
首先获取当前spring.profiles.active

@Configuration
public class ApplicationContextUtils implements ApplicationContextAware {private static ApplicationContext applicationContext = null;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {if(null == ApplicationContextUtils.applicationContext) {ApplicationContextUtils.applicationContext = applicationContext;}}public static ApplicationContext getApplicationContext() {return applicationContext;}public static Object getBean(String name) {return applicationContext.getBean(name);}public static <T> T getBean(Class<T> clazz) {return applicationContext.getBean(clazz);}public static String getActiveProfile() {return applicationContext.getEnvironment().getActiveProfiles()[0];}
}

然后修改xxl-job配置,修改XxlJobConfig类

    @Beanpublic XxlJobSpringExecutor xxlJobExecutor() {log.info(">>>>>>>>>>> xxl-job config init.");XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();xxlJobSpringExecutor.setAdminAddresses(adminAddresses);xxlJobSpringExecutor.setAppname(appname);if(ApplicationContextUtils.getActiveProfile().equalsIgnoreCase("local")){xxlJobSpringExecutor.setIp(ip);}xxlJobSpringExecutor.setPort(port);xxlJobSpringExecutor.setAccessToken(accessToken);xxlJobSpringExecutor.setLogPath(logPath);xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);return xxlJobSpringExecutor;}

springboot使用xxl-job相关推荐

  1. 基于SpringBoot的CodeGenerator

    title: 基于SpringBoot的CodeGenerator tags: SpringBoot Mybatis 生成器 PageHelper categories: springboot dat ...

  2. springboot自带的线程池ThreadPoolTaskExecutor、ThreadPoolTaskScheduler的深入应用——异步任务监听回调,任务中断案例

    一.常用的的线程池对象 1.jdk原生的两个常用线程池对象 ThreadPoolExecutor.ScheduledThreadPoolExecutor,后者继承前者,主要增加了任务调度相关的一些方法 ...

  3. xxl-job搭建、部署、SpringBoot集成xxl-job

    一.搭建xxl-job 1.下载xxl-job代码 码云地址:https://gitee.com/xuxueli0323/xxl-job gitHub地址:https://github.com/xux ...

  4. Xxl Job Helloworld

    刚到新公司不久,新公司使用分布式任务调度平台是 xxl-job.其核心设计目标是开发迅速.学习简单.轻量级.易扩展.现已开放源代码并接入多家公司线上产品线,开箱即用.当然它的特性在 它的官网 描述得非 ...

  5. springboot 集成xxl-job 定时任务管理平台

    项目集成xxl-job https://github.com/nlxs0511/springmybatisplus.git 项目集成邮件      https://github.com/nlxs051 ...

  6. SpringBoot + xxl-job 多数据源异构数据增量同步

    SpringBoot + xxl-job 多数据源异构数据增量同步 文章目录 SpringBoot + xxl-job 多数据源异构数据增量同步 一.概述 二.实现步骤 2.1 项目搭建 2.2 接口 ...

  7. SpringBoot+Vue前后端分离的三只松鼠商城实现毕业设计论文

    题目名称 基于SpringBoot的B2C三只松鼠 电商平台 学 院 理工农学院 专业年级 软件工程2018级 填写时间 2021年10月19日 目 录 错误!未找到图形项目表. 基于SpringBo ...

  8. SpringBoot和Vue前后端分离

    SpringBoot-Vue前后端分离 实现增删改查及分页小DEMO 一.前言 主要通过Spring Boot和Vue`来简单实现一个前后端分离的小demo,该小demo实现增删改查.分页功能.主要是 ...

  9. XXL-JOB详解(整合springboot)保姆级教程

    文章目录 XXL-JOB简介 XXL-JOB是什么 为什么需要任务调度平台,而不用传统的 Timer 与 Quartz 为什么选择XXL-JOB,不选择elasticjob ==学习之前必看,少走很多 ...

  10. Swagger的安装以及SpringBoot整合Swagger2实现SwaggerAPI文档测试

    前言 本篇博客是本人在网上学习Swagger所产出的个人笔记.主要内容有: 1.Swagger的安装 2.Swagger的介绍 3.Swagger2中常用的注解 4.快速上手案例 准备工作:Swagg ...

最新文章

  1. C和C++安全编码笔记:整数安全
  2. 【FFmpeg】如何通过字符串到对应的封装器,以flv为例
  3. html键值对与名称值对的区别,使用网络存储存储键值对的数据-HTML5教程
  4. MongoDB安装和启动
  5. rog live service是什么_双11手机怎么买?ROG游戏手机3“独一份”体验,值得剁手...
  6. leetcode 454. 四数相加 II(哈希表)
  7. 2020 年,为什么非要采用 DevOps 文化不可?
  8. Spring.Net学习笔记九(自定义对象行为)
  9. 在字典中根据条件来筛选数据
  10. Python教程:丛入门到实践
  11. 好的计算机教学,如何营造良好的计算机课堂教学氛围
  12. 2019-11-20 c语言参考手册
  13. Learn OpenGL(七)——OpenGL中使用着色器的基本步骤及GLSL渲染简单示例
  14. [置顶] Android仿人人客户端(v5.7.1)——应用主界面之左侧面板UI实现
  15. 使用Python写的第一个网络爬虫程序
  16. PDF格式分析(六)PDF版本
  17. 无线射频专题《IEEE 802.11协议讲解1@路由高级配置项,Beacon周期、RTS阈值、DTIM》
  18. 剑侠世界3怎么快速起号?
  19. 跟我一起从零学习安卓逆向分析
  20. vue中warning_使用vue的i18n 出现很多warning提示

热门文章

  1. 数据分析--Python连接阿里云数据库
  2. VM虚拟机分区硬盘/安装win10系统
  3. Python调用百度API进行语音识别
  4. 格物、致知、正心、诚意、修身、齐家、治国、平天下
  5. Flask学习(二)-Jinji2模板引擎
  6. 转-注册表对应项详解
  7. Squid在Windows平台搭建代理服务器
  8. 解决Page index must not be less than zero问题
  9. Rabbit MQ 基础
  10. 计算机网络——Cisco Packet Tracer 实验