1. 前置要求

2. 部署流程

https://github.com/xuxueli/xxl-job

https://github.com/xuxueli/xxl-job/blob/2.1.2/doc/db/tables_xxl_job.sql

2.1 下载镜像

docker pull xuxueli/xxl-job-admin:2.3.1

2.2 初始化SQL

tables_xxl_job.sql

2.3 配置文件

### web
server.port=8080
server.servlet.context-path=/xxl-job-admin
 
### actuator
management.server.servlet.context-path=/actuator
management.health.mail.enabled=false
 
### resources
spring.mvc.servlet.load-on-startup=0
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
#mybatis.type-aliases-package=com.xxl.job.admin.core.model
 
###更改配置文件连接mysql信息
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://172.31.0.100:3306/xxl_job?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false&autoReconnect=true ###ip
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
 
### datasource-pool
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=HikariCP
spring.datasource.hikari.max-lifetime=900000
spring.datasource.hikari.connection-timeout=10000
spring.datasource.hikari.connection-test-query=SELECT 1
spring.datasource.hikari.validation-timeout=1000
 
### xxl-job, email
spring.mail.host=smtp.qq.com
spring.mail.port=25
spring.mail.username=xxx@qq.com
spring.mail.from=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=default_token
 
### xxl-job, i18n (default is zh_CN, and you can choose "zh_CN", "zh_TC" and "en")
xxl.job.i18n=zh_CN
 
## xxl-job, triggerpool max size
xxl.job.triggerpool.fast.max=200
xxl.job.triggerpool.slow.max=100
 
### xxl-job, log retention days
xxl.job.logretentiondays=30

2.3 运行容器

docker run -d --name xxl-job-admin \
  -v /app/xxljob/application.properties:/application.properties \
  -v /data/applogs:/data/applogs \
  -p 8080:8080 \
  -e PARAMS='--spring.config.location=/application.properties' \
  xuxueli/xxl-job-admin:2.3.1

3. 后端代码

自行forck代码,注意要修改代码里面的application.properties中的xxl.job.admin.addresses并且到xxljob中创建执行器job-demo

https://gitee.com/tanqidi/xxljob-demo
spring.application.name=xxljob-demo
server.port=8080

# 调度中心服务部署的地址
xxl.job.admin.addresses=http://xxljob.default.svc.cluster.local:8080/xxl-job-admin
# 执行器通讯TOKEN,要和调度中心服务部署配置的accessToken一致,要不然无法连接注册
xxl.job.accessToken=
# 执行器AppName
xxl.job.executor.appname=job-demo
# 执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。
xxl.job.executor.address=
# 执行器IP
xxl.job.executor.ip=
# 执行器端口号:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
xxl.job.executor.port=9999
# 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
xxl.job.executor.logpath=
# 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
xxl.job.executor.logretentiondays=30

3.1 kubesphere devops

