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)
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:
Service2 with properties:
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:
management.endpoints.web.exposure.include=env # some properties speedlog.common-name-property="this should be overwritten after deploy" service1.specific="this should be overwritten after deploy"
management.endpoints.web.exposure.include=env # some properties speedlog.common-name-property="this should be overwritten after deploy" service2.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.
<?xml version="1.0" encoding="UTF-8"?> <jboss-deployment-structure> <deployment> <dependencies> <module name="pl.speedlog.example.twoappinwildfly.service1.configuration"/> </dependencies> </deployment> </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.
<?xml version="1.0" encoding="UTF-8"?> <jboss-web xmlns="http://www.jboss.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd"> <context-root>service1</context-root> </jboss-web>
2) 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.
3) 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.
<?xml version="1.0" encoding="UTF-8"?> <module xmlns="urn:jboss:module:1.1" name="pl.speedlog.example.twoappinwildfly.service1.configuration"> <resources> <resource-root path="."/> </resources> </module>
And now we can create “application.properties” for our application.
management.endpoints.web.exposure.include=env # override properties speedlog.common-name-property="This is text from jboss module of service 1" service1.specific="Service 1 specific"
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:
Now application should load application properties from “application.properties” in application resources and “application-uat.properties” from JBoss/Wildfly module.
4) Now just deloy app in Wildfly and you can check value of properties by link:
Described example with two deployed application is on github:
Declaration of JBoss/Wildfly module is here: