欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker搭建XXLjob

程序员文章站 2022-07-12 20:59:03
...

最近需要使用到调度系统,为了方便管理,直观,所以推荐使用xxljob
官方链接:xxl-job-admin
开始本文的主题,如果根据本文操作,前提是要安装 docker

一 安装mysql(使用yum或者docker安装的都可以,以下使用的是docker)

aaa@qq.com tmp]# docker run -itd --name xxl-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6.40
Unable to find image 'mysql:5.6.40' locally
Trying to pull repository docker.io/library/mysql ...
5.6.40: Pulling from docker.io/library/mysql
be8881be8156: Pull complete
c3995dabd1d7: Pull complete
9931fdda3586: Pull complete
bb1b6b6eff6a: Pull complete
a65f125fa718: Pull complete
62fa8db7a5dc: Pull complete
ea8988c5d951: Pull complete
a14d36079892: Pull complete
ae927d273de4: Pull complete
7cd17abd57c5: Pull complete
adc2c9e19981: Pull complete
Digest: sha256:4902cd80486dca889205576a698a3e6534376ef6d8b94ef9fc9c61e891f1cd15
Status: Downloaded newer image for docker.io/mysql:5.6.40
b3b4acb67fc71f615ab602d43da07e0f99cefd4db114315f3fac714f6f00e32f
[aaa@qq.com tmp]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b3b4acb67fc7        mysql:5.6.40        "docker-entrypoint..."   3 minutes ago       Up 3 minutes        0.0.0.0:3306->3306/tcp   xxl-mysql
[aaa@qq.com tmp]# mkdir xxl-job-1.9.1/doc/db -p
[aaa@qq.com tmp]# cd xxl-job-1.9.1/doc/db/
[aaa@qq.com db]# ll
总用量 0
[aaa@qq.com db]# vim tables_xxl_job.sql
[aaa@qq.com db]# ll
总用量 8
-rw-r--r-- 1 root root 6450 12月 13 16:12 tables_xxl_job.sql
[aaa@qq.com db]# docker cp /tmp/xxl-job-1.9.1/doc/db/tables_xxl_job.sql xxl-mysql:/tmp
[aaa@qq.com db]# docker exec -it xxl-mysql /bin/bash
aaa@qq.com:/# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE `xxl-job` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

mysql> source /tmp/tables_xxl_job.sql
Query OK, 1 row affected (0.01 sec)

