Micro on NATS - Microservices with Messaging

15
Micro on NATS Microservices with messaging

Transcript of Micro on NATS - Microservices with Messaging

Page 1: Micro on NATS - Microservices with Messaging

Micro on NATSMicroservices with messaging

Page 2: Micro on NATS - Microservices with Messaging

Who am I?

Asim Aslam

Working on Micro

Previously at Hailo

Before then Google

@chuhnk

asimaslam.com

Page 3: Micro on NATS - Microservices with Messaging

What is Micro?

A microservice ecosystem

Simplifying building and managing distributed systems

What does that even mean?

Building OSS tools that make it easier to write and run micro service applications

Written in Go

100% Open Source

Page 4: Micro on NATS - Microservices with Messaging

Micro A Microservice Toolkit

Go Micro - Pluggable RPC framework for writing microservices

API - API gateway. Translates HTTP to RPC. Path-to-Service resolution

Web - Dashboard and reverse proxy. Web apps as first class citizens

CLI - Command line interface. Straight forward. No frills.

Sidecar - HTTP interface with all the features of Go Micro

Bot - Hubot style bot for ChatOps. CLI Features. A bot? Really? Yea dude!

Page 5: Micro on NATS - Microservices with Messaging

Go Micro RPC Framework

Registry - Service Discovery

Selector - Client side load balancer

Transport - Synchronous Communication

Broker - Asynchronous Communication

Codec - Message encoding

Client - RPC Client

Server - RPC Server

Page 6: Micro on NATS - Microservices with Messaging

Why NATS?

Built for high performance and scale

Highly available - client and server side clustering

Extremely lightweight - written in Go, no persistence features

At most once delivery - fire and forget

Simple, focused and made for microservices

Written by Derek Collison, with two decades of experience building messaging systems

Page 7: Micro on NATS - Microservices with Messaging

Micro on NATS NATS Plugins

Go-Micro has a pluggable architecture

Each feature written as a Go interface. Just implement the interface.

Integrate NATS using plugins for:

Registry - Service Discovery

Transport - Synchronous Communication

Broker - Asynchronous Communication

Page 8: Micro on NATS - Microservices with Messaging

Transport Synchronous Communication

Handles request-response

Supports bidirectional streaming

Socket style semantics; Send(), Recv(), Close()

type Transport interface { Dial(addr string, opts ...DialOption) (Client, error) Listen(addr string, opts ...ListenOption) (Listener, error) String() string}

Page 9: Micro on NATS - Microservices with Messaging

Transport

1. Server Listen - Subscribes to unique topic, wait for pseudo connections2. Client Dial - Subscribes to unique topic, publishes to Server with reply topic set3. Server Socket - Pseudo Socket created for every new “connection”4. Client Socket - Same as server. Send() - Publish, Recv() - Subscribed messages

Page 10: Micro on NATS - Microservices with Messaging

Broker Asynchronous Communication

Publish and Subscribe mechanism

Includes notion of Topics and Channels (Queues)

Distribute messages between subscribers

type Broker interface { …. Connect() error Disconnect() error Publish(string, *Message, ...PublishOption) error Subscribe(string, Handler, ...SubscribeOption) (Subscriber, error) String() string}

Page 11: Micro on NATS - Microservices with Messaging

Broker

1. Subscribe - Register interest to Topic. Specify Queue to distribute messages2. Publish - Asynchronously publish to Topic3. Encoding - Broker encodes Message to JSON

Page 12: Micro on NATS - Microservices with Messaging

Registry Service Discovery

Service discovery using a message bus? What magic is this?

type Registry interface { Register(*Service, ...RegisterOption) error Deregister(*Service) error GetService(string) ([]*Service, error) ListServices() ([]*Service, error) Watch() (Watcher, error) String() string}

Page 13: Micro on NATS - Microservices with Messaging

Broadcast Queries!

1. All services subscribe for queries on a broadcast topic2. Publish query on broadcast topic with reply address3. Instances of queried service respond4. Wait for responses until upper time limit5. Aggregate and return results

Page 14: Micro on NATS - Microservices with Messaging

Scaling Like A Boss

Cluster per AZ or Region

Multi-AZ deployment

Fault tolerant of AZ failure

Pattern for high availability

Multi-region deployment

Use DNS latency based routing