Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lightbend Lagom - Microservices Just Right

h3nk3
July 28, 2016
150

Lightbend Lagom - Microservices Just Right

Presentation by @ironfish and @h3nk3 at Philly JUG on July 27th, 2016.

h3nk3

July 28, 2016
Tweet

Transcript

  1. Lagom - [lah-gome] Adequate, sufficient, just right A great explanation

    of Lagom: https://www.youtube. com/embed/1tFrRUgFrX4
  2. Agenda • Overview Reactive • Why Lagom? • Lagom Walkthrough

    ◦ Development Environment ◦ Service API ◦ Persistence API • Running in Production
  3. Why Lagom? • State-of-the-art technologies in an opinionated way •

    Building Microservices is hard! • Developer experience matters ◦ No brittle script to run your services ◦ Inter-service communication just works ◦ Services are automatically reloaded on code change • Takes you through to production deployment
  4. • sbt build tool (developer environment) • Play 2.5 •

    Akka 2.4 (clustering, streams, persistence) • Cassandra (default data store) • Jackson (JSON serialization) • Guice (DI) Under the hood
  5. hello-world-system → project root └ helloworld-api → helloworld api project

    └ helloworld-impl → helloworld implementation └ project → sbt configuration files └ plugins.sbt → sbt plugins └ build.sbt → the project build file Anatomy of a Lagom project Each service definition is split into two sbt projects: api & impl
  6. Service Definition // this source is placed in your api

    project package hello.api; import com.lightbend.lagom.javadsl.api.*; import static com.lightbend.lagom.javadsl.api.Service.*; public interface HelloService extends Service { ServiceCall<String, String> sayHello(); default Descriptor descriptor() { return named("helloservice").withCalls( namedCall("hello", this::sayHello) ); } }
  7. ServiceCall explained • ServiceCall contains two types: ◦ Request: type

    of incoming request message (e.g. String) ◦ Response: type of outgoing response message (e.g. String) • CompletionStage: a promise of a value in the future • JSON is the default serialization format for request/response messages • There are two kinds of request/response messages: Strict and Streamed interface ServiceCall<Request, Response> { CompletionStage<Response> invoke(Request request); }
  8. Strict Messages Strict messages are fully buffered into memory ServiceCall<String,

    String> sayHello(); default Descriptor descriptor() { return named("helloservice").withCalls( namedCall("hello", this::sayHello) ); }
  9. Streamed Messages ServiceCall<String, Source<String, ?>> tick(int interval); default Descriptor descriptor()

    { return named("clock").withCalls( pathCall("/tick/:interval", this::tick) ); } • A streamed message is of type Source (an Akka streams API) • Back-pressured, asynchronous handling of messages • WebSocket is the selected transport protocol
  10. Remember this service definition? // this source is placed in

    your api project public interface HelloService extends Service { ServiceCall<String, String> sayHello(); default Descriptor descriptor() { return named("helloservice").withCalls( namedCall("hello", this::sayHello) ); } }
  11. Here is the Service Implementation // this source is placed

    in your implementation project package hello.impl; import com.lightbend.lagom.javadsl.api.*; import static java.util.concurrent.CompletableFuture.completedFuture; import hello.api.HelloService; public class HelloServiceImpl implements HelloService { public ServiceCall<String, String> sayHello() { return name -> completedFuture("Hello " + name); } }
  12. // this source is placed in your implementation project package

    hello.impl; import com.google.inject.AbstractModule; import com.lightbend.lagom.javadsl.server.ServiceGuiceSupport; import hello.api.HelloService; public class HelloModule extends AbstractModule implements ServiceGuiceSupport { protected void configure() { bindServices(serviceBinding(HelloService.class, HelloServiceImpl.class)); } } Register Service Implementation
  13. // Instruct Lagom to load this module by adding it

    to // the application.conf file: play.modules.enabled += hello.impl.HelloModule Register Service Implementation - part II
  14. • Each service owns its data ◦ Only the service

    has direct access to the DB • We advocate the use of Event Sourcing (ES) and CQRS ◦ ES: Capture all state’s changes as events ◦ CQRS: separate models for write and read Principles
  15. Event Sourcing/CQRS: Command Query Responsibility Segregation "CQRS is simply the

    creation of two objects where there was previously only one. The separation occurs based upon whether the methods are a command or a query (the same definition that is used by Meyer in Command and Query Separation: a command is any method (object) that mutates state and a query is any method (object) that returns a value)” - Greg Young
  16. Event Sourcing/CQRS: Historical behavior is captured • Behavioral by nature

    • Convert valid commands into 1/n events • Current state is not persisted • Current state is derived • Append only store
  17. Event Sourcing/CQRS: • Create your own Command and Event classes

    • Subclass PersistentEntity ◦ Define Command and Event handlers ◦ Can be accessed from anywhere in the cluster ◦ (corresponds to an Aggregate Root in DDD)
  18. Benefits of Event Sourcing/CQRS • Allows you to time travel

    • Audit log • Future business opportunities • No need for ORM • Implicit read/write optimization • No database migration script, ever! • Performance & Scalability • Testability & Debuggability
  19. Event Sourcing: Example 1. Create a AddFriend command class 2.

    Create a FriendAdded event class 3. Define a FriendEntity holding the state (i.e., who are the friends of a specific user) a. Create a command handler for the AddFriend command b. Create an event handler for the FriendAdded event
  20. Event Sourcing: Example cont’d 1. Create a AddFriend command class

    2. Create a FriendAdded event class 3. Define a FriendEntity holding the state of what are friends of a given user 4. Create a command handler for the AddFriend command 5. Create an event handler for the FriendAdded event
  21. Event Sourcing: Example cont’d public interface FriendCommand extends Jsonable {

    // other commands . . . @SuppressWarnings("serial") @Immutable @JsonDeserialize public final class AddFriend implements FriendCommand,PersistentEntity.ReplyType<Done> { public final String friendUserId; @JsonCreator public AddFriend(String friendUserId) { this.friendUserId = Preconditions.checkNotNull(friendUserId, "friendUserId"); } // equals, equalTo, hashCode, toString love . . . } }
  22. Event Sourcing: Example cont’d 1. Create a AddFriend command class

    2. Create a FriendAdded event class 3. Define a FriendEntity holding the state of what are friends of a given user 4. Create a command handler for the AddFriend command 5. Create an event handler for the FriendAdded event
  23. Event Sourcing: Example cont’d public interface FriendEvent extends Jsonable, AggregateEvent<FriendEvent>

    { // other commands . . . @SuppressWarnings("serial") @Immutable @JsonDeserialize public class FriendAdded implements FriendEvent { //... @JsonCreator public FriendAdded(String userId, String friendId, Optional<Instant> timestamp) { this.userId = Preconditions.checkNotNull(userId, "userId"); this.friendId = Preconditions.checkNotNull(friendId, "friendId"); this.timestamp = timestamp.orElseGet(() -> Instant.now()); } // equals, equalTo, hashCode, toString love . . . } }
  24. Event Sourcing: Example cont’d 1. Create a AddFriend command class

    2. Create a FriendAdded event class 3. Define a FriendEntity holding the state of what are friends of a given user 4. Create a command handler for the AddFriend command 5. Create an event handler for the FriendAdded event
  25. Event Sourcing: Example cont’d public class FriendEntity extends PersistentEntity<FriendCommand, FriendEvent,

    FriendState> { @Override public Behavior initialBehavior(Optional<FriendState> snapshotState) { BehaviorBuilder b = newBehaviorBuilder(snapshotState.orElse( new FriendState(Optional.empty()))); // define more command and event handlers return b.build(); } }
  26. Event Sourcing: Example cont’d @SuppressWarnings("serial") @Immutable @JsonDeserialize public final class

    FriendState implements Jsonable { public final Optional<User> user; @JsonCreator public FriendState(Optional<User> user) { this.user = Preconditions.checkNotNull(user, "user"); } public FriendState addFriend(String friendUserId) { if (!user.isPresent()) throw new IllegalStateException("friend can't be added before user is created"); PSequence<String> newFriends = user.get().friends.plus(friendUserId); return new FriendState(Optional.of(new User(user.get().userId, user.get().name, Optional.of(newFriends)))); } // equals, equalTo, hashCode, toString love . . . }
  27. Event Sourcing: Example cont’d 1. Create a AddFriend command class

    2. Create a FriendAdded event class 3. Define a FriendEntity holding the state of what are friends of a given user 4. Create a command handler for the AddFriend command 5. Create an event handler for the FriendAdded event
  28. Event Sourcing: Example cont’d public class FriendEntity extends PersistentEntity<FriendCommand, FriendEvent,

    FriendState> { @Override public Behavior initialBehavior(Optional<FriendState> snapshotState) { // CommandHandler BehaviorBuilder b = newBehaviorBuilder(snapshotState.orElse(new FriendState(Optional.empty()))); // Command handlers are invoked for incoming messages (commands). // A command handler must "return" the events to be persisted (if any). b.setCommandHandler(AddFriend.class, (cmd, ctx) -> { if (!state().user.isPresent()) { ctx.invalidCommand("User " + entityId() + " is not created"); return ctx.done(); } else if (state().user.get().friends.contains(cmd.friendUserId)) { ctx.reply(Done.getInstance()); return ctx.done(); } else { return ctx.thenPersist(new FriendAdded(getUserId(), cmd.friendUserId), evt -> ctx.reply(Done.getInstance())); } }); // more command/event handlers } }
  29. Event Sourcing: Example cont’d 1. Create a AddFriend command class

    2. Create a FriendAdded event class 3. Define a FriendEntity holding the state of what are friends of a given user 4. Create a command handler for the AddFriend command 5. Create an event handler for the FriendAdded event
  30. Event Sourcing: Example cont’d public interface FriendService extends Service {

    // other service calls . . . ServiceCall<FriendId, NotUsed> addFriend(String userId); @Override default Descriptor descriptor() { return named("friendservice").withCalls( pathCall("/api/users/:userId", this::getUser), namedCall("/api/users", this::createUser), pathCall("/api/users/:userId/friends", this::addFriend), pathCall("/api/users/:userId/followers", this::getFollowers) ); } }
  31. Event Sourcing: Example cont’d public class FriendServiceImpl implements FriendService {

    private final PersistentEntityRegistry persistentEntities; private final CassandraSession db; @Inject public FriendServiceImpl(PersistentEntityRegistry persistentEntities, CassandraReadSide readSide, CassandraSession db) { this.persistentEntities = persistentEntities; this.db = db; // at service startup we must register the needed entities persistentEntities.register(FriendEntity.class); readSide.register(FriendEventProcessor.class); } @Override public ServiceCall<FriendId, NotUsed> addFriend(String userId) { return request -> { return friendEntityRef(userId).ask(new AddFriend(request.friendId)).thenApply(ack -> NotUsed.getInstance()); }; } private PersistentEntityRef<FriendCommand> friendEntityRef(String userId) { PersistentEntityRef<FriendCommand> ref = persistentEntities.refFor(FriendEntity.class, userId); return ref; } }
  32. Event Sourcing/CQRS: • Tightly integrated with Cassandra • Create the

    query tables: ◦ Subclass CassandraReadSideProcessor ◦ Consumes events produced by the PersistentEntity and updates tables in Cassandra optimized for queries • Retrieving data: Cassandra Query Language ◦ e.g., SELECT id, title FROM postsummary
  33. Running in Production • sbt-native packager is used to produce

    zip, MSI, RPM, Docker • Lightbend ConductR* (our container orchestration tool) • Lightbend Reactive Platform* ◦ Split Brain Resolver (for Akka cluster) ◦ Lightbend Monitoring *Requires a Lightbend subscription (but it is free to use during development)
  34. Current[Lagom] • Current version is 1.0.0 (released yesterday!) • Java

    API, but no Scala API yet ◦ We are working on the Scala API ◦ But using Scala with the Java API works quite well! https: //github.com/dotta/activator-lagom-scala-chirper
  35. Future[Lagom] • Maven support • Message broker integration (e.g. Kafka)

    • Scala API • Support for other cluster orchestration tools ◦ Want Kubernetes support? Contribute! https://github. com/huntc/kubernetes-lib • Support for writing integration tests • Swagger integration
  36. Next: Seq[Step] • Try Lagom yourself ◦ https://lightbend.com/lagom • Lagom

    on Github ◦ https://github.com/lagom/lagom • Read Jonas Bonér's free ebook Reactive Services Architecture ◦ https://lightbend.com/reactive-microservices-architecture • Great presentation by Greg Young on why you should use ES ◦ https://www.youtube.com/watch?v=JHGkaShoyNs
  37. Upgrade your grey matter Two free O’Reilly eBooks by Lightbend

    http://bit.ly/ReactiveMicroservice http://bit.ly/DevelopReactiveMicroservice
  38. Q&A Some commonly asked questions... • CRUD vs ES •

    Microservices Design - Push vs Pull • How does this compare to Serverless/FaaS?