我使用docker-compose用jboss wildfly 10启动1个容器,用mysql启动另一个容器。 如果mysql容器需要一些时间来启动,在这种情况下,wildfly会出现以下错误:
12:42:35,612 WARN [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (ServerService Thread Pool -- 61) IJ000604: Throwable while attempting to get a new connection: null: javax.resource.ResourceException: IJ031084: Unable to create connection
...
Caused by: java.net.UnknownHostException: db
...
启动了wilfly(有错误):
12:42:40,346 ERROR [org.jboss.as] (Controller Boot Thread) WFLYSRV0026: WildFly Full 10.0.0.Final (WildFly Core 2.0.10.Final) started (with errors) in 14048ms - Started 492 of 818 services (57 services failed or missing dependencies, 383 services are lazy, passive or on-demand)
稍后启动mysql容器时,它不会影响wildfly。
我的docker-compose.yml是:
api:
image: eu.gcr.io/jornaloficial-1265/pt.globaleda.jo-api
environment:
MYSQL_USER: service
MYSQL_PASSWORD: service
MYSQL_DATABASE: api
MYSQL_HOST: db
networks:
- back
db:
image: mysql:5.7.11
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- db-data:/var/lib/mysql
networks:
- back
我的api服务的Dockerfile:
FROM jboss/wildfly:10.0.0.Final
MAINTAINER José Meireles "zmeireles@gmail.com"
ADD mysql-connector-java-5.1.38-bin.jar /opt/jboss/wildfly/modules/system/layers/base/com/mysql/main/
ADD module.xml /opt/jboss/wildfly/modules/system/layers/base/com/mysql/main/
ADD keycloak-wildfly-adapter-dist-1.9.1.Final.tar.gz /opt/jboss/wildfly/
ADD standalone.xml /opt/jboss/wildfly/standalone/configuration/standalone.xml
ADD ato_template.odt /opt/jboss/
RUN mkdir /opt/jboss/pdf-src
RUN mkdir /opt/jboss/pdf-dest
ADD pdf-src /opt/jboss/pdf-src/
CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0"]
ADD api.war /opt/jboss/wildfly/standalone/deployments/
我的standalone.xml中的数据源配置:
<datasource jndi-name="java:/mydb" pool-name="my_pool"
enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://${env.MYSQL_HOST}/${env.MYSQL_DATABASE}?autoReconnect=true&autoReconnectForPools=true&useUnicode=yes&characterEncoding=UTF-8&useSSL=false</connection-url>
<driver>mysql</driver>
<security>
<user-name>${env.MYSQL_USER}</user-name>
<password>${env.MYSQL_PASSWORD}</password>
</security>
<validation>
<background-validation>true</background-validation>
<check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
</validation>
</datasource>
重现问题:
docker-compose down
docker-compose up -d api
sleep 10
docker-compose up -d db
docker-compose logs
Wildfly可以检测数据库何时就绪,或者作为替代方法失败并在数据源子系统退出时退出?在后面的例子中,我可以使用一个在循环中启动wildfly的脚本
答案 0 :(得分:0)
WildFly的数据源配置提供了验证连接的选项。在管理GUI中选择Configuration - &gt;子系统 - &gt;数据源 - &gt;非XA - &gt; ProcessEngine - &gt;视图。从屏幕上的选项卡中选择“验证”。
点击“修改”链接。在“检查有效的SQL”框中,输入以下内容:
SELECT 1
我还检查了“背景验证”框。保存配置(您可能必须先禁用数据源并再次执行此过程和/或您可能必须重新加载服务器。)
此配置执行数据源连接的常量检查。
你可以做的另一件事是在Camunda(我认为)中为其数据源设置超时,但我不能说这是怎么做的。我认为这是非常宽容的。