Deploy two springboot applications in Wildfly with external properties

Personally I prefer jar then war ("Make JAR, not WAR" - Josh Long), but sometimes we have constraints that are not depend on us.
This example focus about how to deploy two springboot applications in Wildfly and how to externalize application properties for each application.

The case is:

  • deploy two springboot applications in Wildfly,
  • each application has its own properties,
  • properties need to be loaded from external source (not from jar)
Note

There is also alternative solution for example use of Spring Cloud Config, but it requires additional server.

Let's assume that we have two microservices.
Service1 with properties:

  • speedlog.common-name-property
  • service1.specific

Service2 with properties:

  • speedlog.common-name-property
  • service2.specific

Because we have two application that has property with the same name (speedlog.common-name-property) we can't set this property in:

  • environment variable
  • system properties
  • JVM args of Wildfly

If we do so, both applications would have the same value of this property.
So let's check how we can externalize properties for both applications.

We will add to each application actuator library to easy check properties value in runtime.
We also need to expose "env" endpoint in properties.
So in each application resources we have something like this:

service1/src/main/resources/application.properties

1management.endpoints.web.exposure.include=env
2 
3# some properties
4speedlog.common-name-property="this should be overwritten after deploy"
5service1.specific="this should be overwritten after deploy"

service2/src/main/resources/application.properties

1management.endpoints.web.exposure.include=env
2 
3# some properties
4speedlog.common-name-property="this should be overwritten after deploy"
5service2.specific="this should be overwritten after deploy"

Now let's imagine that we need to override preferences from application resources, when we deploy it in Wildfly.

Example solution may be a JBoss/Wildfly module with application properties file.

Below instructions for service1. For service2 it will be analogous:

  1. We need to add some files in application sources. service1/scr/main/webapp/WEB-INF/jboss-deployment-structure.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<jboss-deployment-structure>
3    <deployment>
4        <dependencies>
5            <module name="pl.speedlog.example.twoappinwildfly.service1.configuration"/>
6        </dependencies>
7    </deployment>
8</jboss-deployment-structure>

In this file we declare where JBoss/Wildfly should find configuration of our module.

And optional we can add in the same directory (WEB-INF) file jboss-web.xml to declare root context (URL) of application.

1<?xml version="1.0" encoding="UTF-8"?>
2<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
3           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4           xsi:schemaLocation="
5      http://www.jboss.com/xml/ns/javaee
6      http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
7    <context-root>service1</context-root>
8</jboss-web>
  1. Now we add a module to Wildfly.
    In Wildfly "module" directory add directory tree that we declare in jboss-deployment-structure.xml.
    So we should create directory pl/speedlog/example/twoappinwildfly/service1/configuration and then additional "main" directory.

  2. Add module definition and properties file for application.
    In directory that we create in previous point, we create file module.xml.
    The "name" of module should be the same as in jboss-deployment-structure.xml in source code of application.

1<?xml version="1.0" encoding="UTF-8"?>
2<module xmlns="urn:jboss:module:1.1" name="pl.speedlog.example.twoappinwildfly.service1.configuration">
3    <resources>
4        <resource-root path="."/>
5    </resources>
6</module>

And now we can create application.properties for our application.

1management.endpoints.web.exposure.include=env
2 
3# override properties
4speedlog.common-name-property="This is text from jboss module of service 1"
5service1.specific="Service 1 specific"
Warning

If you place here application.properties file it will override properties file from application resources!

Just want to override only few properties? There is a simple solution.

a) create file with override properties with profile name for example: application-uat.properties.
b) add to your system environment variable to run application in “uat” profile:

1SPRING_PROFILES_ACTIVE=uat

Now application should load application properties from application.properties in application resources and application-uat.properties from JBoss/Wildfly module.

  1. Now just deloy app in Wildfly and you can check value of properties by link:
1http://localhost:8080/service1/actuator/env/speedlog.common-name-property  
2http://localhost:8080/service1/actuator/env/service1.specific

Described example with two deployed application is on github: https://github.com/speedlog/two-app-in-wildfly

Declaration of JBoss/Wildfly module is here: https://github.com/speedlog/two-app-in-wildfly/tree/master/docker/wildfly/pl/speedlog/example/twoappinwildfly/service1/configuration/main