Distributing Configuration with Apache Tamaya
Total Page:16
File Type:pdf, Size:1020Kb
Configure Once, run everyhere! Configuration with Apache Tamaya BASEL BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENF HAMBURG KOPENHAGEN LAUSANNE MÜNCHEN STUTTGART WIEN ZÜRICH About Me Anatole Tresch Principal Consultant, Trivadis AG (Switzerland) Star Spec Lead Technical Architect, Lead Engineer PPMC Member Apache Tamaya @atsticks [email protected] [email protected] 6.09.16 Configure once, run everywhere 2 Agenda ● Motivation ● Requirements ● The API ● Configuration Backends ● Demo ● Extensions 6.09.16 Configure once, run everywhere 3 Motivation 6.09.16 Configure once, run everywhere 4 What is Configuration ? Simple Key/value pairs? Typed values? 6.09.16 Configure once, run everywhere 5 When is Configuration useful? Use Cases? 6.09.16 Configure once, run everywhere 6 How is it stored? Remotely or locally? Classpath, file or ...? Which format? All of the above (=multiple sources) ? 6.09.16 Configure once, run everywhere 7 When to configure? Develpment time ? Build/deployment time? Startup? Dynamic, anytime? 6.09.16 Configure once, run everywhere 8 Configuration Lifecycle ? Static ? Refreshing ? Changes triggered ? 6.09.16 Configure once, run everywhere 9 Do I need a runtime ? Java SE? Java EE? OSGI? 6.09.16 Configure once, run everywhere 10 Requirements 6.09.16 Bezeichnung Präsentation Requirements ● Developer‘s Perspective ● Architectural/Design Requirements ● Operational Aspects ● Other Aspects 6.09.16 Configure once, run everywhere 12 Developer‘s Requirements ● 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 6.09.16 Configure once, run everywhere 13 Architectural/Design Requirements Decouple code that consumes configuration from ● Backends Used ● Storage Format ● Distribution ● Lifecycle and versioning ● Security Aspects 6.09.16 Configure once, run everywhere 14 Operational‘s Requirements ● 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 6.09.16 Configure once, run everywhere 15 Other Aspects ● Support Access Constraints and Views ● No accidential logging of secrets ● Dynamic changes ● Configuration Validation 6.09.16 Configure once, run everywhere 16 Accessing Configuration The API 6.09.16 Bezeichnung Präsentation API Requirements ● 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) 6.09.16 Configure once, run everywhere 18 Existing Mechanisms ● Environment Properties ● System Properties ● CLI arguments ● Properties, xml-Properties 6.09.16 Configure once, run everywhere 19 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> 6.09.16 Configure once, run everywhere 20 Programmatic API ● Configuration Add dependency config = org.apache.tamaya:core: ConfigurationProvider. 0.2-incubatinggetConfiguration(); ● // Add single Config property to META-INF/javaconfiguration.properties access String● name = config.getOrDefault("name", "John"); GO! 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); 6.09.16 Configure once, run everywhere 21 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> 6.09.16 Configure once, run everywhere 22 @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ª) MyTenantMyTenant tt == newnew MyTenant();MyTenant(); ConfigurationInjectionConfigurationInjection private Address address; .getConfigurationInjector().getConfigurationInjector() .configure(t);.configure(t); 6.09.16 Configure once, run everywhere 23 Configuration Backends 6.09.16 Bezeichnung Präsentation Configuration Backends ● 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 6.09.16 Configure once, run everywhere 25 So what is a property source ? 6.09.16 Configure once, run everywhere 26 PropertySource public interface PropertySource { ● Add dependency org.apache.tamaya:core:PropertyValue get(String key); 0.2-incubating Map<String,String> getProperties(); ● Addboolean Config isScannable(); to META-INF/java StringMap<String,String> getName(); getProperties();configuration.properties int getOrdinal(); ● GO! } public final class PropertyValue{ public String getKey(); public String getValue(); public String get(String key); public Map<String,String> getConfigEntries(); ... } 6.09.16 Configure once, run everywhere 27 Are there predefined property sources ? 6.09.16 Configure once, run everywhere 28 Of course. 6.09.16 Configure once, run everywhere 29 ● System & Environment Properties ● (CLI Arguments) ● Files: ${configDir}/*.properties ● Classpath Resources: /META-INF/javaconfiguration.properties 6.09.16 Configure once, run everywhere 30 And how about remote configuration…? Especially with Containers? 6.09.16 Configure once, run everywhere 31 <dependency> <groupId>org.apache.tamaya.ext</groupId> <artifactId>tamaya-etcd</artifactId> Remote configuration <version>...</version> </dependency> ● Configuration is read from remote source, e.g. ● Etcd cluster ● Consul cluster ● Any Web URL Service Location Layer ● ... Configuration Cluster 6.09.16 Configure once, run everywhere 32 What kind of runtime I need ? ● In fact, it doesn‘t matter ! Configure once, run everywhere ! Java EE Vertx.io Spring Tamaya Tamaya Tamaya Java SE TomEE OSGI Tamaya Tamaya Tamaya Configuration Cluster 6.09.16 Configure once, run everywhere 33 Excourse: Configuring Containers ● 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 6.09.16 Configure once, run everywhere 34 How to add custom configuration ? 6.09.16 Configure once, run everywhere 35 Other files... 6.09.16 Configure once, run everywhere 36 Or resources... 6.09.16 Configure once, run everywhere 37 My self-written super fancy config database ? 6.09.16 Configure once, run everywhere 38 Whatever I like ? 6.09.16 Configure once, run everywhere 39 Use the SPI ! 6.09.16 Configure once, run everywhere 40 PropertySource 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 6.09.16 Configure once, run everywhere 41 PropertySource – Example public class MyPropertySource extends BasePropertySource{ ● Add private dependency 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; } } 6.09.16 Configure once, run everywhere 42 PropertySource - Registration ●● ByAdd default, dependency register it using the java.util.ServiceLoader → /META-INF/services/org.apache.tamaya.spi.PropertySource MyPropertySource 6.09.16 Configure once, run everywhere 43 PropertySource Provider 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. 6.09.16 Configure once, run everywhere 44 PropertySourceProvider ● public Add interface dependency PropertySourceProvider{ public Collection<PropertySource> getPropertySources(); } 6.09.16 Configure once, run everywhere 45 More SPI artifacts 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) 6.09.16 Configure once, run everywhere 46 And how these pieces all fit together ? 6.09.16 Configure once, run everywhere 47 Apache Tamaya in 120 seconds... Configuration ConfigurationContext 1.Configuration = ordered list of PropertyConverter PropertySources 2.Properties found are combined using a PropertyFilters CombinationPolicy PropertyProviders y c i 3.Raw properties are filtered by PropertyFilter <provides> l o 4.For typed access PropertyConverters PropertySource P n o have to do work i t 5.Extensions add more features PropertySource a n i (discussed later) PropertySource