我真的很想将容器管理的事务与JPA一起使用,因此可以通过简单的方法在server.xml中创建数据源,然后在我的代码中使用@PersistenceContext。
我当前面临的问题-也许我觉得太复杂-事实是我在运行时在Cloud Foundry环境中以VCAP_SERVICES系统环境变量的身份获取凭据。
此VCAP_SERVICES变量包含带有必需凭据的大JSON。因此,我首先必须在其中提取相应的JSON,然后将其作为属性传递。
我的最初想法是在这种情况下使用转换器从JSON返回值,以便可以使用它们。但是似乎在已定义的配置xml文件中这是不可能的。
我肯定要防止的是“注入”我自己做的事情,例如拥有属性文件并从其中的不同环境DEV,TEST,PROD复制值,因为Cloud Foundry环境已经在做为我工作。
所以问题是: 如何将凭证从VCAP_SERVICES系统环境变量获取到server.xml配置中?记住:我真的必须解析VCAP_SERVICES变量,因为它是JSON并提取值。
<?xml version="1.0" encoding="UTF-8"?>
<server description="Config example">
<featureManager>
<feature>javaee-8.0</feature>
<feature>mpMetrics-1.1</feature>
<feature>monitor-1.0</feature>
</featureManager>
<!-- Postgres config-example-db definition -->
<dataSource id="DefaultDataSource" jndiName="jdbc/config-example" jdbcDriverRef="postgresql-driver"
type="javax.sql.ConnectionPoolDataSource" transactional="true">
<properties serverName="config-example-db" portNumber="5432"
databaseName="postgres"
user="${config-example.db.username}"
password="${config-example.db.password}"/>
</dataSource>
<basicRegistry id="basic" realm="MicroProfileMetrics">
<user name="admin" password="adminadmin"/>
<user name="nonadmin" password="guest"/>
</basicRegistry>
<administrator-role>
<user>admin</user>
</administrator-role>
</server>
答案 0 :(得分:0)
对于许多服务,自由构建包将自动配置server.xml。
PostgreSQL是其中之一。看到:
https://github.com/cloudfoundry/ibm-websphere-liberty-buildpack/blob/master/docs/services/postgresql.md
此外,对于许多服务,buildpack将创建环境变量,然后可由应用程序访问。
例如,将cloudantNoSQL服务绑定到应用会在VCAP_SERVICES中产生以下凭证:
{
"binding_name": null,
"credentials": {
"apikey": "<apikey>",
"host": "<host>-bluemix.cloudantnosqldb.test.appdomain.cloud",
"iam_apikey_description": "Auto-generated for binding <host>",
"iam_apikey_name": "Cloudant-sd",
"iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Writer",
"iam_serviceid_crn": "crn:v1:staging:public:iam-identity::<id>::serviceid:ServiceId-<serviceid>",
"url": "https://<host>-bluemix.cloudantnosqldb.test.appdomain.cloud",
"username": "<username>-bluemix"
},
然后,buildpack将在runtime-vars.xml中创建变量:
<variable name='cloud.services.Cloudant-sd.label' value='cloudantNoSQLDB'/>
<variable name='cloud.services.Cloudant-sd.plan' value='Lite'/>
<variable name='cloud.services.Cloudant-sd.name' value='Cloudant-sd'/>
<variable name='cloud.services.Cloudant-sd.instance_name' value='Cloudant-sd'/>
<variable name='cloud.services.Cloudant-sd.connection.apikey' value='<apikey'/>
<variable name='cloud.services.Cloudant-sd.connection.host' value='<host>-bluemix.cloudantnosqldb.test.appdomain.cloud'/>
<variable name='cloud.services.Cloudant-sd.connection.iam_apikey_description' value='Auto-generated for binding <host>'/>
<variable name='cloud.services.Cloudant-sd.connection.iam_apikey_name' value='Cloudant-sd'/>
<variable name='cloud.services.Cloudant-sd.connection.iam_role_crn' value='crn:v1:bluemix:public:iam::::serviceRole:Writer'/>
<variable name='cloud.services.Cloudant-sd.connection.iam_serviceid_crn' value='crn:v1:staging:public:iam-identity::<id>::serviceid:ServiceId-<serviceid'/>
<variable name='cloud.services.Cloudant-sd.connection.url' value='https://<host>-bluemix.cloudantnosqldb.test.appdomain.cloud'/>
<variable name='cloud.services.Cloudant-sd.connection.username' value='<username>-bluemix'/>