PayaraMicro 未在来自 WAR 的 glassfish-resources.xml 中添加 jdbc 详细信息

PayaraMicro is not adding jdbc details in glassfish-resources.xml from WAR

我有 Web 应用程序,我正尝试在 Payara Micro 的 docker 容器中部署,但 glassfish-resources.xml 详细信息未添加到服务器的 microdomain.xml

JDBC 定义非常基本,利用了从 docker 命令设置的环境变量(这是 MyApp.war/WEB-INF/glassfish-resources.xml 中的位置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>

    <jdbc-resource pool-name="MyAppDB" jndi-name="jdbc/MyAppDB" enabled="true" ></jdbc-resource>


    <jdbc-connection-pool datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerDataSource" 
                          name="MyAppDB" 
                          res-type="javax.sql.DataSource">
      <property name="User" value="${ENV=MY_APP_DATABASE_USER}"></property>
      <property name="Password" value="${ENV=MY_APP_DATABASE_PASS}"></property>
      <property name="DatabaseName" value="${ENV=MY_APP_DATABASE_NAME}"></property>
      <property name="ServerName" value="${ENV=MY_APP_DATABASE_SERVER}"></property>
      <property name="PortNumber" value="1433"></property>
    </jdbc-connection-pool>    

</resources>

这是 payara.env 文件:

MY_APP_DATABASE_USER=someuser
MY_APP_DATABASE_PASS=somepass
MY_APP_DATABASE_NAME=MY_APP_DB
MY_APP_DATABASE_SERVER=192.168.10.110

正如您从下面的日志中看到的,mysql-jdbc-7.0.0.jre8.jar 确实被添加到类路径中。

Docker 命令:

docker run -p 8080:8080 \
           --mount type=bind,src=$(pwd)/deployments,dst=/opt/payara/deployments \
           --mount type=bind,src=$(pwd)/lib,dst=/opt/payara/lib \
           --env-file payara.env \
           --restart=always \
           payara/micro:5.191 \
           --addlibs /opt/payara/lib \
           --deploy /opt/payara/deployments/my-app.war \
           --contextroot ROOT

服务器输出(删除了多余的日志记录):

[2019-05-29T16:41:46.990+0000] [] [WARNING] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148106990] [levelValue: 900] Payara Micro Runtime directory is located in a temporary file location which can be cleaned by system processes.

[2019-05-29T16:41:47.034+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148107034] [levelValue: 800] Payara Micro Runtime directory is located at /tmp/payaramicro-rt5934725251203193927tmp

[2019-05-29T16:41:47.061+0000] [] [INFO] [] [fish.payara.micro.boot.runtime.PayaraMicroRuntimeBuilder] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148107061] [levelValue: 800] Built Payara Micro Runtime

[2019-05-29T16:41:48.197+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148108197] [levelValue: 800] Added /opt/payara/lib/mssql-jdbc-7.0.0.jre8.jar to classpath

[2019-05-29T16:41:50.422+0000] [] [INFO] [NCLS-CORE-00101] [javax.enterprise.system.core] [tid: _ThreadID=19 _ThreadName=RunLevelControllerThread-1559148109281] [timeMillis: 1559148110422] [levelValue: 800] Network Listener http-listener started in: 24ms - bound to [/0.0.0.0:8080]

[2019-05-29T16:41:50.572+0000] [] [INFO] [NCLS-CORE-00017] [javax.enterprise.system.core] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148110572] [levelValue: 800] Payara Micro  5.191 #badassmicrofish (94) startup time : Embedded (2,200ms), startup services(1,306ms), total(3,506ms)

[2019-05-29T16:41:53.520+0000] [] [INFO] [AS-WEB-GLUE-00198] [javax.enterprise.web] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148113520] [levelValue: 800] Created HTTP listener http-listener on host/port 0.0.0.0:8080

[2019-05-29T16:41:53.587+0000] [] [INFO] [AS-WEB-GLUE-00200] [javax.enterprise.web] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148113587] [levelValue: 800] Created virtual server server

[2019-05-29T16:41:53.957+0000] [] [INFO] [AS-WEB-CORE-00306] [javax.enterprise.web.core] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148113957] [levelValue: 800] Setting JAAS app name glassfish-web

[2019-05-29T16:41:53.957+0000] [] [INFO] [AS-WEB-GLUE-00201] [javax.enterprise.web] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148113957] [levelValue: 800] Virtual server server loaded default web module 

[2019-05-29T16:41:57.129+0000] [] [SEVERE] [] [javax.enterprise.system.tools.deployment.dol] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148117129] [levelValue: 1000] AS-DEPLOYMENT-00026

[2019-05-29T16:41:57.135+0000] [] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148117135] [levelValue: 1000] Exception while deploying the app [my-app]

