• Stars
    star
    607
  • Rank 71,584 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Guidance for building event-driven distributed systems and microservices in Go with NATS JetStream, gRPC and CockroachDB

Guidance for building distributed systems and Microservices in Go

Articles

Technologies Used:

  • Go
  • NATS JetStream
  • gRPC
  • CockroachDB
  • Go kit
  • Zipkin (for distributed tracing)

Compile Proto files

Run the command below from the eventstream directory:

protoc eventstore/*.proto \
		--go_out=. \
		--go-grpc_out=. \
		--go_opt=paths=source_relative \
		--go-grpc_opt=paths=source_relative \
		--proto_path=.

Set up CockroachDB

Set up CockroachDB Cluster with three nodes

cockroach start \
--insecure \
--store=orders-1 \
--listen-addr=localhost:26257 \
--http-addr=localhost:8080 \
--join=localhost:26257,localhost:26258,localhost:26259 \
--background
cockroach start \
--insecure \
--store=orders-2 \
--listen-addr=localhost:26258 \
--http-addr=localhost:8081 \
--join=localhost:26257,localhost:26258,localhost:26259 \
--background
cockroach start \
--insecure \
--store=orders-3 \
--listen-addr=localhost:26259 \
--http-addr=localhost:8082 \
--join=localhost:26257,localhost:26258,localhost:26259 \
--background

cockroach init command to perform a one-time initialization of the cluster

cockroach init --insecure --host=localhost:26257

Start a SQL Shell for CockroachDB:

cockroach sql --insecure --host=localhost:26257

Create user

cockroach user set shijuvar --insecure

Create Databases

cockroach sql --insecure -e 'CREATE DATABASE eventstoredb'
cockroach sql --insecure -e 'CREATE DATABASE ordersdb'

Grant privileges to the shijuvar user

cockroach sql --insecure -e 'GRANT ALL ON DATABASE ordersdb TO shijuvar'
cockroach sql --insecure -e 'GRANT ALL ON DATABASE eventstoredb TO shijuvar'

Run NATS JetStream Server

nats-server -js

Run JetStream with config file

nats-server -c js.conf

// js.conf file

jetstream {
    // jetstream data will be in /data/jetstream-server/jetstream
    store_dir: "/data/jetstream-server"

    // 1GB
    max_memory_store: 1073741824

    // 10GB
    max_file_store: 10737418240
}

http_port: 8222

Prerequisites for running the eventstream demo

  • Run the NATS JetStream Server
  • Run CockroachDB
  • Run bootstrapper app (eventstream/bootstrapper) as a one time activity for creating stream in JetStream and create two databases in CockroachDB
  • In order to use distributed tracing with gokit-ordersvc (order service with Go kit), run Zipkin distributed tracing system

Basic Workflow in the example (eventstream directory):

  1. A client app post an Order to an HTTP API (ordersvc)
  2. An HTTP API (ordersvc) receives the order, then executes a command onto Event Store, which is an immutable log of events of domain events, to create an event via its gRPC API (eventstoresvc).
  3. The Event Store API executes the command and then publishes an event "ORDERS.created" to NATS JetStream server to let other services know that a domain event is created.
  4. The Payment worker (paymentworker) subscribes the event "ORDERS.created", then make the payment, and then create another event "ORDERS.paymentdebited" via Event Store API.
  5. The Event Store API executes a command onto Event Store to create an event "ORDERS.paymentdebited" and publishes an event to NATS JetStream server to let other services know that the payment has been debited.
  6. The Query synchronising worker (querymodelworker) subscribes the event "ORDERS.created" that synchronise the query data model to provide state of the aggregates for query views.
  7. The review worker (reviewworker) subscribes the event "ORDERS.paymentdebited" and finally approves the order, and then create another event "ORDERS.approved" via Event Store API.
  8. A Saga coordinator manages the distributed transactions and makes void transactions on failures (to be implemented)

Training and Consulting in India

As a Consulting Solutions Architect, I do provide training and consulting on Go programming language and distributed systems architectures, in India.

More Repositories

1

gokit

Go Examples: From basics to distributed systems
Go
566
star
2

go-web

Source code for the book "Web Development with Go"
Go
184
star
3

gokit-examples

Examples for building microservices with Go kit (gokit.io)
Go
179
star
4

go-recipes

Code examples for the book "Go Recipes" by Apress.
Go
141
star
5

react-aspnet

Demo app with React and ASP.NET Web API
JavaScript
117
star
6

go-distsys

Distributed Systems programming examples in the Go programming language.
Go
103
star
7

EFMVC.Azure

Extending the EFMVC app for adding Windows Azure specific features
JavaScript
44
star
8

Koa-CRUD

A CRUD demo for Koa.js and Node.js
JavaScript
35
star
9

HapiAzure

Single Page Application (SPA) Demo with Hapi.js, AngularJS and Azure Table Storage.
JavaScript
29
star
10

go-samples-thenewstack

Golang samples for the articles written in "The New Stack" Website.
Go
27
star
11

shijuvar

20
star
12

gophercon-rest-demo

REST API in Go with MongoDB as the data store.
Go
13
star
13

NodeExpressMongo

Building Windows Azure Web Sites by using Node.js, Express.js, Mongoose and MongoDB
JavaScript
11
star
14

golang-docker

Golang web app with Docker
Go
6
star
15

AzureCloudServices

Multi-Tier Windows Azure Cloud Services App with Web Role, Worker Role, Table Storage and Service Bus
JavaScript
5
star
16

RESTNode

REST API samples using Node.js with MongoDB and Mongoose
JavaScript
4
star
17

service-weaver

Examples and guidance for building modern distributed applications in Go with Service Weaver
Go
4
star
18

go-http-samples

Golang samples for HTTP servers
Go
3
star
19

koa-demos

Samples and demos for Koa.js
1
star
20

NodeOnAzure

Node.js examples for Windows Azure
JavaScript
1
star