Microservices:
Klein, aber oho!

Stephan Hochdörfer // 15.10.2015

Über mich

  • Stephan Hochdörfer
  • Head of Technology, bitExpert AG
    Mannheim
  • S.Hochdoerfer@bitExpert.de
  • @shochdoerfer

  • #DevOps, #Automation, #unKonf

Der Fluch des Monolithen

Einschüchternde Codebasis

Fragiles System

Langfristige Verpflichtung

Versuch eines Rewrites?

Die Legacy Wahrheit



Die LTS Wahrheit



Die Lösung?




« [...] This is the Unix philosophy: Write
programs that do one thing and do it well. »
- Doug McIlroy

Zusammenarbeit




« [...] Write programs to work together. Write programs to
handle text streams, because that is a universal interface. »
- Doug McIlroy

Was sind Microservices?




« [...] approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms. »
- James Lewis, Martin Fowler

"Nur" SOA? Oder besser?



Kleine Problemdomäne

Single Responsibility




« A class should have one, and
only one, reason to change. »
- Robert C. Martin

Isolierter Prozess

Docker



Microservices Plattform

Deployment Automatisierung

Build + Deployment isolieren

Separater Datenspeicher

Einfache Kommunikation

Smart endpoints, dumb pipes




« Be of the web, not behind the web. »
- Ian Robinson

Open/closed principle




« You should be able to extend a classes
behavior, without modifying it. »
- Robert C. Martin

Datenstruktur-Evolution

{
  "id": 123,
  "name": "My Product",
  "categories": [{
    "id": 12,
    "name": "My category"
  }],
  "price": 12.34
  "currency": "EUR"
}
{
  "id": 123,
  "name": "My Product",
  "categories": [{
    "id": 12,
    "name": "My category"
  }],
  "price": [12.34, 14.26]
  "currency": ["EUR", "USD"]
}

Datenstruktur-Evolution

{
  "id": 123,
  "name": "My Product",
  "categories": [{
    "id": 12,
    "name": "My category"
  }],
  "prices": [{
    "price": 12.34,
    "currency": "EUR"
  }, {
    "price": 14.26,
    "currency": "USD"
  }]
}

Bauen, Deployen, Supporten

Warum Microservices?




« Microservices are organised around
business capabilities. » - Martin Fowler

Observe, Orient, Decide, Act

Team Organisation

Kleine, fokussierte Teams

Mehr Macht den Entwicklern!

Nutze das "passende" Tool...

...oder tausche es aus!

Liskov substitution principle




« Derived classes must be substitutable
for their base classes. » - Robert C. Martin

Skalierung wird einfacher!

Entwicklung wird schneller...

...je nach Bedarf!

A/B Testing für Services

Feature toggles

Herausforderungen




« There ain’t no such thing as a free lunch. »
- Robert A. Heinlein

Nicht so einfach!



Kleine Services FTW!



Was heißt denn "klein"?



Interface Segregation




« Make fine grained interfaces that
are client specific. » - Robert C. Martin

Standardisierung notwendig!

Monitoring und Logging

Keine verteilte Transaktionen!

Keine kaskadierende Fehler!

Design for failure

Design for failure



Resilience

Früh und oft releasen!

Automatisierung FTW!

Dependency Inversion




« Depend on abstractions, not on concretions. »
- Robert C. Martin

Service Discovery


Service Discovery Beispiel

{
  "require": {
    "sensiolabs/consul-php-sdk": "1.0.*@dev"
  }
}
<?php
require_once __DIR__.'/vendor/autoload.php';

$sf = new SensioLabs\Consul\ServiceFactory();
$kv = $sf->get('kv');

$kv->put('test/foo/bar', 'bazinga');
$kv->get('test/foo/bar', ['raw' => true]);
$kv->delete('test/foo/bar');

Dokumentation FTW!

Microservices Beispiel

Microservices Beispiel

{
  "require": {
    "silex/silex": "~1.2@stable"
  }
}
<?php
require_once __DIR__.'/vendor/autoload.php';

$app = new Silex\Application();
$app->register(new MyApp\Service\SearchServiceProvider());

$app->get('/search/{term}', function($term) use($app) {

    $result = $app['search']->search($term);
    return $app->json($result);
});

$app->run();

Composing your microservice

Microservices Beispiel

<?php
require_once __DIR__.'/vendor/autoload.php';

$app = new Silex\Application();
$app->register(new MyApp\Service\SearchServiceProvider());

$app->get('/search/{term}', function($term) use($app) {

    $result = $app['search']->search($term);
    return $app->json($result);
});

$app = new Alsar\Stack\IpRestrict($app, ['127.0.0.1']);

$app->run();







Vielen Dank! Fragen?







Feedback erwünscht:
https://joind.in/15056