项目作者: scalecube

项目描述 :
ScaleCube Config is a configuration access management library for JVM based distributed applications
高级语言: Java
项目地址: git://github.com/scalecube/scalecube-config.git
创建时间: 2017-08-15T16:01:53Z
项目社区:https://github.com/scalecube/scalecube-config

开源协议:Apache License 2.0

下载


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:

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

Get dynamic typed configuration property:

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

Register callbacks on property modifications:

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

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

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

Utilize object binding:

  1. // Define configuration class
  2. public interface MyConfig {
  3. private boolean meaning;
  4. private int answer;
  5. private double realAnswer;
  6. ...
  7. }
  8. // myapp.config.meaning=true
  9. // myapp.config.answer=42
  10. // myapp.config.realAnswer=42.000000000000001
  11. ObjectConfigProperty<MyConfig> config = configRegistry.objectProperty("myapp.config", MyConfig.class);
  12. // Get current config values
  13. MyConfig currentConfig = config.value(MyConfig.defaultValue() /* or default */);
  14. // Register callback (called only once per config reload even when several properties changed)
  15. config.addCallback((oldConfig, newConfig) ->
  16. System.out.println("MyConfig updated from " + oldConfig + " to " + newConfig));
  17. // Register validator
  18. // If meaning is present, an answer should be at least as big as the answer
  19. config.addValidator(val -> val.meaning && val.answer >= 42);

Start embedded HTTP server which exposes configuration endpoints:

  1. 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:

  1. <dependency>
  2. <groupId>io.scalecube</groupId>
  3. <artifactId>config</artifactId>
  4. <version>x.y.z</version>
  5. </dependency>
  6. <!-- For exposing config HTTP endpoints -->
  7. <dependency>
  8. <groupId>io.scalecube</groupId>
  9. <artifactId>config-http-server</artifactId>
  10. <version>x.y.z</version>
  11. </dependency>
  12. <!-- For MongoDB integration (beta version) -->
  13. <dependency>
  14. <groupId>io.scalecube</groupId>
  15. <artifactId>config-mongo</artifactId>
  16. <version>x.y.z</version>
  17. </dependency>

Bugs and Feedback

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

License

Apache License, Version 2.0