Configure once, run everywhere 2016

65
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

Transcript of Configure once, run everywhere 2016

Page 1: Configure once, run everywhere 2016

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!Configurationwith ApacheTamaya

Page 2: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16

Anatole TreschPrincipal Consultant, Trivadis AG (Switzerland) Star Spec LeadTechnical Architect, Lead EngineerPPMC Member Apache Tamaya

@[email protected]@trivadis.com

2

About Me

Page 3: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16

Agenda

3

● Motivation

● Requirements

● The API

● Configuration Backends

● Demo

● Extensions

Page 4: Configure once, run everywhere 2016

Configure once, run everywhere6.09.164

Motivation

Page 5: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 5

What is Configuration ?

Simple Key/value pairs?

Typed values?

Page 6: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 6

When is Configuration useful?

Use Cases?

Page 7: Configure once, run everywhere 2016

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) ?

Page 8: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 8

When to configure?

Develpment time ?Build/deployment time?

Startup?Dynamic, anytime?

Page 9: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 9

Configuration Lifecycle ?

Static ?Refreshing ?

Changes triggered ?

Page 10: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 10

Do I need a runtime ?

Java SE?Java EE?

OSGI?

Page 11: Configure once, run everywhere 2016

Bezeichnung Präsentation6.09.16

Requirements

Page 12: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 12

● Developer‘s Perspective

● Architectural/Design Requirements

● Operational Aspects

● Other Aspects

Requirements

Page 13: Configure once, run everywhere 2016

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

Page 14: Configure once, run everywhere 2016

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

Page 15: Configure once, run everywhere 2016

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

Page 16: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 16

● Support Access Constraints and Views

● No accidential logging of secrets

● Dynamic changes

● Configuration Validation

Other Aspects

Page 17: Configure once, run everywhere 2016

Bezeichnung Präsentation6.09.16

Accessing Configuration

The API

Page 18: Configure once, run everywhere 2016

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

Page 19: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 19

● Environment Properties

● System Properties

● CLI arguments

● Properties, xml-Properties

Existing Mechanisms

Page 20: Configure once, run everywhere 2016

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> 

Page 21: Configure once, run everywhere 2016

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 accessString name = config.getOrDefault("name", "John");int ChildNum = config.get("childNum", int.class);

// Multi property accessMap<String,String> properties = config.getProperties();

// Templates (provided by extension)MyConfig config = ConfigurationInjection.getConfigurationInjector()                    .getConfig(MyConfig.class);

Page 22: Configure once, run everywhere 2016

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> 

Page 23: Configure once, run everywhere 2016

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);

Page 24: Configure once, run everywhere 2016

Bezeichnung Präsentation6.09.16

Configuration Backends

Page 25: Configure once, run everywhere 2016

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

Page 26: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 26

So what is a property source ?

Page 27: Configure once, run everywhere 2016

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(); ...}

Page 28: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 28

Are there predefined property sources ?

Page 29: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 29

Of course.

Page 30: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 30

● System & Environment Properties

● (CLI Arguments)

● Files: ${configDir}/*.properties

● Classpath Resources: /META­INF/javaconfiguration.properties

Page 31: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 31

And how about remote configuration…?

Especially with Containers?

Page 32: Configure once, run everywhere 2016

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>

Page 33: Configure once, run everywhere 2016

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 EETamaya

Java SETamaya

Tamaya

Vertx.io

TamayaTomEE

Tamaya

Spring

Tamaya

OSGI

Page 34: Configure once, run everywhere 2016

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

Page 35: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 35

How to add custom configuration ?

Page 36: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 36

Other files...

Page 37: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 37

Or resources...

Page 38: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 38

My self-written super fancy config database ?

Page 39: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 39

Whatever I like ?

Page 40: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 40

Use the SPI !

Page 41: Configure once, run everywhere 2016

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

Page 42: Configure once, run everywhere 2016

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; }}

Page 43: Configure once, run everywhere 2016

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

Page 44: Configure once, run everywhere 2016

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

Page 45: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16

PropertySourceProvider

45

● Add dependency public interface PropertySourceProvider{        public Collection<PropertySource> getPropertySources();

}

Page 46: Configure once, run everywhere 2016

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

Page 47: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 47

And how these pieces all fit together ?

Page 48: Configure once, run everywhere 2016

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 PropertyFilter4.For typed access PropertyConverters 

have to do work5.Extensions add more features

(discussed later)6.Component Lifecycle is controlled by the ServiceContextManager

ConfigurationContext

PropertyFilters

PropertySource

PropertySource

PropertySource

PropertySource

Configuration

Com

bina

tionP

olic

yPropertyProviders<provides>

PropertyConverter

Page 49: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 49

So we have:

files, resources, sys- and env-properties

& an SPI to implement and register them ?

Page 50: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 50

What else do we need ?

Page 51: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16 51

Easy Configuration:

„Meta“-Configuration !

Page 52: Configure once, run everywhere 2016

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 !

Page 53: Configure once, run everywhere 2016

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 !

Page 54: Configure once, run everywhere 2016

Bezeichnung Präsentation6.09.16

Demo

Page 55: Configure once, run everywhere 2016

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

Page 56: Configure once, run everywhere 2016

Bezeichnung Präsentation6.09.16

There is more!

Tamaya Extensions

Page 57: Configure once, run everywhere 2016

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>

Page 58: Configure once, run everywhere 2016

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>

Page 59: Configure once, run everywhere 2016

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

Page 60: Configure once, run everywhere 2016

Bezeichnung Präsentation6.09.16

Summary

Page 61: Configure once, run everywhere 2016

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 ?

Page 62: Configure once, run everywhere 2016

Configure once, run everywhere6.09.1662

You like it ?

Page 63: Configure once, run everywhere 2016

Configure once, run everywhere6.09.1663

„It is your turn !“

● Use it● Evangelize it● Join the force!

Page 64: Configure once, run everywhere 2016

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/[email protected]/msg37597.html ● Spring Framework: http://projects.spring.io/spring-framework/● Owner: http://owner.aeonbits.org/

64

Page 65: Configure once, run everywhere 2016

Configure once, run everywhere6.09.16

Q&A

65

Thank you! @atsticks [email protected]

Anatole TreschTrivadis AGPrincipal ConsultantTwitter/Google+: @atsticks [email protected] [email protected]