👉官方文档链接:部署文档
👉项目官网:https://cronjob.horace.cn
👉Gitee仓库:https://gitee.com/horacedh/cron-job
👉GitHub仓库:https://github.com/horacedh/cron-job
一、环境要求
- OS:支持Linux、Windows、MacOS,文中以CentOS为例。
- Java:1.8+,官方用的是:1.8.0_391,64位。
- MySQL:5.7+,官方用的是: 5.7.35-log Source distribution。
二、发行版下载地址
- 下载发行版
- GitHub:https://github.com/HoraceDH/cron-job/releases
- Gitee:https://gitee.com/HoraceDH/cron-job/releases
- 解压文件,这里以gitee仓库为例,下载最新的版本,解压后会得到如下文件内容:
- cronjob-examples:示例代码,可直接运行
- 包含原生Java版本的 example-executor
- SpringBoot版本的 example-executor-starter
- cronjob-managerui:管理后台UI
- cronjob-scheduler:调度器,负责任务调度的服务端
- mysql.sql:完整的数据库初始化脚本
三、初始化数据库
- 初始化SQL,找到
mysql.sql
文件,进入到MySQL命令行中,执行以下命令即可完成初始化,其中path替换为自己的路径:
source /path/mysql.sql
- 成功后,会产生
cron-job
库,并包含如下表:
四、调度器服务端部署
1. 部署第一台实例
1.1 修改配置文件
- 文件路径
etc/application.properties
:
############# 调度器配置 ############# # 用于标识实例的唯一性 server.id=1 ############# 调度器配置 ############# ############# 数据源配置 ############# database.url=jdbc:mysql://localhost:3306/cron-job?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true database.username=horace database.password=SGJIWkjfdsof923847 ############# 数据源配置 ############# ############# Tomcat配置 ############# # Tomcat服务配置 server.port=9527 ############# Tomcat配置 #############
- 主要修改以下配置:
- server.id:用于标识实例的唯一性,每个实例必须唯一。
- server.port:服务端口,如果在同个机器部署,则需要修改端口,保证端口不冲突。
- database.url:数据库连接地址。
- database.username:数据库用户名。
- database.password:数据库密码。
1.2 启动第一台实例
- 执行命令:
sh start.sh
- 查看日志输出,以下表示启动成功,有错则需要看日志排错,日志路径
logs/app.log
:
2025-02-18 17:28:24.427 INFO [background-preinit]o.h.v.i.util.Version - HV000001: Hibernate Validator 6.2.0.Final 2025-02-18 17:28:24.432 INFO [main]c.h.c.s.SchedulerMain - Starting SchedulerMain using Java 1.8.0_391 on HoraceiMacProMax.local with PID 4420 (/Users/horace/Downloads/cron-job-v1.0.1/cronjob-scheduler/cronjob-scheduler-1.0.1.jar started by horace in /Users/horace/Downloads/cron-job-v1.0.1/cronjob-scheduler) 2025-02-18 17:28:24.432 INFO [main]c.h.c.s.SchedulerMain - No active profile set, falling back to 1 default profile: "default" 2025-02-18 17:28:25.276 INFO [main]o.s.b.w.e.t.TomcatWebServer - Tomcat initialized with port(s): 9527 (http) 2025-02-18 17:28:25.283 INFO [main]o.a.c.h.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-9527"] 2025-02-18 17:28:25.284 INFO [main]o.a.c.c.StandardService - Starting service [Tomcat] 2025-02-18 17:28:25.284 INFO [main]o.a.c.c.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.65] 2025-02-18 17:28:25.399 INFO [main]o.a.c.c.C.[.[.[/] - Initializing Spring embedded WebApplicationContext 2025-02-18 17:28:25.399 INFO [main]o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 932 ms 2025-02-18 17:28:25.822 INFO [main]c.a.d.p.DruidDataSource - {dataSource-1} inited 2025-02-18 17:28:26.338 INFO [main]c.h.c.s.s.i.SchedulerServiceImpl - find router strategy handler, name:randomRouterStrategyHandlerImpl, handler:cn.horace.cronjob.scheduler.strategy.RandomRouterStrategyHandlerImpl@431e86b1 2025-02-18 17:28:26.339 INFO [main]c.h.c.s.s.i.SchedulerServiceImpl - find router strategy handler, name:shardingRouterStrategyHandlerImpl, handler:cn.horace.cronjob.scheduler.strategy.ShardingRouterStrategyHandlerImpl@5f36c8e3 2025-02-18 17:28:26.430 INFO [scheduler-thread-1]c.h.c.s.s.i.StatisticsServiceImpl - start statistics, startDate:null, endDate:Tue Feb 18 17:25:00 CST 2025 2025-02-18 17:28:26.432 INFO [scheduler-thread-1]c.h.c.s.s.i.StatisticsServiceImpl - end statistics, count:0, startDate:null, endDate:Tue Feb 18 17:25:00 CST 2025 2025-02-18 17:28:26.661 INFO [main]o.a.c.h.Http11NioProtocol - Starting ProtocolHandler ["http-nio-9527"] 2025-02-18 17:28:26.684 INFO [main]o.s.b.w.e.t.TomcatWebServer - Tomcat started on port(s): 9527 (http) with context path '' 2025-02-18 17:28:26.693 INFO [main]c.h.c.s.SchedulerMain - Started SchedulerMain in 2.551 seconds (JVM running for 3.136) 2025-02-18 17:28:36.421 INFO [scheduler-thread-1]c.h.c.s.s.i.StatisticsServiceImpl - start statistics, startDate:null, endDate:Tue Feb 18 17:25:00 CST 2025 2025-02-18 17:28:36.423 INFO [scheduler-thread-1]c.h.c.s.s.i.StatisticsServiceImpl - end statistics, count:0, startDate:null, endDate:Tue Feb 18 17:25:00 CST 2025
2. 部署第N台实例
- 拷贝一份
cronjob-scheduler
,作为第N台实例,按照第一台实例的方式修改配置文件即可。 - 如若部署更多实例,按照第一台实例的做法,依次部署即可,注意修改
server.id
以及相关配置。
五、管理后台UI部署
1. 存放静态资源
管理后台UI可以在发布包中找到cronjob-managerui
目录,将cronjob-managerui
放到合适的目录下,该目录就是打包后的前端文件。
2. Nginx配置
参照如下模板配置即可,注意修改对应的信息:
upstream cron_job.server { # 配置多个调度器地址,用于负载均衡 server 127.0.0.1:9527; server 127.0.0.1:9528; } server { listen 80; server_name 127.0.0.1; error_log logs/127.0.0.1.error.log error; access_log logs/127.0.0.1.access.log main; # 管理后台UI静态资源 location / { # 用于配合ant.design browserHistory 使用 try_files $uri $uri/index.html /index.html; root /Users/horace/Downloads/cron-job-v1.0.1/cronjob-managerui; index index.html; } # 管理后台API接口 location /manager-api { proxy_pass http://cron_job.server; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; } # 开放API接口 location /openapi { proxy_pass http://cron_job.server; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; } }
3. 启动Nginx
访问http://127.0.0.1
,实际访问地址根据自身部署的站点情况调整,实际生产部署需要配置域名。
4. 登录任务调度平台
看到如下页面时,说明部署成功,输入账号密码 admin/admin 进入系统:
六、启动示例工程
1. 进入目录
进入cronjob-examples/example-executor
目录,并执行如下命令:
sh start.sh
2. 检查执行器日志
出现以下表示启动成功,其中cron job executor register success
表示执行器注册成功,cron job task register success
执行器任务注册成功。
2025-02-18 17:59:40.855 INFO [main]c.h.c.e.s.TaskService - found cron job task, TaskConfig{tenant='horace', method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', name='普通测试任务', appName='example-executor', appDesc='普通示例执行器', tag='common', remark='', cron='* * * * * ? ', routerStrategy=1, expiredStrategy=2, expiredTime=180000, failureStrategy=2, timeout=10000, maxRetryCount=5, failureRetryInterval=5000} 2025-02-18 17:59:40.868 INFO [cron-job-http-server]o.eclipse.jetty.util.log - Logging initialized @448ms to org.eclipse.jetty.util.log.Slf4jLog 2025-02-18 17:59:40.944 INFO [cron-job-http-server]o.e.jetty.server.Server - jetty-9.4.48.v20220622; built: 2022-06-21T20:42:25.880Z; git: 6b67c5719d1f4371b33655ff2d047d24e171e49a; jvm 1.8.0_391-b13 2025-02-18 17:59:40.968 INFO [cron-job-http-server]o.e.jetty.server.session - DefaultSessionIdManager workerName=node0 2025-02-18 17:59:40.969 INFO [cron-job-http-server]o.e.jetty.server.session - No SessionScavenger set, using defaults 2025-02-18 17:59:40.970 INFO [cron-job-http-server]o.e.jetty.server.session - node0 Scavenging every 600000ms 2025-02-18 17:59:40.978 INFO [cron-job-http-server]o.e.j.s.h.ContextHandler - Started o.e.j.s.ServletContextHandler@688910fd{/,null,AVAILABLE} 2025-02-18 17:59:40.992 INFO [cron-job-http-server]o.e.j.s.AbstractConnector - Started ServerConnector@69735139{HTTP/1.1, (http/1.1)}{192.168.16.51:8527} 2025-02-18 17:59:40.992 INFO [cron-job-http-server]o.e.jetty.server.Server - Started @572ms 2025-02-18 17:59:40.992 INFO [cron-job-http-server]c.h.c.e.h.HttpServer - cron job web server started, address:192.168.16.51:8527 2025-02-18 17:59:41.343 INFO [scheduler-thread-1]c.h.c.e.s.OpenApiService - cron job executor register success, serverAddress:http://127.0.0.1, params:ExecutorRegisterParams{tenant='horace', appName='example-executor', appDesc='普通示例执行器', hostName='HoraceiMacProMax.local', tag='common', version='Java-1.0.1', address='192.168.16.51:8527'}, result:MsgObject{code=0, msg='', data=null} 2025-02-18 17:59:42.407 INFO [scheduler-thread-1]c.h.c.e.s.OpenApiService - cron job task register success, address:192.168.16.51:8527, result:MsgObject{code=0, msg='', data=null}
3. 启动任务
- 登录管理后台,点击任务管理,点击启动任务,即可开始任务的调度。
4. 查看调度日志
可以看到,任务的调度很准时!
2025-02-18 18:03:51.000 INFO [executor-task-5]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791224381441, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873031000, receivedDispatcherTime=1739873028002, params=''} 2025-02-18 18:03:52.000 INFO [executor-task-1]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791241158657, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873032000, receivedDispatcherTime=1739873029002, params=''} 2025-02-18 18:03:53.000 INFO [executor-task-2]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791257935873, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873033000, receivedDispatcherTime=1739873030005, params=''} 2025-02-18 18:03:54.000 INFO [executor-task-3]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791274713089, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873034000, receivedDispatcherTime=1739873031002, params=''} 2025-02-18 18:03:55.000 INFO [executor-task-4]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791291490305, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873035000, receivedDispatcherTime=1739873032002, params=''} 2025-02-18 18:03:56.000 INFO [executor-task-5]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791299878913, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873036000, receivedDispatcherTime=1739873033002, params=''} 2025-02-18 18:03:57.000 INFO [executor-task-1]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791308267521, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873037000, receivedDispatcherTime=1739873034002, params=''} 2025-02-18 18:03:58.000 INFO [executor-task-2]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791325044737, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873038000, receivedDispatcherTime=1739873035002, params=''} 2025-02-18 18:03:59.000 INFO [executor-task-3]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791341821953, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873039000, receivedDispatcherTime=1739873036002, params=''} 2025-02-18 18:04:00.000 INFO [executor-task-4]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791350210561, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873040000, receivedDispatcherTime=1739873037002, params=''} 2025-02-18 18:04:01.000 INFO [executor-task-5]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791358599169, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873041000, receivedDispatcherTime=1739873038002, params=''} 2025-02-18 18:04:02.000 INFO [executor-task-1]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791366987777, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873042000, receivedDispatcherTime=1739873039002, params=''} 2025-02-18 18:04:03.000 INFO [executor-task-2]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791375376385, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873043000, receivedDispatcherTime=1739873040002, params=''} 2025-02-18 18:04:04.000 INFO [executor-task-3]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791383764993, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873044000, receivedDispatcherTime=1739873041002, params=''} 2025-02-18 18:04:05.000 INFO [executor-task-4]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791400542209, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873045000, receivedDispatcherTime=1739873042002, params=''} 2025-02-18 18:04:06.000 INFO [executor-task-5]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791408930817, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873046000, receivedDispatcherTime=1739873043002, params=''} 2025-02-18 18:04:07.000 INFO [executor-task-1]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791417319425, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873047000, receivedDispatcherTime=1739873044002, params=''} 2025-02-18 18:04:08.000 INFO [executor-task-2]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791425708033, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873048000, receivedDispatcherTime=1739873045002, params=''} 2025-02-18 18:04:09.000 INFO [executor-task-3]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791434096641, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873049000, receivedDispatcherTime=1739873046002, params=''} 2025-02-18 18:04:10.000 INFO [executor-task-4]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791434100737, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873050000, receivedDispatcherTime=1739873047002, params=''} 2025-02-18 18:04:11.000 INFO [executor-task-5]c.h.c.e.t.DemoCronTask - task handler..., params:TaskParams{page=1, total=1, taskLogId=6334722791450873857, taskId=6334721498112069633, method='cn.horace.cronjob.examples.tasks.DemoCronTask.handle', exeType=0, cron='* * * * * ?', tag='common', executionTime=1739873051000, receivedDispatcherTime=1739873048002, params=''}
5. 查看调度详情
至此,全部部署完成!微服务应用内接入按照文档说明即可:请参考:快速开始。
👉项目官网:https://cronjob.horace.cn
👉Gitee仓库:https://gitee.com/horacedh/cron-job
👉GitHub仓库:https://github.com/horacedh/cron-job