0

I am new in Akka and want to set up different application.conf files per environment (test, staging, prod, etc.).

I read the documentation from Akka but I couldn't find a way to do it.

May you know any proper way to have multiple configuration files?

pik4
  • 1,283
  • 3
  • 21
  • 56

4 Answers4

3

Akka uses the Typesafe Config Library.

The convenience method ConfigFactory.load() loads the following (first-listed are higher priority):

  1. system properties
  2. application.conf (all resources on classpath with this name)
  3. application.json (all resources on classpath with this name)
  4. application.properties (all resources on classpath with this name)
  5. reference.conf (all resources on classpath with this name)

So we usually create an application.conf file in /resources folder.

For different environments, we can create environment specific files like

  1. development.conf
  2. production.conf

Using includes, include application.conf at the beginning and specify environment specific configurations which will override the application.conf.

To use a specific environment force a different config source (e.g. from command line -Dconfig.resource=environment.conf)

Start your application using below command:

$ sbt run -Dconfig.resource=development.conf

0

Here are couple ways to do it.

  1. Add tags with in a file itself like

prod-config : { YOUR_CONFIGURATION_FOR_PROD }

test-config : { YOUR_CONFIGURATION_TEST_ENV }

and so-on

  1. Make separate files for the each environment and rename it once after deploying them to specific environment. Say from application-prod.conf to application.conf
Community
  • 1
  • 1
Puneeth Reddy V
  • 1,538
  • 13
  • 28
  • In both of suggestions, how you can choose different file per environment? – pik4 Apr 24 '18 at 08:09
  • In 1st approach you have only one file which have properties per environment separated by tags based on environment. In 2nd approach you can ask build tool to rename (say for Maven https://stackoverflow.com/a/26639059/3595618) using a custom property(https://stackoverflow.com/a/24206194/3595618). – Puneeth Reddy V Apr 24 '18 at 12:50
0

A possibility is have a single variable in application.conf which would be "confFile", based on an actual env like

confFile=${?CONF_FILE_NAME}

Then in your code, you load the correspondind file like this

val configFile = ConfigFactory.load().getString("confFile")
val appConf = ConfigFactory.load(configFile)
C4stor
  • 8,355
  • 6
  • 29
  • 47
  • This config file should be `.conf`, `.properties` or something else? – pik4 Apr 24 '18 at 08:08
  • Both are possible. If I'm not mistaken, the typesafe config factory will look for .conf, .properties and .json extensions – C4stor Apr 24 '18 at 09:12
0

The options for this aren't so much related to Akka as it is to the config library. Akka uses the default config resolution unless you give it a Config instance.

With no specific user code to manually select a specific file you have a few options. I think the most useful are the system properties config.resource to choose a file on the class path (inside the app jar for example), config.file to use a file from the file system.

Of course you may still have reasons to write your own code to select files as the other answers suggested.

More details in the config library docs: https://github.com/lightbend/config#standard-behavior

johanandren
  • 11,249
  • 1
  • 25
  • 30