Skip to content

ScaleCube Config is a configuration access management library for JVM based distributed applications

License

Notifications You must be signed in to change notification settings

scalecube/scalecube-config

Repository files navigation

ScaleCube Config

Maven Central

ScaleCube Config is a configuration management library for JVM based distributed applications.

It provides the following functionality:

  • Dynamic typed properties
  • Register callbacks on property changes
  • Object binding for grouping properties
  • Extensible range of supported property sources (environment variables, program arguments, classpath, property files, mongodb, git repository, zookeeper etc.)
  • Support centralized hierarchical property sources
  • Control over order of applying different property sources
  • Audit log of property changes
  • Expose properties and settings via JMX and/or HTTP

Usage

Configure and create configuration registry instance:

Predicate<Path> predicate = path -> path.toString().endsWith(".props"); // match by .props extension
ConfigRegistrySettings settings = ConfigRegistrySettings.builder()
        .addLastSource("classpath", new ClassPathConfigSource(predicate))
        .addLastSource("configDirectory", new DirectoryConfigSource("conf" /* base path */, predicate))
        .addListener(new Slf4JConfigEventListener()) // print all property changes to log
        .build();
ConfigRegistry configRegistry = ConfigRegistry.create(settings);

Get dynamic typed configuration property:

LongConfigProperty timeoutProperty = configRegistry.longProperty("http.request-timeout");
long timeout = timeoutProperty.get(30 /* default value */);

Register callbacks on property modifications:

timeoutProperty.addCallback((oldValue, newValue) -> 
        System.out.println("Timeout value changed to " + newValue));

Register validators on property values (only values which passed all validators will be applied):

timeoutProperty.addValidator(val -> val >= 0); // timeout can't be negative

Utilize object binding:

// Define configuration class
public interface MyConfig {
  private boolean meaning;
  private int answer;
  private double realAnswer;
  ...
}

// myapp.config.meaning=true
// myapp.config.answer=42
// myapp.config.realAnswer=42.000000000000001
ObjectConfigProperty<MyConfig> config = configRegistry.objectProperty("myapp.config", MyConfig.class);

// Get current config values
MyConfig currentConfig = config.value(MyConfig.defaultValue() /* or default */);

// Register callback (called only once per config reload even when several properties changed)
config.addCallback((oldConfig, newConfig) -> 
        System.out.println("MyConfig updated from " + oldConfig + " to " + newConfig)); 
        
// Register validator
// If meaning is present, an answer should be at least as big as the answer  
config.addValidator(val -> val.meaning && val.answer >= 42);     

Start embedded HTTP server which exposes configuration endpoints:

ConfigRegistryHttpServer.create(configRegistry, 5050); // starts http server on port 5050

After HTTP server is started explore configuration registry by browsing following endpoints:

See more examples at config-examples module.

Maven

Binaries and dependency information for Maven can be found at http://search.maven.org.

Change history and version numbers can be found at CHANGES.md.

Maven dependency:

<dependency>
  <groupId>io.scalecube</groupId>
  <artifactId>config</artifactId>
  <version>x.y.z</version>
</dependency>

<!-- For exposing config HTTP endpoints -->
<dependency>
  <groupId>io.scalecube</groupId>
  <artifactId>config-http-server</artifactId>
  <version>x.y.z</version>
</dependency>

<!-- For MongoDB integration (beta version) -->
<dependency>
  <groupId>io.scalecube</groupId>
  <artifactId>config-mongo</artifactId>
  <version>x.y.z</version>
</dependency>

Bugs and Feedback

For bugs, questions and discussions please use the GitHub Issues.

License

Apache License, Version 2.0