lagom : reactive microservice framework

78
Lagom Reactive microservices framework #NightClazz @Lagom 12/05/16 Fabrice Sznajderman @fsznajderman

Upload: fabrice-sznajderman

Post on 09-Jan-2017

1.344 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Lagom : Reactive microservice framework

LagomReactive microservices framework

#NightClazz @Lagom12/05/16

Fabrice Sznajderman@fsznajderman

Page 2: Lagom : Reactive microservice framework

Agenda

● Core concepts

● Lagom framework

● Hands on Lagom

Page 3: Lagom : Reactive microservice framework

Who I am?

Fabrice Sznajderman ○ Java/Scala/Web developer

■ Java/Scala trainer■ Co-organizer of Scala IO 2016■ Contributor on Lagom

● BrownBagLunch.fr

Page 4: Lagom : Reactive microservice framework

Chapter 1

CoreConcepts

Page 5: Lagom : Reactive microservice framework

Agenda

● Microservices architecture

● CQRS

● Event Sourcing

Core concepts

Page 6: Lagom : Reactive microservice framework

Core concepts

MicroservicesArchitecture

Page 7: Lagom : Reactive microservice framework

Monolithic vs Microservices

Page 8: Lagom : Reactive microservice framework

Monolithic issues

● All features are concentrated in one app

● One technology used (silver bullet?)

● Complexity rising over time● new comers have difficulties to

dive into code base

Page 9: Lagom : Reactive microservice framework

Microservices-based Architecture

Reactive Microservices Architecture: Design Principles for Distributed Systems - James Boner http://www.oreilly.com/programming/free/reactive-microservices-architecture.html

Microservices-Based Architecture is a simple concept: it advocates creating a system from a collection of small, isolated services, each of which owns their data, and is independently isolated, scalable and resilient to failure. Services integrate with other services in order to form a cohesive system that’s far more flexible than the typical enterprise systems we build today.

Page 10: Lagom : Reactive microservice framework

What is a microservice?Standalone component

Page 11: Lagom : Reactive microservice framework

What is a microservice?Isolated

Page 12: Lagom : Reactive microservice framework

What is a microservice?Own its data

Page 13: Lagom : Reactive microservice framework

What is a microservice?Located microservice

Page 14: Lagom : Reactive microservice framework

What is a microservice?Communication by asynchronous message

Page 15: Lagom : Reactive microservice framework

What is a good microservice ?

Answer with 3 questions ...

Page 16: Lagom : Reactive microservice framework

Question 1Does my service do only one thing?

If you can state a microservice’s full purpose with a short sentence, your service is OK!

“This service manages users’ accounts”

Page 17: Lagom : Reactive microservice framework

Question 2Is my service autonomous?

A service should be responsible for its own behavior. It shouldn’t rely on other services to do its job.

“An autonomous service would accept the any request regardless of the status of others services.”

Page 18: Lagom : Reactive microservice framework

Question 3Does this service own its own data?

A service “owns” data if it is the sole writer and the sole reader of the database where the data lives

“Data can be accessed only throught the service interface, no directly”

Page 19: Lagom : Reactive microservice framework

Anti-pattern

● Adapted use-case?● Transactionnal● Distributed monolithic layers● “Don’t Repeat Yourself”● Shared data repository

Page 20: Lagom : Reactive microservice framework

That implies challenges

● Organisational impacts● Monitoring of services● Locating service● Test strategy● Distributed development

environment

Page 21: Lagom : Reactive microservice framework

Reading

Reactive Microservices Architecture by Jonas Boner

Page 22: Lagom : Reactive microservice framework

CQRS Core concepts

Page 23: Lagom : Reactive microservice framework

CQRS stands for

● Command● Query● Responsability● Segregation

Page 24: Lagom : Reactive microservice framework

Traditional approach

Page 25: Lagom : Reactive microservice framework

Traditional approach

● Using the same model for read and write

● Read & write have different needs○ Representation of data can

take several forms○ Update side contains

validation rules● All together rises complexity● Performance issues

Page 26: Lagom : Reactive microservice framework

Write : Normalization, consistency, transactional...

Read : Denormalization, scalability, performance...

Needs between read and write are not the same.