pipeline {
  agent {
    node {
      label 'jdk21-maven3.9.9'
    }

  }
  stages {
    stage('编译信息') {
      agent none
      steps {
        script {
          env.FULL_VERSION_NUMBER="${params.INPUT_VERSION_NUMBER}"
        }

        sh 'echo "当前分支:${FULL_VERSION_NUMBER}"'
      }
    }

    stage('拉取项目') {
      agent none
      steps {
        container('maven') {
          git(credentialsId: 'gitee-secret', branch: FULL_VERSION_NUMBER, url: 'https://gitee.com/tanqidi/xxljob-demo.git', changelog: true, poll: false)
        }

      }
    }

    stage('项目编译') {
      agent none
      steps {
        container('maven') {
          sh 'mvn -Dmaven.test.skip=true clean package'
        }

      }
    }

    stage('打包镜像') {
      agent none
      steps {
        container('maven') {
          sh 'docker build -f Dockerfile -t ${REGISTRY}/${HARBOR_NAMESPACE}/${PROJECT_NAME}:${FULL_VERSION_NUMBER}.${BUILD_NUMBER} .'
        }

      }
    }

    stage('镜像推送') {
      agent none
      steps {
        container('maven') {
          withCredentials([usernamePassword(credentialsId : 'aliyun-harbor-secret' ,passwordVariable : 'name' ,usernameVariable : 'passwd' ,)]) {
            sh 'echo "$name" | docker login $REGISTRY -u "$passwd" --password-stdin'
            sh 'docker push ${REGISTRY}/${HARBOR_NAMESPACE}/${PROJECT_NAME}:${FULL_VERSION_NUMBER}.${BUILD_NUMBER}'
          }

        }

      }
    }

    stage('部署到UAT环境') {
      agent none
      steps {
        container('maven') {
          withCredentials([kubeconfigFile(credentialsId: "$KUBECONFIG_CREDENTIAL_ID", variable: 'KUBECONFIG')]) {
            script {
              sh "echo \'${DEPLOY_YAML}\' > deploy.yaml"
              sh "cat deploy.yaml"
              sh "envsubst < deploy.yaml | kubectl apply -f -"
            }

          }

        }

      }
    }

  }
  environment {
    KUBECONFIG_CREDENTIAL_ID = 'k8s-kubeconfig'
    REGISTRY = 'registry.cn-hangzhou.aliyuncs.com'
    HARBOR_NAMESPACE = 'tanqidi-temp'
    PROJECT_NAME = 'xxljob-demo'
    PROJECT_PORT = '8080'
    PROJECT_NAMESPACE = 'default'
    DEPLOY_YAML = '''

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: ${PROJECT_NAME}
  name: ${PROJECT_NAME}
  namespace: ${PROJECT_NAMESPACE}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ${PROJECT_NAME}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600

  template:
    metadata:
      labels:
        app: ${PROJECT_NAME}
    spec:
      imagePullSecrets:
        - name: harbor
      containers:
        - name: ${PROJECT_NAME}
          imagePullPolicy: Always
          image: ${REGISTRY}/${HARBOR_NAMESPACE}/${PROJECT_NAME}:${FULL_VERSION_NUMBER}.${BUILD_NUMBER}
          ports:
            - containerPort: ${PROJECT_PORT}
              protocol: TCP
          resources:
            limits:
              cpu: \'1\'
              memory: 2Gi
            requests:
              cpu: 25m
              memory: 850Mi
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  name: ${PROJECT_NAME}
  namespace: ${PROJECT_NAMESPACE}
  labels:
    app: ${PROJECT_NAME}
spec:
  ports:
    - port: ${PROJECT_PORT}
      targetPort: ${PROJECT_PORT}
  selector:
    app: ${PROJECT_NAME}
  type: ClusterIP
  sessionAffinity: None

'''
  }
  parameters {
    string(name: 'INPUT_VERSION_NUMBER', defaultValue: '', description: '请输入分支的版本,如:release-1.3.20220531')
  }
}

image-qwks.png

3.2 xxl-job 配置

根据application.properties中的 xxl.job.executor.appname=job-demo 创建出执行器,并且为自动注册

image-qelm.png
任务管理选择为job-demo,新增一个名为demoJobHandler的任务,cron随便填,可以是:*/1 * * * * ?

image-qcwl.png

跑完流水线启动完程序不出意外可以看到顺利自动注册上来了。

image-jduv.png

到任务管理中,手动执行一次demoJobHandler任务,可以看到一切都按照预期完成了工作。

image-pzgd.png

image-lwjb.png

4. 版本升级

XXL-JOB 从 2.1.2 升级到 2.3.1 的 SQL 字段补全内容

