php项目接入xxl-job调度系统的示例详解

来自:网络
时间:2022-12-25
阅读:
目录

1. 部署xxl-job调度中心

此处略,请自行百度。下面重点介绍如何将php项目接入xxl-job调度。

2. 整合xxl-job调度系统

核心是使用xxl-job的GLUE运行模式,通过一段php代码片段,调用远程的http资源。

2.1 创建执行器项目

参考执行器示例项目, xxl-job-executor-samples/xxl-job-executor-sample-springboot,修改下其中的 application.properties 文件,内容如下:

# 执行器项目使用的端口号
server.port=8585
# no web
#spring.main.web-environment=false
 
# log config
logging.config=classpath:logback.xml
 
### 调度中心地址
xxl.job.admin.addresses=http://127.0.0.1:8081/job-admin
 
### xxl-job, access token
xxl.job.accessToken=
 
### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-base
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=127.0.0.1
xxl.job.executor.port=9999
### xxl-job executor log-path
xxl.job.executor.logpath=xxl-job-log
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30

2.2 新增执行器

在调度中心的管理界面,新增一个执行器,执行器的相关信息必须与【2.1】步骤中的配置保持一致,如下:

php项目接入xxl-job调度系统的示例详解

 注意:此处的机器地址,在新版中必须带上协议名称,如:http。

 2.3 部署执行器项目

将编译好的 xxl-job-executor-sample-springboot-2.2.1-SNAPSHOT.jar 包,复制到php项目所在的机器上,并启动。启动命令为:

java -jar -Dfile.encoding=utf-8 "xxl-job-executor-sample-springboot-2.2.1-SNAPSHOT.jar"

2.4 新增GLUE模式任务

php项目接入xxl-job调度系统的示例详解

 执行器选择【2.2】步骤中新建的执行器即可,运行模式选择【GLUE(PHP)】。

2.5 编写php代码片段

在任务列表中,找到之前新建的GLUE任务,然后在对应的操作栏中,选择【GLUE IDE】菜单,进入xxl-job内置的web编辑器,即可编写与业务相关的php代码。

php项目接入xxl-job调度系统的示例详解

 示例内容如下:

<?php
    $url = 'https://www.baidu.com/';
	$result = file_get_contents($url);
	var_dump($result);
	exit(0);
?>

php项目接入xxl-job调度系统的示例详解

 注意:

① 脚本任务通过 Exit Code 判断任务执行结果,0 成功,-1(非0状态码)失败。

② 调度过程中,将会在 gluesource 目录(位于日志根目录下)下,生成一个临时的php文件,文件内容即为步骤【2.5】中编写的代码片段。

返回顶部
顶部