Config
The following methods can be used to define the configuration for your microservice using the powerful Config
API:
If you do not specify any of this information, we attempt to resolve it through the following methods: - Resolve file first in working directory, then in classpath, using the following names: - The "configFile" argument in the command line arguments passed in through the constructor.
- The value of the
Main-Config
entry in the manifest file. - A config file in the same location and with the same name as the executable jar file. (e.g.
java -jar myjar.jar
will look formyjar.cfg
). - Resolve any
*.cfg
file that can be found in the working directory. - Resolve any of the following files in the classpath:
juneau.cfg
default.cfg
application.cfg
app.cfg
settings.cfg
If no configuration file is found, and empty in-memory configuration is used. The configName(String) method allows you to explicitly specify the name of the external configuration file location for your microservice.
Microservice
.create()
.config("my-files/MyMicroservice.cfg")
.build()
.start()
;
By default, we try to find the file on the file system and then the classpath.
If located on the file system, then the configuration is writeable and the microservice can automatically listen for and
react to changes in the configuration file on the file system.
If located on the classpath, then the configuration can still react to modifications made to it through the Config
API
but the changes cannot be persisted since the location prevents the file from being modified.
The configStore(ConfigStore) method can be used to explicitly specify a configuration store. This can include your own custom configuration store, such as one that's implemented in a relational database.
Microservice
.create()
.configStore(new MyConfigSqlStore())
.configName("MyConfig")
.build()
.start()
;
The config(Config) method can be used to explicitly specify a Config file as the microservice configuration. When this method is used, the above two methods are bypassed entirely.
Config config = getMyOwnConfig();
Microservice
.create()
.config(config)
.build()
.start()
;
Once the configuration is resolved, it is made as the system default configuration available through the Config.getSystemDefault().
This in turn allows it to be used by REST resources that reference the system default configuration via the
SYSTEM_DEFAULT
such as those implementing the BasicRestConfig interface.
BasicRestConfig.java
@Rest(
config="$S{juneau.configFile,SYSTEM_DEFAULT}"
...
)
The Microservice.getConfig() method can be used to get access to the configuration.
Config config = Microservice.getInstance().getConfig();
File logDir = config.get("Logging/logDir").as(File.class).orElse(null);
boolean append = config.get("Logging/append").asBoolean().orElse(null);
String format = config.get("Logging/format", "[{date} {level}] {msg}%n").orElse(null);
long limit = config.get("Logging/limit").asLong().orElse(null);
Map levels = config.get("Logging/levels").as(Map.class, String.class, Level.class).orElse(null);
Changes to the configuration file can trigger notifications that can be used to restart your microservice or make various other on-the-fly changes. This can be accomplished by either overriding the Microservice.onConfigChange(ConfigEvents) or implementing a listener and using the onConfigChange() methods. These will be described in detail later.