Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Microservices in Java and Scala (sfscala)

2 107 vues

Publié le

This is a presentation I gave at SF Scala.
I describe the motivations for having a pattern language for microservices.
I then describe how to build event-driven microservices using event sourcing and CQRS.
I show some Java and Scala code examples.

Publié dans : Logiciels

Microservices in Java and Scala (sfscala)

  1. 1. @crichardson Microservices in Java and Scala Chris Richardson Founder of Eventuate.io Founder of the original CloudFoundry.com Author of POJOs in Action @crichardson chris@chrisrichardson.net http://microservices.io http://eventuate.io http://plainoldobjects.com Copyright © 2015. Chris Richardson Consulting, Inc. All rights reserved
  2. 2. @crichardson About Chris
  3. 3. @crichardson About Chris Consultant and trainer focusing on microservices (public class: April 28th, Oakland, CA) http://www.chrisrichardson.net/
  4. 4. @crichardson About Chris Founder of a startup that is creating a platform that makes it easy for application developers write microservices (http://eventuate.io)
  5. 5. @crichardson For more information http://bit.ly/eventsmarch17
  6. 6. Agenda Why 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
  7. 7. @crichardson In 1986… http://en.wikipedia.org/wiki/Fred_Brooks
  8. 8. @crichardson Yet 30 years later…. If you __________________ a puppy will die Therefore you must _______________
  9. 9. @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 ….
  10. 10. @crichardson How we make decisions Decide using emotions Rationalize with our intellect http://en.wikipedia.org/wiki/Mahout
  11. 11. @crichardson The structure of a pattern = Great framework for discussing and thinking about technology
  12. 12. @crichardson The structure of a pattern Resulting context aka the situation Name Context Problem Related patterns (conflicting) issues etc to address Forces Solution
  13. 13. @crichardson
  14. 14. Agenda Why 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
  15. 15. @crichardson
  16. 16. In theory: We can build a modular monolith But in practice: We build a big ball of mud
  17. 17. Microservices are not a silver bullet but …
  18. 18. @crichardson The benefits typically outweigh the drawbacks for large, complex applications
  19. 19. @crichardson Build and deliver better software faster
  20. 20. @crichardson Easily try other technologies ... and fail safely
  21. 21. Agenda Why 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
  22. 22. Data management patterns Database per Service Event-driven architecture Shared database Event sourcing Transaction log tailing Database triggers Application events CQRS Database architecture
  23. 23. Database per Service Orders Service Customer Service Order Database Customer Database Sharded SQL NoSQL DB
  24. 24. @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 ?
  25. 25. @crichardson Event-driven architecture
  26. 26. @crichardson How atomically update database and publish an event Order Service Order Database Message Broker insert Order publish OrderCreatedEvent dual write problem ?
  27. 27. @crichardson Reliably generating events
  28. 28. @crichardson Use event-sourcing Event table Aggregate type Event id Aggregate id Event data Order 902101 …OrderApproved Order 903101 …OrderShipped Event type Order 901101 …OrderCreated
  29. 29. @crichardson Replay events to recreate state Order state OrderCreated(…) OrderAccepted(…) OrderShipped(…) Events Periodically snapshot to avoid loading all events
  30. 30. But what about queries?
  31. 31. @crichardson Command Query Responsibility Segregation (CQRS) Command side Commands Aggregate Event Store Events Query side Queries (Materialized) View Events
  32. 32. @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
  33. 33. Eventuate architecture
  34. 34. Eventuate platform
  35. 35. 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
  36. 36. Agenda Why 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
  37. 37. Customer command side
  38. 38. @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
  39. 39. @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 }
  40. 40. @crichardson Customer command processing
  41. 41. @crichardson Customer applying events
  42. 42. @crichardson Creating an order save() concisely specifies: 1.Creates Order aggregate 2.Processes command 3.Applies events 4.Persists events
  43. 43. @crichardson Event handling in Customers 1. Load Customer aggregate 2. Processes command 3. Applies events 4. Persists events Triggers BeanPostProcessor Durable subscription name
  44. 44. Customer - query side
  45. 45. @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
  46. 46. @crichardson Query-side event handler that updates customer view
  47. 47. @crichardson Updating and query view using Spring Data for MongoDB...
  48. 48. Agenda Why 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
  49. 49. @crichardson Functional Customer aggregate Customer creditLimit creditReservations … CustomerAggregate processCommand(Account, Command) : Seq[Events] applyEvent(Account, Event) : Account Immutable state Behavior
  50. 50. @crichardson Aggregate type classes Used by Event Store to reconstitute aggregates Hardwired
  51. 51. @crichardson Customer Aggregate…. State Behavior
  52. 52. @crichardson …command processing…
  53. 53. @crichardson … applying events
  54. 54. Agenda Why 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
  55. 55. @crichardson Kanban board example
  56. 56. @crichardson Architecture Create/update boards and tasks Change notifications Materialized views Event Store
  57. 57. @crichardson Demo
  58. 58. 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
  59. 59. @crichardson @crichardson chris@chrisrichardson.net http://bit.ly/eventsmarch17 Questions?

×