今天想把xxl-job-admin跑在容器中,然后在部署的过程中踩了一些坑,特此记录
因为xxl-job-admin需要连接数据库,而我本机的mysql是部署在docker容器中,我以为只要将xxl-job-admin按正常容器化部署就可以访问了,结果在实施过程中遇到了一些坑,如果你在其中一个容器访问另一个容器中的mysql遇到一些报错的情况,你可以参考我这篇文章,看有没有参考性
如果对mysql容器化部署还不熟的同学请先看我这篇文章
我部署在容器中的mysql访问地址为:localhost:3306
首先mysql服务访问正常,通过客户端可以正常访问
首先参照官方文档进行部署,Docker 镜像方式搭建调度中心
大致步骤如下(官网截图):
直接使用下面带参数的形式启动,然后访问http://localhost:8080/xxl-job-admin
,发现不通
docker run -e PARAMS="--spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai" -p 8080:8080 -v /tmp:/data/applogs --name xxl-job-admin -d xuxueli/xxl-job-admin:2.3.0
查看一下日志,发现有报错
docker ps -l : 查询出容器id
docker logs --since 2m fba1cd734f5a 查看容器最近2分钟内的运行日志
yinchendong@Yinchds-Macbook-Pro ~ % docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fba1cd734f5a xuxueli/xxl-job-admin:2.3.0 "sh -c 'java -jar $J…" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp xxl-job-admin
yinchendong@Yinchds-Macbook-Pro ~ % docker logs -t -f --tail 200f fba1cd734f5a
2021-10-25T08:05:09.509249100Z
2021-10-25T08:05:09.509300100Z . ____ _ __ _ _
2021-10-25T08:05:09.509342600Z /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2021-10-25T08:05:09.509599800Z ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2021-10-25T08:05:09.509657700Z \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2021-10-25T08:05:09.509795800Z ' |____| .__|_| |_|_| |_\__, | / / / /
2021-10-25T08:05:09.509918600Z =========|_|==============|___/=/_/_/_/
2021-10-25T08:05:09.512214900Z :: Spring Boot :: (v2.4.2)
2021-10-25T08:05:09.512261400Z
2021-10-25T08:05:09.628521000Z 16:05:09.657 logback [main] INFO c.x.job.admin.XxlJobAdminApplication - Starting XxlJobAdminApplication using Java 1.8.0_265 on fba1cd734f5a with PID 8 (/app.jar started by root in /)
2021-10-25T08:05:09.631424600Z 16:05:09.631 logback [main] INFO c.x.job.admin.XxlJobAdminApplication - No active profile set, falling back to default profiles: default
2021-10-25T08:05:12.241708000Z 16:05:12.241 logback [main] INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
2021-10-25T08:05:12.256515600Z 16:05:12.256 logback [main] INFO o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
2021-10-25T08:05:12.258135100Z 16:05:12.257 logback [main] INFO o.a.catalina.core.StandardService - Starting service [Tomcat]
2021-10-25T08:05:12.259091900Z 16:05:12.258 logback [main] INFO o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-10-25T08:05:12.331648600Z 16:05:12.331 logback [main] INFO o.a.c.c.C.[.[.[/xxl-job-admin] - Initializing Spring embedded WebApplicationContext
2021-10-25T08:05:12.332883700Z 16:05:12.332 logback [main] INFO o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 2293 ms
2021-10-25T08:05:13.348661900Z 16:05:13.348 logback [main] INFO c.x.j.a.c.scheduler.XxlJobScheduler - >>>>>>>>> init xxl-job admin success.
2021-10-25T08:05:13.398194100Z 16:05:13.396 logback [xxl-job, admin JobLogReportHelper] INFO com.zaxxer.hikari.HikariDataSource - HikariCP - Starting...
2021-10-25T08:05:13.610714600Z 16:05:13.610 logback [main] INFO o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
2021-10-25T08:05:13.778379100Z 16:05:13.777 logback [main] INFO o.s.b.a.w.s.WelcomePageHandlerMapping - Adding welcome page template: index
2021-10-25T08:05:14.375184800Z 16:05:14.374 logback [main] INFO o.s.b.a.e.web.EndpointLinksResolver - Exposing 2 endpoint(s) beneath base path '/actuator'
2021-10-25T08:05:14.405676900Z 16:05:14.405 logback [main] INFO o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
2021-10-25T08:05:14.432612700Z 16:05:14.432 logback [main] INFO o.a.c.c.C.[.[.[/xxl-job-admin] - Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-25T08:05:14.433908800Z 16:05:14.433 logback [main] INFO o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
2021-10-25T08:05:14.435904600Z 16:05:14.435 logback [main] INFO o.s.web.servlet.DispatcherServlet - Completed initialization in 1 ms
2021-10-25T08:05:14.438400400Z 16:05:14.438 logback [main] INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path '/xxl-job-admin'
2021-10-25T08:05:14.459731400Z 16:05:14.459 logback [main] INFO c.x.job.admin.XxlJobAdminApplication - Started XxlJobAdminApplication in 5.47 seconds (JVM running for 6.52)
2021-10-25T08:05:18.002457200Z 16:05:18.001 logback [xxl-job, admin JobScheduleHelper#scheduleThread] INFO c.x.j.a.c.thread.JobScheduleHelper - >>>>>>>>> init xxl-job admin scheduler success.
2021-10-25T08:05:18.595737100Z 16:05:18.594 logback [xxl-job, admin JobLogReportHelper] ERROR com.zaxxer.hikari.pool.HikariPool - HikariCP - Exception during pool initialization.
2021-10-25T08:05:18.595831700Z java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
2021-10-25T08:05:18.595860100Z at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
2021-10-25T08:05:18.595884600Z at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
2021-10-25T08:05:18.596418100Z at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
2021-10-25T08:05:18.596463800Z at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
2021-10-25T08:05:18.596508900Z at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
2021-10-25T08:05:18.596530700Z at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:903)
2021-10-25T08:05:18.596560700Z at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
发现报java.sql.SQLNonTransientConnectionException: Could not create connection to database server
这个异常,于是检查,因为我的mysql配置的有密码,这里没有写密码,所以先停止容器并删除,然后加上账号密码的配置再试试
docker stop fba1cd734f5a:停止容器
docker rm fba1cd734f5a:移除容器
yinchendong@Yinchds-Macbook-Pro ~ % docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fba1cd734f5a xuxueli/xxl-job-admin:2.3.0 "sh -c 'java -jar $J…" 8 minutes ago Up 8 minutes 0.0.0.0:8080->8080/tcp xxl-job-admin
yinchendong@Yinchds-Macbook-Pro ~ % docker stop fba1cd734f5a
fba1cd734f5a
yinchendong@Yinchds-Macbook-Pro ~ % docker rm fba1cd734f5a
fba1cd734f5a
容器移除后,然后加上账号密码的配置再执行
docker run -e PARAMS="--spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=root --spring.datasource.password=password" -p 8080:8080 -v /tmp:/data/applogs --name xxl-job-admin -d xuxueli/xxl-job-admin:2.3.0
新启动了容器,结果发现还是无法访问,重复上面的步骤查看日志,发现报如下错误
yinchendong@Yinchds-Macbook-Pro ~ % docker logs --since 2m 4f674f04293f
2021-10-25T09:03:09.791331000Z at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
2021-10-25T09:03:09.791354600Z at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:847)
2021-10-25T09:03:09.791374900Z ... 39 common frames omitted
2021-10-25T09:03:09.791397200Z Caused by: java.net.ConnectException: Connection refused (Connection refused)
2021-10-25T09:03:09.791420700Z at java.net.PlainSocketImpl.socketConnect(Native Method)
2021-10-25T09:03:09.791444100Z at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
2021-10-25T09:03:09.791467600Z at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
2021-10-25T09:03:09.791491200Z at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
2021-10-25T09:03:09.791509500Z at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
2021-10-25T09:03:09.791532600Z at java.net.Socket.connect(Socket.java:607)
2021-10-25T09:03:09.791556200Z at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
2021-10-25T09:03:09.791579800Z at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
2021-10-25T09:03:09.791603700Z ... 41 common frames omitted
2021-10-25T09:03:09.792529000Z 17:03:09.792 logback [xxl-job, admin JobScheduleHelper#scheduleThread] INFO com.zaxxer.hikari.HikariDataSource - HikariCP - Starting...
2021-10-25T09:03:09.797454400Z 17:03:09.796 logback [xxl-job, admin JobLogReportHelper] ERROR c.x.j.a.c.thread.JobLogReportHelper - >>>>>>>>>>> xxl-job, job log report thread error:{}
2021-10-25T09:03:09.797485200Z org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
2021-10-25T09:03:09.797508900Z ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
2021-10-25T09:03:09.797534300Z ### The error may exist in class path resource [mybatis-mapper/XxlJobLogMapper.xml]
2021-10-25T09:03:09.797556500Z ### The error may involve com.xxl.job.admin.dao.XxlJobLogDao.findLogReport
2021-10-25T09:03:09.797580400Z ### The error occurred while executing a query
2021-10-25T09:03:09.797604200Z ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
2021-10-25T09:03:09.797626800Z at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
2021-10-25T09:03:09.797649100Z at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
2021-10-25T09:03:09.797672600Z at com.sun.proxy.$Proxy70.selectOne(Unknown Source)
2021-10-25T09:03:09.797696200Z at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:160)
2021-10-25T09:03:09.797720000Z at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:87)
2021-10-25T09:03:09.797741900Z at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152)
2021-10-25T09:03:09.797758800Z at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
2021-10-25T09:03:09.797780800Z at com.sun.proxy.$Proxy71.findLogReport(Unknown Source)
2021-10-25T09:03:09.797803100Z at com.xxl.job.admin.core.thread.JobLogReportHelper$1.run(JobLogReportHelper.java:71)
2021-10-25T09:03:09.797827400Z at java.lang.Thread.run(Thread.java:748)
可以发现报拒绝连接的异常,但是账号密码链接什么的配的都是正常的,客户端能正常连接
于是百度了解问题,看到其中有个哥们说两个容器之间需要用link来操作,于是我试了下,发现确实可行,下面是解决方案
1、还是先停止并删除刚有问题的容器
2、重新启动容器,加上link,代码如下
docker run -e PARAMS="--spring.datasource.url=jdbc:mysql://mysql:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=root --spring.datasource.password=password" -p 8070:8080 -v /tmp:/data/applogs --name xxl-job-admin -d --link mysql:mysql xuxueli/xxl-job-admin:2.3.0
这个有两个地方需要注意:
1、–link mysql:mysql 这个是代表链接的mysql容器,前面的mysql指的是mysql容器的名称(–name=mysql),后面为指定mysql容器在link下的别名
--link
:alias
其中,name和id是源容器的name和id,alias是源容器在link下的别名。
关于link的用法和含义,可以参考下这篇文章:https://www.jianshu.com/p/21d66ca6115e
2、spring.datasource.url
由jdbc:mysql://127.0.0.1:3306
换成了jdbc:mysql://mysql:3306
,其中jdbc:mysql://
后面跟的这个mysql
是mysql容器在此容器映射的别名,通过这个别名就可以找到mysql容器
至此我们再试一下,发现ok了
在此进行记录,希望对大家有所帮助