Microservices:
Klein, aber oho!

Stephan Hochdörfer // 19.04.2016

Über mich

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

  • #PHP, #DevOps, #Automation
  • #phpugffm, #phpugmrn, #unKonf

Der Fluch des Monolithen

Einschüchternde Codebasis

Fragiles System

Langfristige Verpflichtung

Versuch eines Rewrites?

Die Legacy Wahrheit




« Legacy code is a business asset that helped
the business get where it is. Treat it with respect. »
- @ylarrivee

Die LTS Wahrheit




« good long term software is not commonly about reuse,
its about the ability to rewrite completely isolated bits
without hitting the big bang » - @gregyoung

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?




« I guess it is easier to use a new name (Microservices)
rather than say that this is what SOA actually meant [...] »
- @arnonrgo

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

Herausforderung




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

Nicht so einfach!




« If you can’t build a monolith, what makes you think
microservices are the answer? » - Simon Brown

Kleine Services FTW!




« Small teams typically work better than big teams.
Small services typically work better than monoliths.
#microservices #microservice » - @microsvcs

Was heißt denn "klein"?




« @olivergierke Micro Services are neither
micro nor service. :-) » - @ewolff

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




« @olivergierke No. I meant to says that REST is not the
most difficult problem for Microservices. E.g. resilience
is probably harder. » - @ewolff

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

$> composer.phar require zendframework/zend-diactoros
$> composer.phar require zendframework/zend-stratigility
use Zend\Stratigility\MiddlewarePipe;
use Zend\Stratigility\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Zend\Diactoros\Response\JsonResponse;

class SearchMiddleware implements MiddlewareInterface
{
    public function __invoke(Request $request, Response $response,
        callable $next = null)
    {
        $params = $request->getQueryParams();
        $service = new SearchService();
        return new JsonResponse($service->search($params['search']));
    }
}

Microservices Beispiel

use Zend\Stratigility\MiddlewarePipe;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;

$api = new MiddlewarePipe();
$api->pipe(new SearchMiddleware());
$response = $api(ServerRequestFactory::fromGlobals(), new Response());

Extending your Microservice

$> composer.phar require oscarotero/psr7-middlewares
use Zend\Stratigility\MiddlewarePipe;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;
use Psr7Middlewares\Middleware\Firewall;

$api = new MiddlewarePipe();
$api->pipe(new Firewall(['127.0.0.1']));
$api->pipe(new SearchMiddleware());
$response = $api(ServerRequestFactory::fromGlobals(), new Response());







Vielen Dank! Fragen?