2

我有一个网络应用程序,我试图在 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 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">
4

2 回答 2

5

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

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>

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

于 2019-06-05T20:56:16.650 回答
0

很好的问答。此外,不仅 Micro 请求java:app/JNDI 名称的前缀,full server 也会这样做。

于 2022-01-21T01:49:00.290 回答