什么是XXL JOB
-
XXL-JOB是一个分布式任务调度平台;
-
主要分为调度中心管理后台和执行器两部分。
初始化调度数据库
- 使用MySQL作为xxl-job集群的数据库;
- 可以在项目资源中直接使用已有的库,或者自行安装自行采购新的数据库;
- 收集好访问数据库的必要信息
MYSQL_HOST: 数据库IP或访问域名
MYSQL_PORT: 数据库访问端口
MYSQL_DB_NAME: 用于保存nacos集群信息的数据库实例名
MYSQL_USER: 可访问上述数据库实例的用户名
MYSQL_PASSWORD: 可访问上述数据库实例的密码
- 初始化支持xxl-job集群运行必要的表和数据,一定要用自己部署的xxl-job版本的那份SQL脚本去做初始化,这一点非常重要,使用的初始化SQL版本如果不匹配,会导致xxl-job无法正常使用。
// 例如
1. https://github.com/xuxueli/xxl-job/blob/master/doc/db/tables_xxl_job.sql
2. https://github.com/xuxueli/xxl-job/blob/2.3.1/doc/db/tables_xxl_job.sql
部署调度中心管理后台
- 如果是在公有云平台部署xxl-job,一般可以直接通过可视化操作界面选择镜像,一步步创建有状态负载。
- 如果没有可视化操作界面,编写yaml文件,然后通过命令行创建无状态负载。
- 不论使用哪种方式创建,特别注意spec.template.spec.containers.env中配置的环境变量,下面会详细介绍这些环境变量的用途。
- 启动实例后,根据自身所在的网络环境,可以直接访问xxl job admin页面,也可能要配置一些请求转发和代理才能访问。
kind: Deployment
apiVersion: apps/v1
metadata:
name: mojiayi-admin
namespace: mojiayi-prod
spec:
replicas: 1
selector:
matchLabels:
app: xxl-job-admin
version: v1
template:
metadata:
creationTimestamp: null
labels:
app: xxl-job-admin
version: v1
spec:
containers:
- name: container-xxl-job-admin
image: 镜像域名/组织名/xuxueli/xxl-job-admin:2.3.1
ports:
- containerPort: 8080
protocol: TCP
env:
- name: PARAMS
value: |
--spring.datasource.url=jdbc:mysql://173.26.10.89:3306/mojiayi_xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
--spring.datasource.username=mojiayi_xxl_job
--spring.datasource.password=数据库密码
--xxl.job.login.username=admin
--xxl.job.login.password=登录xxl job admin的密码
resources:
limits:
cpu: '2'
memory: 3500Mi
requests:
cpu: 1500m
memory: 3000Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
securityContext: {}
imagePullSecrets:
- name: default-secret
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: xxljob
operator: In
values:
- xxljob
schedulerName: default-scheduler
tolerations:
- key: node.kubernetes.io/not-ready
operator: Exists
effect: NoExecute
tolerationSeconds: 300
- key: node.kubernetes.io/unreachable
operator: Exists
effect: NoExecute
tolerationSeconds: 300
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
环境变量
- 以spring.datasource开头的3个,配置保存调度信息的数据库
- spring.datasource.url 连接数据库的地址、端口和实例名等信息
- spring.datasource.username 连接数据库的用户名
- spring.datasource.password 连接数据库的密码
- 以xxl.job.login开头的2个,是xxl-job后台管理员的初始账号名和登录密码
- xxl.job.login.username 后台管理员的初始账号名
- xxl.job.login.password 后台管理员的初始登录密码
为执行器添加集群内访问服务
- 执行器需要通过集群内的实例名和端口访问调度中心;
- 创建类型为集群内访问ClusterIP的服务,为方便管理,服务端口和容器端口都是8080。
metadata:
name: xxl-job-admin
namespace: mojiayi-prod
labels:
app: xxl-job-admin
version: v1
spec:
ports:
- name: cce-service-0
protocol: TCP
port: 8080
targetPort: 8080
- name: cce-service-1
protocol: TCP
port: 9998
targetPort: 9998
selector:
app: xxl-job-admin
version: v1
clusterIP: 10.247.163.70
clusterIPs:
- 10.237.133.75
type: ClusterIP
sessionAffinity: None
status:
loadBalancer: {}
apiVersion: v1
kind: Service
部署执行器
-
通过可视化操作界面,或yaml文件,创建执行器;
-
特别注意spec.template.spec.args里指定的启动参数。
kind: Deployment
apiVersion: apps/v1
metadata:
name: xxl-job-executor
namespace: mojiayi-prod
labels:
appgroup: ''
version: v1
spec:
replicas: 1
selector:
matchLabels:
app: xxl-job-executor
version: v1
template:
metadata:
creationTimestamp: null
labels:
app: xxl-job-executor
version: v1
spec:
containers:
- name: container-xxl-job-executor
image: 镜像域名/组织名/caryyu/xxl-job-executor-sample-springboot:latest
args:
- '--xxl.job.admin.addresses=http://xxl-job-admin:8080/xxl-job-admin'
env:
- name: PAAS_APP_NAME
value: xxl-job-executor
- name: PAAS_NAMESPACE
value: mojiayi-prod
- name: PAAS_PROJECT_ID
value: c9f15b53a3614c6a9fea9b69d966693e
resources:
limits:
cpu: '2'
memory: 3512Mi
requests:
cpu: 1500m
memory: 3000Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
securityContext: {}
imagePullSecrets:
- name: default-secret
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: xxljob
operator: In
values:
- xxljob
schedulerName: default-scheduler
tolerations:
- key: node.kubernetes.io/not-ready
operator: Exists
effect: NoExecute
tolerationSeconds: 300
- key: node.kubernetes.io/unreachable
operator: Exists
effect: NoExecute
tolerationSeconds: 300
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
在后端项目中配置xxl-job地址
- 因为目前只部署了一个xxl-job实例,实际上没有集群,可以使用以下地址,由调度中心管理后台实例名称:命名空间.平台特有的固定字符:端口/xxl-job-admin组成。
xxl:
job:
accessToken: default_token
admin:
addresses: http://xxl-job-admin.mojiayi-prod.svc.cluster.local:8080/xxl-job-admin
- 如果增加xxl-job实例,有了真正的集群部署,参考Nacos集群部署时的做法,通过Headless Service在集群内提供负载均衡。
重要的题外话
xxl-job应该可以部署成有状态负载,因为它和Nacos一样,属于部署之后不会经常被修改和重启的中间件。