Use different config file according to deployment server

77 views
Skip to first unread message

Vasilis Souvatzis

unread,
Oct 24, 2018, 4:06:57 PM10/24/18
to Payara Forum
Good evening! Hopefully I'm asking at the right place, I'm new to proper development and deployment and I'm tasked to do things I don't know yet how.

I'm making a Java EE program and deploying locally. At some point we'll be deploying on a couple more servers, staging and deployment (all Payara of course).

I want to have config files (Microprofile Config API rocks!!!) for each deployment server.

Is there a way to detect which server I'm deploying on so it automatically uses a specific config file? Perhaps with an environment variable on each server which says the name of the server and then choose the config file?

For example, with Spring Boot I know you can specify the properties file when you run "java jar...". Is there anything similar when deploying a .war file?

I don't know yet how we'll be deploying, maybe at first through the admin console and after a while with CI/CD.

Rudy De Busscher

unread,
Oct 25, 2018, 5:05:55 AM10/25/18
to Payara Forum
Hi Vasilis,

Payara has different sources for the MicroProfile config defined.  You can have a look at this documentation page https://docs.payara.fish/documentation/microprofile/config.html

The idea is that you don't read a different file name based on the server name, but that you read a certain file, which contains different content on each server. (externalized from your deployment artifact)

Regards
Rudy

Vasilis Souvatzis

unread,
Oct 26, 2018, 3:24:52 AM10/26/18
to Payara Forum
Thank you Rudy, I went through the doc you provided and it's quite good actually.

The way I see it I have two options, either set the config properties on the server and read those (different content on each server) or just have the server name property and based on that read different values from my project's internal config file. Maybe they don't want to have settings on the server if we deploy more apps, I don't know.

What Payara has done with the config though is brilliant, I already set a couple of server entries and it works like a charm...

Rudy De Busscher

unread,
Oct 26, 2018, 5:09:09 AM10/26/18
to Payara Forum
> What Payara has done with the config though is brilliant

Thanks :)


I'm glad that I could help.

Regards
Rudy

On Wednesday, 24 October 2018 22:06:57 UTC+2, Vasilis Souvatzis wrote:

Ondro Mihályi

unread,
Oct 29, 2018, 10:11:11 AM10/29/18
to Vasilis Souvatzis, payara...@googlegroups.com
Hi Vasilis,

It's possible to create a custom MicroProfile config source in an app to read config properties based on an environment setting. 

To create a config source, just create a class that implements ConfigSource and register it via a service loader file in the app's classpath. You can follow this great blog post: https://www.phillip-kruger.com/post/microprofile_config/

You custom configu source can provide any value, even return a value of another key. For example, this would work in the ConfigSource.getValue() method:

public String getValue(String key) {
  Config config = ConfigProvider.getConfig();
  return config.getValue("testing." + key, String.class);
}

The above would return a value of each key with the "testing." prefix, so for a key "timeout" it would return the value of "testing.timeout". 
If you make the prefix configurable based on another MicroProfile Config property, you can provide config variants for each environment and switch between environments with a single system property or environment variable.

I hope that helps,
Ondro
Dňa pi 26. 10. 2018, 9:24 Vasilis Souvatzis <vas...@gmail.com> napísal(a):
--
You received this message because you are subscribed to the Google Groups "Payara Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to payara-forum...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/payara-forum/794d560f-e95b-4263-a325-d44c3a28a07f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vasilis Souvatzis

unread,
Nov 1, 2018, 4:42:48 AM11/1/18
to Payara Forum
Correct me if I'm wrong but from what I understand the custom ConfigSource uses the class instead of a config file to provide the key-values right? This is quite versatile too!

"If you make the prefix configurable based on another MicroProfile Config property, you can provide config variants for each environment and switch between environments with a single system property or environment variable."
That's an excellent suggestion as well, I've already set server.name variables to the servers so I guess I'm already halfway to what you're suggesting. Very nice.

Thank you very much for now, this will help me tremendously very soon. From deploying my home projects to my local server, I now have to deploy to three systems :)

Ondro Mihályi

unread,
Nov 1, 2018, 7:46:28 AM11/1/18
to Vasilis Souvatzis, Payara Forum
Yes, Vasilis,

You can provide configuration also using a class inside your app (on the classpath) that implements ConfigSource and is registered using a service locator file. This is described in Phillip Kruger's blog which I linked to together with using config to configure a custom config class. This means you can use config to configure config. You just need to prevent loops in your config source, which is easy - just return null for config keys required by your config source class.

Ondro

Dňa št 1. 11. 2018, 9:42 Vasilis Souvatzis <vas...@gmail.com> napísal(a):
Reply all
Reply to author
Forward
0 new messages