Goal
In this tutorial, we’ll cover how you can overwrite Payara configurations to access the services in Platform.sh.
Preparation
This tutorial assumes you have
- A working Payara application with Java 8 or higher
- A text editor of your choice.
Problems
Platform.sh has a Java configuration-reader library that provides a streamlined and easy to use way to interact with a Platform.sh environment and those services. However, you can also use the application regularly and overwrite those configurations when you deploy your application on Platform.sh. That is useful when you either already have one app and want to move to Platform.sh or keep the default configuration to run locally.
Steps
First, to use a database as a data source in Payara Micro, you’ll need to have the database already running. Once you have that in place, download the JDBC driver for the database and put it into your WEB-INF/lib
directory.
In your web.xml
add the following:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
<data-source>
<name>java:global/JPAExampleDataSource</name>
<class-name>org.postgresql.ds.PGSimpleDataSource</class-name>
<server-name>${server.host}</server-name>
<port-number>5432</port-number>
<database-name>${server.database}</database-name>
<user>${server.user}</user>
<password>${server.password}</password>
</data-source>
</web-app>
You can overwrite those configurations on the platform.app.yaml
the application configuration to Platform.sh. As shown in the configuration below.
name: app
type: "java:8"
disk: 1024
hooks:
build: mvn clean package payara-micro:bundle
relationships:
database: "db:postgresql"
web:
commands:
start: |
export HOST=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].host"`
export PASSWORD=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].password"`
export USER=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].username"`
export DATABASE=`echo $PLATFORM_RELATIONSHIPS|base64 -d|jq -r ".database[0].path"`
java -jar -Xmx$(jq .info.limits.memory /run/config.json)m -XX:+ExitOnOutOfMemoryError \
-Dserver.host=$HOST \
-Dserver.database=$DATABASE \
-Dserver.user=$USER \
-Dserver.password=$PASSWORD \
target/microprofile-microbundle.jar --port $PORT
Therefore, you can have the configuration or just migrate the application that already exists to Platform.sh.