The todo-backend quickstart demonstrates how to implement a backend that exposes a HTTP API with JAX-RS
to manage a list of ToDo which are persisted in a database with JPA.
This quickstart shows how to deploy a JBoss EAP application on OpenShift that connects to a PostgreSQL database also hosted on OpenShift.
What is it?
The todo-backend quickstart demonstrates how to implement a backend that exposes a HTTP API with JAX-RS
to manage a list of ToDo which are persisted in a database with JPA.
-
The backend exposes a HTTP API to manage a list of todos that complies with the specs defined at todobackend.com.
-
It requires a connection to a PostgreSQL database to persist the todos.
-
It can be build with JBoss EAP S2I images for cloud deployment
-
It is deployed on OpenShift using the Helm Chart for JBoss EAP.
System Requirements
The application this project produces is designed to be run on Red Hat JBoss Enterprise Application Platform 7.4 or later.
All you need to build this project is Java 8.0 (Java SDK 1.8) or later and Maven 3.3.1 or later. See Configure Maven to Build and Deploy the Quickstarts to make sure you are configured correctly for testing the quickstarts.
Architecture
Architecture with S2I
This backend is built using JBoss EAP S2I Builder and Runtime images. When the image is built, it provisions the JBoss EAP application server and all the feature packs it needs for its features:
build:
s2i:
galleonLayers:
- cloud-server
- postgresql-datasource
The cloud-server layer provides everything needed to run the backend on OpenShift. This also includes access to
Jakarta EE APIs such as CDI, JAX-RS, JPA, etc. These two layers comes from the JBoss EAP feature pack provided in the
JBoss EAP S2I builder image.
Connection to the PostgreSQL database
As mentioned, the JDBC drivers and datasource configuration that the backend uses to connect to the PostgreSQL database
is provided by the org.jboss.eap:eap-datasources-galleon-pack feature pack.
By default, it exposes a single datasource.
In the backend, the name of this datasource is ToDos and is specified in the persistence.xml to configure JPA:
<persistence-unit name="primary">
<jta-data-source>java:jboss/datasources/ToDos</jta-data-source>
</persistence-unit>
At runtime, we only need a few environment variables to establish the connection from JBoss EAP to the external PostgreSQL database:
-
POSTGRESQL_DATABASE- the name of the database (that will be calledtodos) -
POSTGRESQL_SERVICE_HOST- the host to connect to the database -
POSTGRESQL_SERVICE_PORT- The port to connect to the database -
POSTGRESQL_USER&POSTGRESQL_PASSWORD- the credentials to connect to the database -
POSTGRESQL_DATASOURCE- The name of the datasources (as mentioned above, it will beToDos)
Filters for Cross-Origin Resource Sharing (CORS)
The Web frontend for this quickstart uses JavaScript calls to query the backend’s HTTP API.
We must enable Cross-Origin Resource Sharing (CORS) filters in the undertow subsystem of JBoss EAP to allow
these HTTP requests to succeed.
Configuration with JBoss EAP S2I
As we use S2I to build the application, we need to provide environment variables that will be used to configure the CORS filters in Undertow.
Each filter requires three environment variables (its name, its header key and value) to define them. So to define an
HTTP header such as Access-Control-Allow-Methods: GET, POST, OPTION, PUT, DELETE, PATCH we need three environment variables:
-
acam_FILTER_RESPONSE_HEADER_NAME=Access-Control-Allow-Methods -
acam_FILTER_RESPONSE_HEADER_VALUE=GET, POST, OPTION, PUT, DELETE, PATCH -
acam_FILTER_REF_NAME=Access-Control-Allow-Methods
We need one more environment variable to specify all the filters we want to enabled. The value is the list
of prefixes we used for each individual filter (such as acam in the example above):
-
FILTERS=acao, acam, acah, acac, acma
Run the Backend on OpenShift
Prerequisites
-
You must be logged in OpenShift and have an
occlient to connect to OpenShift -
Helm must be installed to deploy the backend on OpenShift.
Once you have installed Helm, you need to add the repository that provides Helm Charts for JBoss EAP:
$ helm repo add jboss-eap https://jbossas.github.io/eap-charts/
"jboss-eap" has been added to your repositories
$ helm search repo jboss-eap
NAME CHART VERSION APP VERSION DESCRIPTION
jboss-eap/eap74 ... ... A Helm chart to build and deploy EAP 7.4 applications
Deploy a PostgreSQL Database on OpenShift
$ oc new-app postgresql-ephemeral \
-p DATABASE_SERVICE_NAME=todos-db \
-p POSTGRESQL_DATABASE=todos
This will create a PostgreSQL database named todos on OpenShift that can be accessed on the port 5432 on the service todos-db.
We don’t need to copy the credentials to connect to the database as we will retrieve them later using the todos-db secret that was created
when the database is deployed.
Build and Deploy the Backend on OpenShift with JBoss EAP S2I
The backend will be built and deployed on OpenShift with a Helm Chart for JBoss EAP.
$ helm install todo-backend --set build.ref=EAP_7.4.0.Final -f https://raw.githubusercontent.com/jbossas/eap-charts/main/examples/eap74/todo-backend/todo-backend-s2i.yaml jboss-eap/eap74
NAME: todo-backend
...
STATUS: deployed
REVISION: 1
The Helm Chart for this quickstart contains all the information to build an image from the source code using S2I:
build:
uri: https://github.com/jboss-developer/jboss-eap-quickstarts.git
mode: s2i
s2i:
galleonLayers:
- cloud-server
- postgresql-datasource
Any configuration specified by this chart is described in its README that is displayed in OpenShift Dev console or using the command:
$ helm show readme jboss-eap/eap74
Environment variables for PostgreSQL
The Helm Chart also contains the environment variables required to connect to the PostgreSQL database. In local deployment the credentials were passed directly as the values of the environment variables. For OpenShift, we rely on secrets so that the credentials are never copied outside OpenShift:
deploy:
env:
- name: POSTGRESQL_PASSWORD
valueFrom:
secretKeyRef:
key: database-password
name: todos-db
When the application is deployed, the value for the POSTGRESQL_PASSWORD will be taken from the key database-password
in the secret todos-db.
Let’s wait for the application to be built and deployed:
$ oc get deployment/todo-backend -w
NAME READY UP-TO-DATE AVAILABLE AGE
todo-backend 0/3 3 0 31s
...
todo-backend 3/3 3 3 4m31s
Use the todobackend Web Frontend
Once the backend is deployed on OpenShift, it can be accessed from the route todo-backend.
Let’s find the host that we can use to connect to this backend:
$ oc get route todo-backend -o jsonpath="{.spec.host}"
todo-backend-jmesnil1-dev.apps.sandbox.x8i5.p1.openshiftapps.com
This value will be different for every installation of the backend.
|
Make sure to prepend the host with |
We can verify that this application is properly working as a ToDo Backend by running its specs on it.
Once all tests passed, we can use the todobackend client to have a Web application connected to the backend.
|
todobackend.com is an external service used to showcase this quickstart. It might not always be functional but does not impact the availability of this backend. |
Clean Up
Remove the Backend
The backend can be deleted from OpenShift by running the command:
$ helm delete todo-backend
release "todo-backend" uninstalled
Remove the Database
The PostresSQL database can be deleted from OpenShift by running the commands:
$ oc delete all -l template=postgresql-ephemeral-template
replicationcontroller "todos-db-1" deleted
service "todos-db" deleted
deploymentconfig.apps.openshift.io "todos-db" deleted
$ oc delete secret todos-db
secret "todos-db" deleted
Conclusion
This quickstart shows how the datasource feature pack provided by JBoss EAP simplifies the deployment of a JBoss EAP Jakarta EE backend on OpenShift to connect to an external database and exposes an HTTP API.