Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Configure once, run everywhere 2016

317 views

Published on

Slideset I showed at JavaOne 2016.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Configure once, run everywhere 2016

  1. 1. BASEL BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENF HAMBURG KOPENHAGEN LAUSANNE MÜNCHEN STUTTGART WIEN ZÜRICH Configure Once, run everyhere! Configuration with Apache Tamaya
  2. 2. Configure once, run everywhere6.09.16 Anatole Tresch Principal Consultant, Trivadis AG (Switzerland) Star Spec Lead Technical Architect, Lead Engineer PPMC Member Apache Tamaya @atsticks anatole@apache.org anatole.tresch@trivadis.com 2 About Me
  3. 3. Configure once, run everywhere6.09.16 Agenda 3 ● Motivation ● Requirements ● The API ● Configuration Backends ● Demo ● Extensions
  4. 4. Configure once, run everywhere6.09.16 4 Motivation
  5. 5. Configure once, run everywhere6.09.16 5 What is Configuration ? Simple Key/value pairs? Typed values?
  6. 6. Configure once, run everywhere6.09.16 6 When is Configuration useful? Use Cases?
  7. 7. Configure once, run everywhere6.09.16 7 How is it stored? Remotely or locally? Classpath, file or ...? Which format? All of the above (=multiple sources) ?
  8. 8. Configure once, run everywhere6.09.16 8 When to configure? Develpment time ? Build/deployment time? Startup? Dynamic, anytime?
  9. 9. Configure once, run everywhere6.09.16 9 Configuration Lifecycle ? Static ? Refreshing ? Changes triggered ?
  10. 10. Configure once, run everywhere6.09.16 10 Do I need a runtime ? Java SE? Java EE? OSGI?
  11. 11. Bezeichnung Präsentation6.09.16 Requirements
  12. 12. Configure once, run everywhere6.09.16 12 ● Developer‘s Perspective ● Architectural/Design Requirements ● Operational Aspects ● Other Aspects Requirements
  13. 13. Configure once, run everywhere6.09.16 13 ● Easy to use. ● Developers want defaults. ● Developers don‘t care about the runtime (for configuration only). ● Developers are the ultimate source of truth ● Type Safety Developer‘s Requirements
  14. 14. Configure once, run everywhere6.09.16 Architectural/Design Requirements 14 Decouple code that consumes configuration from ● Backends Used ● Storage Format ● Distribution ● Lifecycle and versioning ● Security Aspects
  15. 15. Configure once, run everywhere6.09.16 15 ● Enable Transparency: ● What configuration is available ? ● What are the current values and which sources provided the value ? ● Documentation ● Manageable: ● Configuration changes without redeployment or restart. ● Solution must integrate with existing environment Operational‘s Requirements
  16. 16. Configure once, run everywhere6.09.16 16 ● Support Access Constraints and Views ● No accidential logging of secrets ● Dynamic changes ● Configuration Validation Other Aspects
  17. 17. Bezeichnung Präsentation6.09.16 Accessing Configuration The API
  18. 18. Configure once, run everywhere6.09.16 18 ● Leverage existing functionality where useful ● Only one uniform API for access on all platforms! ● Defaults provided by developer during development (no interaction with operations or external dependencies) API Requirements
  19. 19. Configure once, run everywhere6.09.16 19 ● Environment Properties ● System Properties ● CLI arguments ● Properties, xml-Properties Existing Mechanisms
  20. 20. Configure once, run everywhere6.09.16 20 Dependencies - API & Core <dependency>     <groupId>org.apache.tamaya</groupId>     <artifactId>tamaya­api</artifactId>     <version>0.2­SNAPSHOT</version> </dependency> <dependency>     <groupId>org.apache.tamaya</groupId>     <artifactId>tamaya­core</artifactId>     <version>0.2­SNAPSHOT</version> </dependency> 
  21. 21. Configure once, run everywhere6.09.16 Programmatic API 21 ● Add dependency org.apache.tamaya:core: 0.2-incubating ● Add Config to META-INF/javaconfiguration.properties ● GO! Configuration config =                  ConfigurationProvider.getConfiguration(); // single property access String name = config.getOrDefault("name", "John"); int ChildNum = config.get("childNum", int.class); // Multi property access Map<String,String> properties = config.getProperties(); // Templates (provided by extension) MyConfig config = ConfigurationInjection.getConfigurationInjector()                     .getConfig(MyConfig.class);
  22. 22. Configure once, run everywhere6.09.16 22 Dependencies – Injection SE <dependency>     <groupId>org.apache.tamaya.ext</groupId>     <artifactId>tamaya­injection­api</artifactId>     <version>0.2­SNAPSHOT</version> </dependency> <dependency>     <groupId>org.apache.tamaya.ext</groupId>     <artifactId>tamaya­injection</artifactId>     <version>0.2­SNAPSHOT</version> </dependency> 
  23. 23. Configure once, run everywhere6.09.16 23 @Config(value=„admin.server“, defaultValue=“127.0.0.1“) private String server; @Config(value=“admin.port“,                 defaultValue=“8080“) private int port; @Config(value=“admin.connections“) private int connections = 5; @Config(„address“) private Address address; MyTenant t = new MyTenant(); ConfigurationInjection     .getConfigurationInjector()     .configure(t); MyTenant t = new MyTenant(); ConfigurationInjection     .getConfigurationInjector()     .configure(t);
  24. 24. Bezeichnung Präsentation6.09.16 Configuration Backends
  25. 25. Configure once, run everywhere6.09.16 25 ● Support existing mechanisms OOTB ● Provide a simple SPI for (multiple) property sources ● Define a mechanism to prioritize different property sources ● Allow different strategies to combine values ● Support Filtering ● Support Type Conversion Configuration Backends
  26. 26. Configure once, run everywhere6.09.16 26 So what is a property source ?
  27. 27. Configure once, run everywhere6.09.16 PropertySource 27 ● Add dependency org.apache.tamaya:core: 0.2-incubating ● Add Config to META-INF/java Map<String,String> getProperties();configuration.properties ● GO! public interface PropertySource { PropertyValue get(String key); Map<String,String> getProperties(); boolean isScannable(); String getName(); int getOrdinal(); } public final class PropertyValue{ public String getKey(); public String getValue(); public String get(String key); public Map<String,String> getConfigEntries(); ... }
  28. 28. Configure once, run everywhere6.09.16 28 Are there predefined property sources ?
  29. 29. Configure once, run everywhere6.09.16 29 Of course.
  30. 30. Configure once, run everywhere6.09.16 30 ● System & Environment Properties ● (CLI Arguments) ● Files: ${configDir}/*.properties ● Classpath Resources: /META­INF/javaconfiguration.properties
  31. 31. Configure once, run everywhere6.09.16 31 And how about remote configuration…? Especially with Containers?
  32. 32. Configure once, run everywhere6.09.16 32 ● Configuration is read from remote source, e.g. ● Etcd cluster ● Consul cluster ● Any Web URL ● ... Remote configuration Service Location Layer Configuration Cluster <dependency>     <groupId>org.apache.tamaya.ext</groupId>     <artifactId>tamaya­etcd</artifactId>     <version>...</version> </dependency>
  33. 33. Configure once, run everywhere6.09.16 33 ● In fact, it doesn‘t matter ! Configure once, run everywhere ! What kind of runtime I need ? Configuration Cluster Java EE Tamaya Java SE Tamaya Tamaya Vertx.io Tamaya TomEE Tamaya Spring Tamaya OSGI
  34. 34. Configure once, run everywhere6.09.16 34 ● Configuration on deployment by environment properties: docker run ­e stage prod  ­d ­n MyApp user/image ● or Dockerfile/Docker Image: FROM java:8­jre ... ENV stage prod Excourse: Configuring Containers
  35. 35. Configure once, run everywhere6.09.16 35 How to add custom configuration ?
  36. 36. Configure once, run everywhere6.09.16 36 Other files...
  37. 37. Configure once, run everywhere6.09.16 37 Or resources...
  38. 38. Configure once, run everywhere6.09.16 38 My self-written super fancy config database ?
  39. 39. Configure once, run everywhere6.09.16 39 Whatever I like ?
  40. 40. Configure once, run everywhere6.09.16 40 Use the SPI !
  41. 41. Configure once, run everywhere6.09.16 41 Property sources ● Mostly map to exact one file, resource or backend ● Have a unique name ● Must be thread safe ● Can be dynamic ● Provide an ordinal ● Can be scannable PropertySource
  42. 42. Configure once, run everywhere6.09.16 PropertySource – Example 42 ● Add dependency public class MyPropertySource extends BasePropertySource{     private Map<String,String> props = new HashMap<>();     public SimplePropertySource() throws IOException {         URL url = getClass().getClassLoader().getResource(                    “/META­INF/myFancyConfig.xml“);         // read config properties into props         ...     }     @Override     public String getName() { return “/META­INF/myFancyConfig.xml“; };     @Override     public Map<String, String> getProperties() { return props; } }
  43. 43. Configure once, run everywhere6.09.16 PropertySource - Registration 43 ● Add dependency● By default, register it using the java.util.ServiceLoader      → /META­INF/services/org.apache.tamaya.spi.PropertySource    MyPropertySource
  44. 44. Configure once, run everywhere6.09.16 44 Property source provider ● Allow dynamic registration of multiple property sources ● E.g. all files found in a config directory ● Are evaluated once and then discarded ● Are also registered using the ServiceLoader. PropertySource Provider
  45. 45. Configure once, run everywhere6.09.16 PropertySourceProvider 45 ● Add dependencypublic interface PropertySourceProvider{          public Collection<PropertySource> getPropertySources(); }
  46. 46. Configure once, run everywhere6.09.16 46 Furthermore Tamaya uses ● Filters for filtering values evaluated (remove, map, change) ● Converters for converting String values to non-String types ● A ValueCombinationPolicy ● determines how values evaluated are combined to a final value (defaults to overriding) More SPI artifacts
  47. 47. Configure once, run everywhere6.09.16 47 And how these pieces all fit together ?
  48. 48. Configure once, run everywhere6.09.16 Apache Tamaya in 120 seconds... 48 1.Configuration = ordered list of PropertySources 2.Properties found are combined using a CombinationPolicy 3.Raw properties are filtered by PropertyFilter 4.For typed access PropertyConverters  have to do work 5.Extensions add more features (discussed later) 6.Component Lifecycle is controlled by the ServiceContextManager ConfigurationContext PropertyFilters PropertySource PropertySource PropertySource PropertySource Configuration CombinationPolicy PropertyProviders <provides> PropertyConverter
  49. 49. Configure once, run everywhere6.09.16 49 So we have: files, resources, sys- and env-properties & an SPI to implement and register them ?
  50. 50. Configure once, run everywhere6.09.16 50 What else do we need ?
  51. 51. Configure once, run everywhere6.09.16 51 Easy Configuration: „Meta“-Configuration !
  52. 52. Configure once, run everywhere6.09.16 52 ● Configuration that configures configuration ● E.g. at META­INF/tamaya­config.xml ● Allows easy and quick setup of your configuration environment ● Allows dynamic enablement of property sources ● ... Meta-Configuration DRAFT !
  53. 53. Configure once, run everywhere6.09.16 53 <configuration> <context> <context-param name="stage">DEV</context-param> </context> <sources> <source type="env-properties" enabled="${stage=TEST || stage=PTA || stage=PROD}" ordinal="200"/> <source type="sys-properties" /> <source type="file"> <observe period="20000">true</observe> <location>./config.json</location> </source> <source type="resources" multiple="true"> <multiple>true</multiple> <location>/META-INF/application-config.yml</location> </source> <source type="ch.mypack.MyClassSource"> <locale>de</locale> </source> <source type="includes" enabled="${context.cstage==TEST}"> <include>TEST.properties</include> </source> </sources> </configuration> DRAFT !
  54. 54. Bezeichnung Präsentation6.09.16 Demo
  55. 55. Configure once, run everywhere6.09.16 Demo DEMO 55 ● 1 Microservice ● Running on Java EE 7 (Wildfly) ● Multiple Configuration Sources: ● Environment Properties ● System Properties ● Classpath ● Files ● Etcd Server
  56. 56. Bezeichnung Präsentation6.09.16 There is more! Tamaya Extensions
  57. 57. Configure once, run everywhere6.09.16 57 Property resolution... java.home=/usr/lib/java compiler=${ref:java.home}/bin/javac <dependency>     <groupId>org.apache.tamaya.ext</groupId>     <artifactId>tamaya­resolver</artifactId>     <version>...</version> </dependency>
  58. 58. Configure once, run everywhere6.09.16 58 Resource expressions… public class MyProvider extends AbstractPathPropertySourceProvider{   public MyProvider(){     super(“classpath:/META­INF/config/**/*.properties“);   }   @Override   protected Collection<PropertySource> getPropertySources(URL url) {       // TODO map resource to property sources       return Collections.emptySet();   } } <dependency>     <groupId>org.apache.tamaya.ext</groupId>     <artifactId>tamaya­resources</artifactId>     <version>...</version> </dependency>
  59. 59. Configure once, run everywhere6.09.16 And more: a topic on its own! 59 ● Tamaya-spi-support: Some handy base classes to implement SPIs ● Tamaya-functions: Functional extension points (e.g. remapping, scoping) ● Tamaya-events: Detect and publish ConfigChangeEvents ● Tamaya-optional: Minimal access layer with optional Tamaya support ● Tamaya-filter: Thread local filtering ● Tamaya-inject-api: Tamaya Configuration Injection Annotations ● Tamaya-inject: Configuration Injection and Templates SE Implementation (lean, no CDI) ● Format Extensions: yaml, json, ini, … including formats-SPI ● Integrations with CDI, Spring, OSGI*, Camel, etcd ● Tamaya-mutable-config*: Writable ConfigChangeRequests ● Tamaya-model*: Configuration Model and Auto Documentation ● Tamaya-collections*: Collection Support ● Tamaya-resolver: Expression resolution, placeholders, dynamic values ● Tamaya-resources: Ant styled resource resolution •... * experimental
  60. 60. Bezeichnung Präsentation6.09.16 Summary
  61. 61. Configure once, run everywhere6.09.16 Summarizing... 61 ● A Complete thread- and type-safe Configuration API ● Compatible with all major runtimes ● Simple, but extendible design ● Extensible ● Small footprint ● Base for current Java EE 8 spec ?
  62. 62. Configure once, run everywhere6.09.16 62 You like it ?
  63. 63. Configure once, run everywhere6.09.16 63 „It is your turn !“ ● Use it ● Evangelize it ● Join the force!
  64. 64. Configure once, run everywhere6.09.16 Links ● Project Page: http://tamaya.incubator.apache.org ● Twitter: @tamayaconfig ● Blog: http://javaeeconfig.blogspot.com ● Presentation by Mike Keith on JavaOne 2013: https://oracleus.activeevents.com/2013/connect/sessionDetail.ww?SESSION_ID=7755 ● Apache Deltaspike: http://deltaspike.apache.org ● Java Config Builder: https://github.com/TNG/config-builder ● Apache Commons Configuration: http://commons.apache.org/proper/commons-configuration/ ● Jfig: http://jfig.sourceforge.net/ ● Carbon Configuration: http://carbon.sourceforge.net/modules/core/docs/config/Usage.html ● Comparison on Carbon and Others: http://www.mail-archive.com/commons-dev@jakarta.apache.org/msg37597.html ● Spring Framework: http://projects.spring.io/spring-framework/ ● Owner: http://owner.aeonbits.org/ 64
  65. 65. Configure once, run everywhere6.09.16 Q&A 65 Thank you! @atsticks anatole@apache.org Anatole Tresch Trivadis AG Principal Consultant Twitter/Google+: @atsticks anatole@apache.org anatole.tresch@trivadis.com

×