Coder Social home page Coder Social logo

ivan-tamayo / event-sourcing-examples Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cer/event-sourcing-examples

0.0 1.0 0.0 252 KB

Example code for my building and deploying microservices with event sourcing, CQRS and Docker presentation

License: Other

Shell 2.63% Java 64.75% Groovy 1.49% Scala 31.14%

event-sourcing-examples's Introduction

#Event-Sourcing+CQRS example application

This example application is the money transfer application described in my talk Building and deploying microservices with event sourcing, CQRS and Docker. This talk describe a way of architecting highly scalable and available applications that is based on microservices, polyglot persistence, event sourcing (ES) and command query responsibility segregation (CQRS). Applications consist of loosely coupled components that communicate using events. These components can be deployed either as separate services or packaged as a monolithic application for simplified development and testing.

Built using the Eventuate platform

Read the overview or look at the other example applications.

Big ideas

This example illustrates several important concepts:

  • How to decompose an application into microservices - as described below the application consists of several services. For example, bank accounts are managed by one service, money transfers by another service.

  • Using an event-driven architecture to achieve data consistency - rather than using traditional distributed transaction to maintain database consistency this application uses an eventually consistent, event-driven approach.

  • Using event sourcing to implement the event-driven architecture - the domain logic consists of Domain-Driven Design (DDD) aggregates that using event sourcing.

  • Using Command Query Responsibility Segregation (CQRS) - update requests (HTTP POSTs and PUTs) and view requests (HTTP GETs) are handled by separate services.

  • How event sourcing enables deployment flexibility - the application can either be deployed as a monolith or as microservices.

About the example application

This example application provides a REST API for creating and viewing bank accounts and transferring money between them.

The following diagram shows the architecture:

Money transfer architecture

There are four logical services:

  • Accounts (command-side) - REST API for creating accounts
  • Money transfers (command-side) - REST API for transferring money
  • Account view updater (query-side) - subscribes to events and updates a MongoDB View
  • Account view reader (query-side) - REST API for retrieving accounts

One of the neat things about the modular architecture is that there are two ways to deploy these four services:

  • monolithic-service - all services are packaged as a single Spring Boot executable JAR
  • Microservices - three separate Spring Boot executable JARs
  • accounts-command-side-service - command-side accounts
  • transactions-command-side-service - command-side money transfers
  • accounts-query-side-service - Account View Updater and Account View Reader

About the examples

There are currently the following versions of the example application:

  • java-spring - a Java and Spring Boot example
  • scala-spring - a Scala and Spring Boot example

Other examples will be added shortly including a Scala/Play example.

For more information, please see the wiki

About the Event Store

The application uses one of two event stores:

  • Embedded SQL-based event store, which is great for integration tests. It is also used when running the monolithic version of the application.
  • Event Store server - this is a full featured event store. See this wiki page for more details.

Building the application (and running the tests)

Both versions of the application use Gradle. To build an application, execute this command in the application's top-level directory:

./gradlew assemble

Note: you do not need to install Gradle. It will be automatically downloaded by ./gradlew.

This will build a Spring Boot jar in each of the *-service directories.

You can also run the tests using gradle build. However, you must set some environment variables.

First, you need to tell the query side code how to connect to MongoDB:

  export SPRING_DATA_MONGODB_URI=mongodb://192.168.59.103/yourdb

Docker Compose is a great way to run MongoDB. You can run the docker-compose up -d mongodb to run MongoDB.

Second, some of the tests in accounts-command-side-service, transactions-command-side-service, accounts-query-side-service and e2e-test need you need to set some environment variables that tell them how to connect to the Event Store server. But don't worry. The build is configured to ignore failures for those projects.

Running the application

To run the application, you must to set the SPRING_DATA_MONGODB_URI environment variable, which tells the query services how to connect to MongoDB.

There are a couple of different ways of running the application.

Running the monolithic application

One option is to run the self-contained monolithic application. It uses the embedded event store.

Simply use this command:

java -jar monolithic-service/build/libs/monolithic-service.jar

Running the microservices

The other option is to run the services separately. However, in order to do this you need to get credentials for the Event Store.

One way to run the services is to use the scripts run-all-services.sh, which runs the services, and kill-all-services.sh, which kills the processes.

A much better way, however, is to use Docker Compose. Simply run the command docker-compose up to launch the services. This will create containers for MongoDB and each of the services.

You can now, for example, use the curl commands in handy-curl-commands.sh to interact with the server.

You can also use the Swagger UI exposed by each service http://host:port/swagger-ui.html.

event-sourcing-examples's People

Contributors

cer avatar rstoyanchev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.