PayaraMicro没有在WAR的玻璃鱼资源中添加jdbc详细信息.xml



我有 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 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.

我还更改为使用以下 DOCTYPE 使用payara-resources.xml

<!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">

我终于开始工作了。 首先,我正在部署的应用程序是带有打包的 EJB 模块的 WAR,因此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中,但我更喜欢使用资源获得的控制.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 请求 JNDI 名称的java:app/前缀,而部署到完整服务器的项目不使用该前缀(我还没有在完整服务器部署中使用java:app/进行测试)。 无论如何,以上有效。

很好的问题和答案。此外,不仅Micro请求JNDI名称的java:app/前缀,而且完整服务器也这样做。

相关内容

  • 没有找到相关文章

最新更新