Database changed
Query OK, 0 rows affected (0.03 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected, 2 warnings (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected, 1 warning (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

在XXLjob源码中有sql 文件,直接导入到mysql里就可以啦

二 、安装xxlJOB

[aaa@qq.com mnt]# docker pull xuxueli/xxl-job-admin:2.1.2
Trying to pull repository docker.io/xuxueli/xxl-job-admin ...
2.1.2: Pulling from docker.io/xuxueli/xxl-job-admin
795df959b1e6: Pull complete
a2e8defe034f: Pull complete
50d8dad75e60: Pull complete
ad497c735995: Pull complete
40288f52be3a: Pull complete
7b39862ad1f3: Pull complete
2b4a4683cf71: Pull complete
Digest: sha256:c72604c8357dd9fbba823f04878be2a7b8bc77cef1e51e8007ee02ec72d9bb59
Status: Downloaded newer image for docker.io/xuxueli/xxl-job-admin:2.1.2
[aaa@qq.com mnt]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
docker.io/xuxueli/xxl-job-admin   2.1.2               cfe34e8d6785        18 hours ago        227 MB
docker.io/mongo                   latest              965553e202a4        6 weeks ago         363 MB
docker.io/wangyanglinux/myapp     v1                  d4a5e0eaa84f        21 months ago       15.5 MB

三、运行

[aaa@qq.com db]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
82bdd506aa64        cfe34e8d6785        "sh -c 'java -jar ..."   47 seconds ago      Up 47 seconds       0.0.0.0:8080->8080/tcp   xxl-job-admin
[aaa@qq.com db]# docker stop 82bdd506aa64
82bdd506aa64
[aaa@qq.com db]# docker rm 82bdd506aa64
82bdd506aa64
[aaa@qq.com db]# docker run -e PARAMS="--spring.datasource.url=jdbc:mysql://《mysql地址》:3306/xxl_job?Unicode=true&characterEncoding=UTF-8 --spring.datasource.username=《mysql账号》  --spring.datasource.password=《mysql密码》" -p 8080:8080 -v /tmp:/data/applogs --name xxl-job-admin  --privileged=true -d cfe34e8d6785
[aaa@qq.com db]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
ae8ce52568de        cfe34e8d6785        "sh -c 'java -jar ..."   14 seconds ago      Up 12 seconds       0.0.0.0:8080->8080/tcp   xxl-job-admin
[aaa@qq.com db]# docker logs -t -f --tail 200f ae8ce52568de
2019-12-13T08:28:46.018966000Z   .   ____          _            __ _ _
2019-12-13T08:28:46.019267000Z  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
2019-12-13T08:28:46.019546000Z ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2019-12-13T08:28:46.019999000Z  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
2019-12-13T08:28:46.020280000Z   '  |____| .__|_| |_|_| |_\__, | / / / /
2019-12-13T08:28:46.020631000Z  =========|_|==============|___/=/_/_/_/
2019-12-13T08:28:46.034079000Z  :: Spring Boot ::       (v1.5.22.RELEASE)
2019-12-13T08:28:46.497399000Z 16:28:46.490 logback [main] INFO  c.x.job.admin.XxlJobAdminApplication - Starting XxlJobAdminApplication on ae8ce52568de with PID 6 (/app.jar started by root in /)
2019-12-13T08:28:46.502447000Z 16:28:46.497 logback [main] INFO  c.x.job.admin.XxlJobAdminApplication - No active profile set, falling back to default profiles: default
2019-12-13T08:28:47.290910000Z 16:28:47.286 logback [main] INFO  o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Refreshing org.springframework.bootaaa@qq.com41480ec1: startup date [Fri Dec 13 16:28:47 CST 2019]; root of context hierarchy
2019-12-13T08:28:47.355948000Z 16:28:47.350 logback [background-preinit] INFO  o.h.validator.internal.util.Version - HV000001: Hibernate Validator 5.3.6.Final
2019-12-13T08:28:51.315954000Z 16:28:51.314 logback [main] INFO  o.s.b.c.e.t.TomcatEmbeddedServletContainer - Tomcat initialized with port(s): 8080 (http)
2019-12-13T08:28:51.344838000Z 16:28:51.341 logback [main] INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
2019-12-13T08:28:51.389910000Z 16:28:51.384 logback [main] INFO  o.a.catalina.core.StandardService - Starting service [Tomcat]
2019-12-13T08:28:51.390319000Z 16:28:51.385 logback [main] INFO  o.a.catalina.core.StandardEngine - Starting Servlet Engine: Apache Tomcat/8.5.43
2019-12-13T08:28:51.747611000Z 16:28:51.746 logback [localhost-startStop-1] INFO  o.a.c.c.C.[.[.[/xxl-job-admin] - Initializing Spring embedded WebApplicationContext
2019-12-13T08:28:51.748453000Z 16:28:51.746 logback [localhost-startStop-1] INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 4460 ms
2019-12-13T08:28:52.587753000Z 16:28:52.582 logback [localhost-startStop-1] INFO  o.s.b.w.s.ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/]
2019-12-13T08:28:52.600791000Z 16:28:52.598 logback [localhost-startStop-1] INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'metricsFilter' to: [/*]
2019-12-13T08:28:52.601183000Z 16:28:52.598 logback [localhost-startStop-1] INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'characterEncodingFilter' to: [/*]
2019-12-13T08:28:52.601480000Z 16:28:52.599 logback [localhost-startStop-1] INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-12-13T08:28:52.601822000Z 16:28:52.599 logback [localhost-startStop-1] INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-12-13T08:28:52.602906000Z 16:28:52.599 logback [localhost-startStop-1] INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'requestContextFilter' to: [/*]
2019-12-13T08:28:52.603830000Z 16:28:52.599 logback [localhost-startStop-1] INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'webRequestLoggingFilter' to: [/*]
2019-12-13T08:28:52.604046000Z 16:28:52.600 logback [localhost-startStop-1] INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'applicationContextIdFilter' to: [/*]
2019-12-13T08:28:55.514688000Z 16:28:55.511 logback [main] INFO  c.x.j.a.c.scheduler.XxlJobScheduler - >>>>>>>>> init xxl-job admin success.
2019-12-13T08:28:55.610190000Z 16:28:55.607 logback [xxl-job, admin JobRegistryMonitorHelper] WARN  o.a.tomcat.jdbc.pool.ConnectionPool - maxIdle is larger than maxActive, setting maxIdle to: 30
2019-12-13T08:28:57.373683000Z 16:28:57.369 logback [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.bootaaa@qq.com41480ec1: startup date [Fri Dec 13 16:28:47 CST 2019]; root of context hierarchy
2019-12-13T08:28:57.566437000Z 16:28:57.562 logback [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/chartInfo]}" onto public com.xxl.job.core.biz.model.ReturnT<java.util.Map<java.lang.String, java.lang.Object>> com.xxl.job.admin.controller.IndexController.chartInfo(java.util.Date,java.util.Date)

启动成功以后,使用 ip:8080/xxl-job-admin,就可以看到如图
docker搭建XXLjob
以上只是xxljob启动成,如果直接使用执行器,请继续浏览……

一、打开本地项目源码,进行打包(本人使用IDEA)

打包的方式很多,我使用了maven 进行打包如图
docker搭建XXLjob
ps:记得修改执行器中的,日志地址,也要记得在服务器上创建可以读写的日志地址,记得要改配置文件地址,要跟调度器地址一样
打包成功上传到服务器上,某个目录下《本人的目录 /tmp/Dockerfile》
docker搭建XXLjob
在此目录下有两个文件,一个就是刚才打包好的文件,另外一个是dockerfile
dockerfile内容如下

# Pull base image
FROM  java:8

MAINTAINER "aaa@qq.com"

VOLUME /tmp

# 添加
ADD xxl-job-executor-sample-springboot-2.1.2.jar app.jar
RUN bash -c 'touch /app.jar'


# Define default command.
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

#设置时区
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

dockerfile指令:  
Dockerfile由多条指令组成,每条指令在编译镜像时执行相应的程序完成某些功能,由指令+参数组成,以逗号分隔,#作为注释起始符,虽说指令不区分大小写,但是一般指令使用大些,参数使用小写
docker搭建XXLjob
文件都存在以后,使用 docker build -t xxl-job-executor-sample-springboot-2.1.2:v1.0 . 进行创建images 镜像
等到执行成功,就可以使用 docker images 看到刚才执行好的镜像了,然后使用

//启动一个执行器 ,也可以启动多个,只需要改动 端口,和执行器的端口
docker run --privileged=true -d -p 8081:8080 -p 9999:9999 xxl-job-executor-sample-springboot-2.1.2:2.1.2 
docker run --privileged=true -d -p 8082:8080 -p 9998:9999 xxl-job-executor-sample-springboot-2.1.2:2.1.2 

启动是用docker logs 容器名 --tail 100 -f 来查看日志

最后的结果:如图
docker搭建XXLjob
到此就搭建成功,可以使用了。欢迎大家留言指正。
本人也开通了微信公众号,欢迎大家关注,一起交流。
docker搭建XXLjob