Chapter 4. Design and Development

4.1. Overview

For a discussion of microservices as a software architectural style, as well as the design and composition of the sample application, refer to the previously published reference architecture on Building microservices with JBoss EAP 7.

OpenShift provides an ideal platform for deploying, hosting and managing microservices. By deploying each service as an individual docker container, OpenShift helps isolate each service and decouples its lifecycle and deployment from that of other services. OpenShift can configure the desired number of replicas for each service and provide intelligent scaling to respond to varying load.

This sample application uses the Source-to-Image (S2I) mechanism to build and assemble reproducible container images from the application source and on top of supported OpenShift images.

4.2. Application Structure

The source code for the sample application is checked in to a public GitHub repository. The code is organized as four separate directories, each structured as a Maven project with a pom file at the root. An aggregation POM file is provided at the top level of the root directory to build all four project if needed, although this build file is neither required nor used by OpenShift.

4.3. Customizing the Application Server

The Product and Sales services each have a database dependency and use their respective MySQL database to store and retrieve data. The supported xPaaS Middleware Images bundle MySQL JDBC drivers, but the driver would have to be declared in the server configuration file and a datasource would need to be described in order for the application to access the database through a connection pool.

To make customizations to a server, provide an updated server configuration file. The replacement configuration file should be named standalone-openshift.xml and placed in a directory called configuration at the root of the project.

Note

Some configuration be performed by simply providing descriptive environment variables. For example, supplying the DB_SERVICE_PREFIX_MAPPING variable and value instructs the script to add MySQL and/or PostgreSQL datasources to the EAP instance. Refer to the documentation for OpenShift images for further details.

In order to make the required changes to the correct baseline, obtain the latest server configuration file. The supported image is located at registry.access.redhat.com/jboss-eap-7/eap70-openshift. To view the original copy of the file, you can run this docker container directly:

# docker run -it registry.access.redhat.com/jboss-eap-7/eap70-openshift \ cat /opt/eap/standalone/configuration/standalone-openshift.xml
<?xml version="1.0" ?>

<server xmlns="urn:jboss:domain:4.0">
    <extensions>
    ...

Declare the datasource with parameterized variables for the database credentials. For example, to configure the product datasource for the product service:

<subsystem xmlns="urn:jboss:domain:datasources:1.2">
    <datasources>
        <datasource jndi-name="java:jboss/datasources/ProductDS" enabled="true"
                use-java-context="true" pool-name="ProductDS">
            <connection-url>
                  jdbc:mysql://${env.DATABASE_SERVICE_HOST:product-db}
                      :${env.DATABASE_SERVICE_PORT:3306}/${env.MYSQL_DATABASE:product}
            </connection-url>
            <driver>mysql</driver>
            <security>
                <user-name>${env.MYSQL_USER:product}</user-name>
                <password>${env.MYSQL_PASSWORD:password}</password>
            </security>
        </datasource>

The datasource simply refers to the database driver as mysql. Declare the driver class in the same section after the datasource:


</datasource>
<drivers>
    <driver name="mysql" module="com.mysql">
        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
    </driver>

With the above configuration, environment variables are substituted to specify connection details and the database host name is resolved to the name of the OpenShift service hosting the database.

The default EAP welcome application is disabled in the Red Hat xPaaS EAP image. To deploy the Presentation application to the root context, rename the warName to ROOT in the Maven pom file:

<build>
  <finalName>${project.artifactId}</finalName>
  <plugins>
    <plugin>
      <artifactId>maven-war-plugin</artifactId>
      <version>${version.war.plugin}</version>
      <configuration>
        <warName>ROOT</warName>
        <!-- Java EE 7 doesn’t require web.xml, Maven needs to catch up! -->
        <failOnMissingWebXml>false</failOnMissingWebXml>
      </configuration>
    </plugin>
  </plugins>
</build>