[2019-05-29T16:41:57.135+0000] [] [SEVERE] [NCLS-CORE-00026] [javax.enterprise.system.core] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148117135] [levelValue: 1000] [[
  Exception during lifecycle processing
org.glassfish.deployment.common.DeploymentException: JNDI lookup failed for the resource: Name: [MyAppPU], Lookup: [jdbc/MyAppDB], Type: [javax.sql.DataSource]
    at com.sun.enterprise.deployment.util.ResourceValidator.validateJNDIRefs(ResourceValidator.java:896)

Caused by: javax.naming.NamingException: Lookup failed for 'jdbc/MyAppDB' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NameNotFoundException: MyAppDB not found]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:496)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:442)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at com.sun.enterprise.deployment.util.ResourceValidator.validateJNDIRefs(ResourceValidator.java:891)
    ... 32 more
Caused by: javax.naming.NameNotFoundException: MyAppDB not found
    at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:237)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:204)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:208)
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:70)
    at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:114)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:483)
    ... 36 more
]]

[2019-05-29T16:41:57.284+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148117284] [levelValue: 800] Deployed 1 archive(s)

[2019-05-29T16:42:01.387+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148121387] [levelValue: 800] [[

Payara Micro URLs:

]]

[2019-05-29T16:42:01.388+0000] [] [INFO] [] [PayaraMicro] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559148121388] [levelValue: 800] Payara Micro  5.191 #badassmicrofish (build 94) ready in 14,400 (ms)

更新:

我添加了预引导脚本来设置系统标志 deployment.resource.validation,如 payara/Payara/issues/3449

中所述

preboot.asadmin:

create-system-properties deployment.resource.validation=false

启动日志显示命令执行成功,但从未添加资源:

[2019-06-03T15:04:58.993+0000] [] [INFO] [] [fish.payara.boot.runtime.BootCommand] [tid: _ThreadID=1 _ThreadName=main] [timeMillis: 1559574298993] [levelValue: 800] Boot Command create-system-properties returned with result SUCCESS : PlainTextActionReporterSUCCESSNo monitoring data to report.

我也改为使用 payara-resources.xml 和以下 DOCTYPE:

<!DOCTYPE resources PUBLIC "-//Payara.fish//DTD Payara Server 4 Resource Definitions//EN" "https://raw.githubusercontent.com/payara/Payara-Server-Documentation/master/schemas/payara-resources_1_6.dtd">

我终于搞定了。首先,我正在部署的应用程序是一个 WAR,带有打包的 EJB 模块,因此 persistence.xml 需要打包在 WAR/WEB-INF/lib 中的一个 jar 中。这是结构:

WAR/WEB-INF/lib/ejb1.jar
WAR/WEB-INF/lib/ejb2.jar
WAR/WEB-INF/lib/persistence.jar
WAR/WEB-INF/lib/persistence.jar/META-INF/persistence.xml
WAR/WEB-INF/payara-resources.xml

请务必注意,payara-resources.xml 中的资源定义使用了环境变量。有人建议我将定义放在 WAR/WEB-INF/web.xml 中,但我更喜欢使用 resources.xml 获得的控制权,而且我不确定 ENV 变量交换是否有效.

WAR/WEB-INF/payara-resources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Payara.fish//DTD Payara Server 4 Resource Definitions//EN" "https://raw.githubusercontent.com/payara/Payara-Server-Documentation/master/schemas/payara-resources_1_6.dtd">
<resources>
    <jdbc-resource pool-name="MyAppDB" jndi-name="java:app/jdbc/MyApp" enabled="true" ></jdbc-resource>

    <jdbc-connection-pool datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerDataSource" 
                          name="MyAppDB" 
                          res-type="javax.sql.DataSource">
      <property name="User" value="${ENV=MY_APP_DATABASE_USER}"></property>
      <property name="Password" value="${ENV=MY_APP_DATABASE_PASS}"></property>
      <property name="DatabaseName" value="${ENV=MY_APP_DATABASE_NAME}"></property>
      <property name="ServerName" value="${ENV=MY_APP_DATABASE_SERVER}"></property>
      <property name="PortNumber" value="1433"></property>
    </jdbc-connection-pool>  
</resources>

这是坚持:

WAR/WEB-INF/lib/persistence.jar/META-INF/persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="MyAppPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:app/jdbc/MyApp</jta-data-source>
    <!-- other stuff //-->
  </persistence-unit>
</persistence>

persistence.xml 使用 jar 的好处之一是可以跨项目(服务)共享持久性定义。不幸的是,Micro 请求 JNDI 名称的 java:app/ 前缀,而部署到完整服务器的项目不使用它(我还没有在完整服务器部署中使用 java:app/ 进行测试)。无论如何,上面的工作。

很好的问答。此外,不仅 Micro 请求 JNDI 名称的 java:app/ 前缀,而且完整的服务器也是如此。