http://martinfowler.com/bliki/CommandQuerySeparation.html

Facts

Page 27: Lagom : Reactive microservice framework

Command : Change the state of a system but do not return a value

Queries : Return a result and do not change the observable state of the system (are free of side effects)

Principle is that we divide our model into two sharply separated categories:

http://martinfowler.com/bliki/CommandQuerySeparation.html

CQRS approach

Page 28: Lagom : Reactive microservice framework

CQRS approach

Page 29: Lagom : Reactive microservice framework

CQRS approah

● Each model are on ○ separate logicals processes○ separate hardware

● Database :○ shared ○ unique per model

● Unique model with differente interfaces

Several manners to implement

Page 30: Lagom : Reactive microservice framework

Other architecture

Page 31: Lagom : Reactive microservice framework

CQRS approah

● Not a silver bullet, every case doesn’t fit with this CQRS approach.

● Doesn’t apply CQRS on whole system, only be used on specific portions of system (bounded context - DDD)

Cautions

Page 32: Lagom : Reactive microservice framework

Next

CQRS design fits well with Event Sourcing pattern...

Page 33: Lagom : Reactive microservice framework

EventSourcing

Core concepts

Page 34: Lagom : Reactive microservice framework

Traditional approah Active record

Page 35: Lagom : Reactive microservice framework

Traditional approah Active record

Page 36: Lagom : Reactive microservice framework

Traditional approah

● Only final state is captured● Debugability & tracability not

really easy● Complexity with mapping O & R● Performance issue (update)

How we have reached this state?

Active record

Page 37: Lagom : Reactive microservice framework

Event Sourcing approach

● Don't update the current state of objects

● Save the events that lead to the current state

Different approach

Page 38: Lagom : Reactive microservice framework

Event Sourcing approach

● Complete log of every state change

● Debugability & tracability come easy

● Good performance (commit log)

and more …

Benefits

Page 39: Lagom : Reactive microservice framework

● Complete rebuild● Temporal query● Event replay

Event Sourcing approach More benefits

Page 40: Lagom : Reactive microservice framework

● Commands are going to change the state (or not)

● Each state change creates an event● Order of events must be preserved

Event Sourcing approachHow does it works?

Page 41: Lagom : Reactive microservice framework

● Command’s name describe an action to do (imperative)

● Event’s name describe an action in the past

Event Sourcing approachRemarks

Page 42: Lagom : Reactive microservice framework

Event Sourcing approachRestore state into model

Page 43: Lagom : Reactive microservice framework

Event Sourcing approachDeleting an object

Update the query side with deletion of bank account.

Page 44: Lagom : Reactive microservice framework

Event Sourcing approachSnapshot

● Several events will be created● Restore all events to reach one state

might be expensive● To avoid this, we use a snapshot

Page 45: Lagom : Reactive microservice framework

LagomFramework

Chapter 2

Page 46: Lagom : Reactive microservice framework

Agenda

● Big picture of Lagom

● Project structure

● My first microservice

Lagom framework

Page 47: Lagom : Reactive microservice framework

Big pictureof Lagom

Page 48: Lagom : Reactive microservice framework

Big picture of Lagom

First of all, … Lagom pronounce :

Page 49: Lagom : Reactive microservice framework

What is Lagom?

● Microservices framework for a

system of microservices

● Based on the Reactive principles

● A fully integrated development

environment

Page 50: Lagom : Reactive microservice framework

Which language?

● Core of framework written in

Scala

● Developments are in Java

● Scala version migth be available

soon

Page 51: Lagom : Reactive microservice framework

Main features

● Service API

● Persistence API

● Development Environment

● Production Environment

Page 52: Lagom : Reactive microservice framework

Service API

● Interface as descriptor

● Defined API of exposed services

● Synchronous request/response

● Asynchronous streaming

message

Page 53: Lagom : Reactive microservice framework

Persistence API

● Provides event-sourced

persisted entities

● CQRS Read side

● Entry point for events handlers

Page 54: Lagom : Reactive microservice framework

Development Environment

● One command to start all

services

● Hot reload of your code

● Several services provided out of

the box

● IDE Integration

Page 55: Lagom : Reactive microservice framework

Production Environment