ALTER TABLE `xxl-job`.`xxl_job_info` ADD COLUMN `schedule_type` varchar(50)  NOT NULL DEFAULT 'NONE' COMMENT '调度类型' AFTER `alarm_email`;
ALTER TABLE `xxl-job`.`xxl_job_info` ADD COLUMN `schedule_conf` varchar(128)  NULL DEFAULT NULL COMMENT '调度配置,值含义取决于调度类型' AFTER `schedule_type`;
ALTER TABLE `xxl-job`.`xxl_job_info` ADD COLUMN `misfire_strategy` varchar(50)  NOT NULL DEFAULT 'DO_NOTHING' COMMENT '调度过期策略' AFTER `schedule_conf`;
ALTER TABLE `xxl-job`.`xxl_job_log_report` ADD COLUMN `update_time` datetime NULL DEFAULT NULL AFTER `fail_count`;
ALTER TABLE `xxl-job`.`xxl_job_info` MODIFY COLUMN `job_cron` varchar(128)  NOT NULL DEFAULT '' COMMENT '任务执行CRON';

5. kubernetes

5.1 configmap

kind: ConfigMap
apiVersion: v1
metadata:
  name: xxljob-jdk21-config
  namespace: default
  annotations:
    kubesphere.io/creator: tanqidi
data:
  application.properties: >-
    ### web
    server.port=8080
    server.servlet.context-path=/xxl-job-admin
     
    ### actuator
    management.server.servlet.context-path=/actuator
    management.health.mail.enabled=false
     
    ### resources
    spring.mvc.servlet.load-on-startup=0
    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
    #mybatis.type-aliases-package=com.xxl.job.admin.core.model
     
    ###更改配置文件连接mysql信息
    ### xxl-job, datasource
    spring.datasource.url=jdbc:mysql://172.31.0.100:3306/xxl_job:3306/xxl-job?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false&autoReconnect=true
    ###ip
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
     
    ### datasource-pool
    spring.datasource.type=com.zaxxer.hikari.HikariDataSource
    spring.datasource.hikari.minimum-idle=10
    spring.datasource.hikari.maximum-pool-size=30
    spring.datasource.hikari.auto-commit=true
    spring.datasource.hikari.idle-timeout=30000
    spring.datasource.hikari.pool-name=HikariCP
    spring.datasource.hikari.max-lifetime=900000
    spring.datasource.hikari.connection-timeout=10000
    spring.datasource.hikari.connection-test-query=SELECT 1
    spring.datasource.hikari.validation-timeout=1000
     
    ### xxl-job, email
    spring.mail.host=smtp.qq.com
    spring.mail.port=25
    spring.mail.username=xxx@qq.com
    spring.mail.from=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=
     
    ### xxl-job, i18n (default is zh_CN, and you can choose "zh_CN", "zh_TC" and
    "en")
    xxl.job.i18n=zh_CN
     
    ## xxl-job, triggerpool max size
    xxl.job.triggerpool.fast.max=200
    xxl.job.triggerpool.slow.max=100
     
    ### xxl-job, log retention days
    xxl.job.logretentiondays=30

5.2 service

以nodeport方式试验访问,路径是:http://xxx.xxx.xxx.xxx:30990/xxl-job-admin/toLogin

kind: Service
apiVersion: v1
metadata:
  name: xxljob
  namespace: default
  labels:
    app: xxljob
  annotations:
    kubesphere.io/creator: admin
spec:
  ports:
    - name: http-8080
      protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 30990
  selector:
    app: xxljob
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  internalTrafficPolicy: Cluster

5.3 deployment

kind: Deployment
apiVersion: apps/v1
metadata:
  name: xxljob
  namespace: default
  annotations:
    deployment.kubernetes.io/revision: '3'
    kubesphere.io/creator: tanqidi
