Top Banner
@crichardson Microservices in Java and Scala Chris Richardson Founder of Eventuate.io Founder of the original CloudFoundry.com Author of POJOs in Action @crichardson [email protected] http://microservices.io http://eventuate.io http://plainoldobjects.com Copyright © 2015. Chris Richardson Consulting, Inc. All rights reserved
59

Microservices in Java and Scala (sfscala)

Apr 16, 2017

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Microservices in Java and Scala (sfscala)

@crichardson

Microservices in Java and Scala

Chris Richardson

Founder of Eventuate.io Founder of the original CloudFoundry.com Author of POJOs in Action

@crichardson [email protected] http://microservices.io http://eventuate.io http://plainoldobjects.com

Copyright © 2015. Chris Richardson Consulting, Inc. All rights reserved

Page 2: Microservices in Java and Scala (sfscala)

@crichardson

About Chris

Page 3: Microservices in Java and Scala (sfscala)

@crichardson

About Chris

Consultant and trainer focusing on

microservices (public class: April 28th, Oakland, CA)

http://www.chrisrichardson.net/

Page 4: Microservices in Java and Scala (sfscala)

@crichardson

About Chris

Founder of a startup that is creating a platform that makes it easy for

application developers write microservices

(http://eventuate.io)

Page 5: Microservices in Java and Scala (sfscala)

@crichardson

For more information

http://bit.ly/eventsmarch17

Page 6: Microservices in Java and Scala (sfscala)

AgendaWhy a pattern language for microservices?

Monolith architecture vs. microservices

Developing microservices with event sourcing and CQRS

Microservices in Java

Microservices in Scala Example: real-time, collaborative Kanban board application

Page 7: Microservices in Java and Scala (sfscala)

@crichardson

In 1986…

http://en.wikipedia.org/wiki/Fred_Brooks

Page 8: Microservices in Java and Scala (sfscala)

@crichardson

Yet 30 years later….

If you __________________ a puppy will die Therefore you must _______________

Page 9: Microservices in Java and Scala (sfscala)

@crichardson

Quiz - fill in the blanks….

mutate state

use monads

use objects

use functions

block a thread

use async.

make a REST call

send a messageuse Spring

use ….

Page 10: Microservices in Java and Scala (sfscala)

@crichardson

How we make decisions

Decide using

emotions

Rationalize with our intellect

http://en.wikipedia.org/wiki/Mahout

Page 11: Microservices in Java and Scala (sfscala)

@crichardson

The structure of a pattern

=

Great framework for discussing and thinking about technology

Page 12: Microservices in Java and Scala (sfscala)

@crichardson

The structure of a pattern

Resulting context

aka the situation

Name

Context

Problem

Related patterns

(conflicting) issues etc to address Forces

Solution

Page 13: Microservices in Java and Scala (sfscala)

@crichardson

Page 14: Microservices in Java and Scala (sfscala)

AgendaWhy a pattern language for microservices?

Monolith architecture vs. microservices

Developing microservices with event sourcing and CQRS

Microservices in Java

Microservices in Scala Example: real-time, collaborative Kanban board application

Page 15: Microservices in Java and Scala (sfscala)

@crichardson

Page 16: Microservices in Java and Scala (sfscala)

In theory:

We can build a modular monolith

But in practice:

We build a big ball of mud

Page 17: Microservices in Java and Scala (sfscala)

Microservices are not a silver bullet but …

Page 18: Microservices in Java and Scala (sfscala)

@crichardson

The benefits typically outweigh the drawbacks

for large, complex applications

Page 19: Microservices in Java and Scala (sfscala)

@crichardson

Build and deliver better software faster

Page 20: Microservices in Java and Scala (sfscala)

@crichardson

Easily try other technologies

... and fail safely

Page 21: Microservices in Java and Scala (sfscala)

AgendaWhy a pattern language for microservices?

Monolith architecture vs. microservices

Developing microservices with event sourcing and CQRS

Microservices in Java

Microservices in Scala Example: real-time, collaborative Kanban board application

Page 22: Microservices in Java and Scala (sfscala)

Data management patterns

Database per Service

Event-driven architecture

Shared database

Event sourcing

Transaction log tailing

Database triggers

Application events

CQRS

Database architecture

Page 23: Microservices in Java and Scala (sfscala)

Database per Service

Orders Service Customer Service

Order Database

Customer Database

Sharded SQL

NoSQL DB

Page 24: Microservices in Java and Scala (sfscala)

@crichardson

Customer management

How to maintain invariants?

Order management

Order Service

placeOrder()

Customer Service

updateCreditLimit()

Customer

creditLimit ...

has ordersbelongs toOrder

total

Invariant: sum(open order.total) <= customer.creditLimit

?

Page 25: Microservices in Java and Scala (sfscala)

@crichardson

Event-driven architecture

Page 26: Microservices in Java and Scala (sfscala)

@crichardson

How atomically update database and publish an event

Order Service

Order Database

Message Broker

insert Order

publish OrderCreatedEvent

dual write problem

?

Page 27: Microservices in Java and Scala (sfscala)

@crichardson

Reliably generating events

Page 28: Microservices in Java and Scala (sfscala)

@crichardson

Use event-sourcingEvent table

Aggregate type

Event id

Aggregate id

Event data

Order 902101 …OrderApproved

Order 903101 …OrderShipped

Event type

Order 901101 …OrderCreated

Page 29: Microservices in Java and Scala (sfscala)

@crichardson

Replay events to recreate state

Order

state

OrderCreated(…) OrderAccepted(…) OrderShipped(…)

Events

Periodically snapshot to avoid loading all events

Page 30: Microservices in Java and Scala (sfscala)

But what about queries?

Page 31: Microservices in Java and Scala (sfscala)

@crichardson

Command Query Responsibility Segregation (CQRS)

Command side

Commands

Aggregate

Event Store

Events

Query side

Queries

(Materialized) View

Events

Page 32: Microservices in Java and Scala (sfscala)

@crichardson

Query-side design

Event Store

Updater

View Updater Service

Events

Reader

HTTP GET Request

View Query Service

View Store

e.g. MongoDB

Neo4J CloudSearch

update query

Page 33: Microservices in Java and Scala (sfscala)

Eventuate architecture

Page 34: Microservices in Java and Scala (sfscala)

Eventuate platform

Page 35: Microservices in Java and Scala (sfscala)

Multiple flavors of client frameworks

“Traditional Java” mutable object-oriented domain objects

https://github.com/cer/event-sourcing-examples/tree/master/java-spring

Functional Scala with immutable domain objects

https://github.com/cer/event-sourcing-using-scala-typeclasses

Hybrid OO/Functional Scala with immutable domain objects

https://github.com/cer/event-sourcing-examples/tree/master/scala-spring

Page 36: Microservices in Java and Scala (sfscala)

AgendaWhy a pattern language for microservices?

Monolith architecture vs. microservices

Developing microservices with event sourcing and CQRS

Microservices in Java

Microservices in Scala Example: real-time, collaborative Kanban board application

Page 37: Microservices in Java and Scala (sfscala)

Customer command side

Page 38: Microservices in Java and Scala (sfscala)

@crichardson

The Customer aggregate

creditLimit creditReservations : Map<OrderId, Money>

Customer

List<Event> process(CreateCustomerCommand cmd) { … } List<Event> process(ReserveCreditCommand cmd) { … } … void apply(CustomerCreatedEvent anEvent) { … } void apply(CreditServedEvent anEvent) { … } …

State

Behavior

Page 39: Microservices in Java and Scala (sfscala)

@crichardson

Familiar concepts restructured

class Customer {

public void reserveCredit( orderId : String, amount : Money) {

// verify

// update state this.xyz = … }

public List<Event> process( ReserveCreditCommand cmd) { // verify … return … new CreditReservedEvent(); }

public void apply( CreditReservedEvent event) { // update state this.xyz = event.xyz }

Page 40: Microservices in Java and Scala (sfscala)

@crichardson

Customer command processing

Page 41: Microservices in Java and Scala (sfscala)

@crichardson

Customer applying events

Page 42: Microservices in Java and Scala (sfscala)

@crichardson

Creating an order

save() concisely specifies: 1.Creates Order aggregate 2.Processes command 3.Applies events 4.Persists events

Page 43: Microservices in Java and Scala (sfscala)

@crichardson

Event handling in Customers

1. Load Customer aggregate 2. Processes command 3. Applies events 4. Persists events

Triggers BeanPostProcessor Durable subscription name

Page 44: Microservices in Java and Scala (sfscala)

Customer - query side

Page 45: Microservices in Java and Scala (sfscala)

@crichardson

MongoDB view: customer and their order history

{ "_id" : "0000014f9a45004b 0a00270000000000", "_class" : "net.chrisrichardson…..views.orderhistory.CustomerView", "version" : NumberLong(5), "orders" : { "0000014f9a450063 0a00270000000000" : { "state" : "APPROVED", "orderId" : "0000014f9a450063 0a00270000000000", "orderTotal" : { "amount" : "1234" } }, "0000014f9a450063 0a00270000000001" : { "state" : "REJECTED", "orderId" : "0000014f9a450063 0a00270000000001", "orderTotal" : { "amount" : "3000" } } }, "name" : "Fred", "creditLimit" : { "amount" : "2000" } }

Denormalized = efficient lookup

Page 46: Microservices in Java and Scala (sfscala)

@crichardson

Query-side event handler that updates customer view

Page 47: Microservices in Java and Scala (sfscala)

@crichardson

Updating and query view using Spring Data for MongoDB...

Page 48: Microservices in Java and Scala (sfscala)

AgendaWhy a pattern language for microservices?

Monolith architecture vs. microservices

Developing microservices with event sourcing and CQRS

Microservices in Java

Microservices in Scala Example: real-time, collaborative Kanban board application

Page 49: Microservices in Java and Scala (sfscala)

@crichardson

Functional Customer aggregate

Customer

creditLimit creditReservations …

CustomerAggregate

processCommand(Account, Command) : Seq[Events]

applyEvent(Account, Event) : Account

Immutable state Behavior

Page 50: Microservices in Java and Scala (sfscala)

@crichardson

Aggregate type classesUsed by

Event Store to

reconstitute aggregates

Hardwired

Page 51: Microservices in Java and Scala (sfscala)

@crichardson

Customer Aggregate….State

Behavior

Page 52: Microservices in Java and Scala (sfscala)

@crichardson

…command processing…

Page 53: Microservices in Java and Scala (sfscala)

@crichardson

… applying events

Page 54: Microservices in Java and Scala (sfscala)

AgendaWhy a pattern language for microservices?

Monolith architecture vs. microservices

Developing microservices with event sourcing and CQRS

Microservices in Java

Microservices in Scala Example: real-time, collaborative Kanban board application

Page 55: Microservices in Java and Scala (sfscala)

@crichardson

Kanban board example

Page 56: Microservices in Java and Scala (sfscala)

@crichardson

ArchitectureCreate/update boards

and tasks

Change notifications Materialized views

Event Store

Page 57: Microservices in Java and Scala (sfscala)

@crichardson

Demo

Page 58: Microservices in Java and Scala (sfscala)

Summary

Microservices are not a silver bullet but they are the best choice for large/complex applications

Use an event-driven microservices architecture

Build services using event sourcing + CQRS

Using a language/framework specific programming model

Page 59: Microservices in Java and Scala (sfscala)

@crichardson

@crichardson [email protected]

http://bit.ly/eventsmarch17

Questions?