● ConductR for production

environment

● Scaling

● Deployment

● Monitoring

Page 56: Lagom : Reactive microservice framework

Communication protocols

● Polyglot systems

● HTTP, WebSocket, JSON are

standards

● can consumed

● can be consumed

Page 57: Lagom : Reactive microservice framework

Component technologies

● Collection of technologies

● Some come from Lightbend

● Others are third-party and

open-source

Page 58: Lagom : Reactive microservice framework

Component technologies

● Java 8 (& Scala)● Immutables● SBT● Jackson● Cassandra● Play framework● Guice● Akka : Persistence, Pub/Sub,

cluster● Akka Stream : Streaming part● SLF4J & Logback

Page 59: Lagom : Reactive microservice framework

● Asynchronous & non blocking as

default

● Distributed persistence : ES /

CQRS as default

● Circuit breaker (as default)

● Productivity for developers :

Expressive service interface

Design philosophy

Page 60: Lagom : Reactive microservice framework

Project Structure

with Lagom

Page 61: Lagom : Reactive microservice framework

Activator

activator sampleProject lagom-java

● Engine provided by Lightbend

● Create project from template

● Template name for Lagom: lagom-java

● Activator must be installed apart

● Generated a new project :

Page 62: Lagom : Reactive microservice framework

Project tree

Page 63: Lagom : Reactive microservice framework

SBT build file

● Project configuration

● Describe all modules

● Describe all dependencies per module

● Host all parameters for project

○ cassandra

○ service locator

○ etc

Page 64: Lagom : Reactive microservice framework

Service declaration

● One service is composed of 2 sub-

projects

○ API

○ Implementation

● Implementation depends on API

Page 65: Lagom : Reactive microservice framework

SBT build fileorganization in ThisBuild := "sample.helloworld"

// the Scala version that will be used for cross-compiled librariesscalaVersion in ThisBuild := "2.11.7"

lazy val helloworldApi = project("helloworld-api") .settings( version := "1.0-SNAPSHOT", libraryDependencies += lagomJavadslApi )

lazy val helloworldImpl = project("helloworld-impl") .enablePlugins(LagomJava) .settings( version := "1.0-SNAPSHOT", libraryDependencies ++= Seq( lagomJavadslPersistence, lagomJavadslTestKit ) ) .settings(lagomForkedTestSettings: _*) .dependsOn(helloworldApi)

Page 66: Lagom : Reactive microservice framework

Service imported

● One service could be imported as

dependency

● Just declare it and it will start as others

Page 67: Lagom : Reactive microservice framework

Launch services

● One command to launch all Lagom’s

service : sbt runAll

● Several services are launched

○ Cassandra

○ Service locator

○ service gateway

○ All services that you declared

Page 68: Lagom : Reactive microservice framework

Launch one service

● One command to launch one Lagom’s

service : sbt project_name/run

● no more services are launched

● One service Locator must be available

apart.

Page 69: Lagom : Reactive microservice framework

Locator service

● All services register on this locator

● By default, one service locator is started

● It can be started apart

Page 70: Lagom : Reactive microservice framework

Cassandra embedded

● One instance of Cassandra is started by

default

● It can be unactivate if no persistence is

required

● It can be started apart

● Cassandra’s client can be used to

explore tables

Page 71: Lagom : Reactive microservice framework

Mapping of one Service

Page 72: Lagom : Reactive microservice framework

Write a servicewith Lagom

Page 73: Lagom : Reactive microservice framework

Demo

Here, we will see :

○ Global configuration of project

○ Service Interface - Descriptor

○ Related implementation

○ All stuffs linked with persistence (CQRS-EventSourcing)

Page 74: Lagom : Reactive microservice framework

Just an information !

● 5 days ago, the API has change !!!

ServiceCall<Id,Request, Response>

interface ServiceCall<Request, Response> {

CompletionStage<Response> invoke(Request request);

//others methods

}

Page 75: Lagom : Reactive microservice framework

Ok, now we can dive into dark side...code! ;)

Page 76: Lagom : Reactive microservice framework

Thank you!Next …

Page 77: Lagom : Reactive microservice framework

Hands on!Chapter 3

Page 78: Lagom : Reactive microservice framework