spec:
  replicas: 1
  selector:
    matchLabels:
      app: xxljob
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: xxljob
      annotations:
        kubesphere.io/restartedAt: '2025-03-21T08:32:56.000Z'
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
        - name: conf
          configMap:
            name: xxljob-jdk21-config
            defaultMode: 420
      containers:
        - name: xxljob
          image: 'xuxueli/xxl-job-admin:2.3.1'
          command:
            - /bin/sh
            - '-c'
            - >
              java -server -Xms4096M -Xmx4096M  -XX:+PrintGC -XX:+PrintGCDetails
              -XX:+PrintGCTimeStamps -Xloggc:/data/jvm.log
              -XX:+HeapDumpOnOutOfMemoryError
              -XX:HeapDumpPath=/data/heapdump.bin -Xdebug
              -Xrunjdwp:transport=dt_socket,server=y,suspend=n 
              -Djava.security.egd=file:/dev/./urandom -jar /app.jar
              --spring.config.location=/app/application.properties
          ports:
            - name: client
              containerPort: 8080
              protocol: TCP
          env:
            - name: LANG
              value: en_US.UTF-8
          resources:
            limits:
              cpu: '4'
              memory: 6Gi
            requests:
              cpu: 200m
              memory: 4Gi
          volumeMounts:
            - name: host-time
              readOnly: true
              mountPath: /etc/localtime
            - name: conf
              mountPath: /app/application.properties
              subPath: application.properties
          livenessProbe:
            httpGet:
              path: /xxl-job-admin/actuator/health
              port: 8080
              scheme: HTTP
            timeoutSeconds: 2
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 6
          readinessProbe:
            httpGet:
              path: /xxl-job-admin/actuator/health
              port: 8080
              scheme: HTTP
            timeoutSeconds: 2
            periodSeconds: 5
            successThreshold: 1
            failureThreshold: 3
          startupProbe:
            httpGet:
              path: /xxl-job-admin/actuator/health
              port: 8080
              scheme: HTTP
            initialDelaySeconds: 100
            timeoutSeconds: 2
            periodSeconds: 5
            successThreshold: 1
            failureThreshold: 40
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      securityContext: {}
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600

5.4 ingress

kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
  name: xxljob-dashborad
  namespace: default
  annotations:
    kubernetes.io/ingress.class: nginx
    kubesphere.io/alias-name: xxljob管理
    kubesphere.io/creator: admin
    nginx.ingress.kubernetes.io/proxy-redirect-from: 'http://'
    nginx.ingress.kubernetes.io/proxy-redirect-to: '$scheme://'
    nginx.ingress.kubernetes.io/server-snippet: 'rewrite ^/$ http://xxljob.uat.tanqidi.com/xxl-job-admin permanent;'
    nginx.ingress.kubernetes.io/upstream-hash-by: $binary_remote_addr
spec:
  tls:
    - hosts:
        - xxljob.uat.tanqidi.com
      secretName: tanqidi-uat-ssl
  rules:
    - host: xxljob.uat.tanqidi.com
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service:
                name: xxljob
                port:
                  number: 8080

5. 常见异常

能顺利启动xxljob,但是服务却一直无法注册,响应的错误内容是一大串html内容 response content invalid(<!DOCTYPE html>...... 你必须要认真检查一下application.properties的配置项,以下写法会导致此错误

正确:server.servlet.context-path =/xxl-job-admin

错误:server.context-path = /xxl-job-admin

57cff3f6-ed4b-43d0-9568-20301c6ac0a9.jpeg

6. arm64镜像构建

本次构建2个版本分别为2.1.2与2.3.1,如果想要构建其他版本欢迎留言补充。

2.1.2 Dockerfile

自行编译 xxl-job-admin-xxx.jar https://github.com/xuxueli/xxl-job

docker buildx build --push --platform linux/arm64 -t tanqidi/xxl-job-admin:2.1.2 .

docker buildx build --push --platform linux/arm64 -t tanqidi/xxl-job-admin:2.3.1 .

FROM openjdk:8-jre-slim

ENV PARAMS=""

ENV TZ=PRC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

ADD target/xxl-job-admin-*.jar /app.jar

ENTRYPOINT ["sh","-c","java -jar /app.jar $PARAMS"]

6.x 镜像成品

  • tanqidi/xxl-job-admin:2.1.2

  • tanqidi/xxl-job-admin:2.3.1

写在最后