Configure once, run everywhere 2016

Post on 16-Apr-2017

201 views 5 download

Transcript of 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

Configure once, run everywhere6.09.16

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

@atsticksanatole@apache.organatole.tresch@trivadis.com

2

About Me

Configure once, run everywhere6.09.16

Agenda

3

● Motivation

● Requirements

● The API

● Configuration Backends

● Demo

● Extensions

Configure once, run everywhere6.09.164

Motivation

Configure once, run everywhere6.09.16 5

What is Configuration ?

Simple Key/value pairs?

Typed values?

Configure once, run everywhere6.09.16 6

When is Configuration useful?

Use Cases?

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

Configure once, run everywhere6.09.16 8

When to configure?

Develpment time ?Build/deployment time?

Startup?Dynamic, anytime?

Configure once, run everywhere6.09.16 9

Configuration Lifecycle ?

Static ?Refreshing ?

Changes triggered ?

Configure once, run everywhere6.09.16 10

Do I need a runtime ?

Java SE?Java EE?

OSGI?

Bezeichnung Präsentation6.09.16

Requirements

Configure once, run everywhere6.09.16 12

● Developer‘s Perspective

● Architectural/Design Requirements

● Operational Aspects

● Other Aspects

Requirements

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

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

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

Configure once, run everywhere6.09.16 16

● Support Access Constraints and Views

● No accidential logging of secrets

● Dynamic changes

● Configuration Validation

Other Aspects

Bezeichnung Präsentation6.09.16

Accessing Configuration

The API

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

Configure once, run everywhere6.09.16 19

● Environment Properties

● System Properties

● CLI arguments

● Properties, xml-Properties

Existing Mechanisms

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> 

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

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> 

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

Bezeichnung Präsentation6.09.16

Configuration Backends

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

Configure once, run everywhere6.09.16 26

So what is a property source ?

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

Configure once, run everywhere6.09.16 28

Are there predefined property sources ?

Configure once, run everywhere6.09.16 29

Of course.

Configure once, run everywhere6.09.16 30

● System & Environment Properties

● (CLI Arguments)

● Files: ${configDir}/*.properties

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

Configure once, run everywhere6.09.16 31

And how about remote configuration…?

Especially with Containers?

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>

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

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

Configure once, run everywhere6.09.16 35

How to add custom configuration ?

Configure once, run everywhere6.09.16 36

Other files...

Configure once, run everywhere6.09.16 37

Or resources...

Configure once, run everywhere6.09.16 38

My self-written super fancy config database ?

Configure once, run everywhere6.09.16 39

Whatever I like ?

Configure once, run everywhere6.09.16 40

Use the SPI !

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

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

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

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

Configure once, run everywhere6.09.16

PropertySourceProvider

45

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

}

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

Configure once, run everywhere6.09.16 47

And how these pieces all fit together ?

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

Configure once, run everywhere6.09.16 49

So we have:

files, resources, sys- and env-properties

& an SPI to implement and register them ?

Configure once, run everywhere6.09.16 50

What else do we need ?

Configure once, run everywhere6.09.16 51

Easy Configuration:

„Meta“-Configuration !

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 !

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 !

Bezeichnung Präsentation6.09.16

Demo

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

Bezeichnung Präsentation6.09.16

There is more!

Tamaya Extensions

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>

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>

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

Bezeichnung Präsentation6.09.16

Summary

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 ?

Configure once, run everywhere6.09.1662

You like it ?

Configure once, run everywhere6.09.1663

„It is your turn !“

● Use it● Evangelize it● Join the force!

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

Configure once, run everywhere6.09.16

Q&A

65

Thank you! @atsticks anatole@apache.org

Anatole TreschTrivadis AGPrincipal ConsultantTwitter/Google+: @atsticks anatole@apache.org anatole.tresch@trivadis.com