Microservices in Golang

24
Microservice in Go Moath Qasim LOVOO GmbH

Transcript of Microservices in Golang

Microservice in GoMoath Qasim LOVOO GmbH

Microservice

Microservice

Microservice

Microservice

Microservice

Microservice

Microservice Microservice

So what is this Microservices is all about?

Microservices Architecture is a software architecture style in which complex applications are composed of small, independent processes communicating with each other using language-agnostic APIs.

Switches

Load Balancer

bla bla bla

DB

DB

DB

Ohhhhhh !! So What??

Let’s go “Monolithic”

let us imagine we are building a social network application.

Front End

User Services

Messaging Services

Payment Services

DB

DB

DB

Web Server

Yes, That’s what I am talking about

-Simple to develop -Simple to deploy -Simple to scale

, HOWEVER What if, the application get’s bigger and bigger and the team gets also bigger and bigger.

You might run into these problems:

- Huge applications can be a boogieman specially for new developers.

- Overloaded web containers.

- Continuous deployment is difficult.

- Requires a long-term commitment to a technology stack.

- Scaling the application can be difficult.

Hmm, I really didn’t see that coming!!

Enter Microservices Architecture

- Split your application into independent components based on the functionality of each one of them.

- Find a protocol to make those services interact with each other whether in synchronous way or in asynchronous way.

- Remember, each service is totally independent in order to be decoupled from other services, so each one of them should have it’s own database.

- Consistency between those services can be achieved via having a database replication mechanism or by applications-level events.

Looks a little bit better, right?

User Services

Messaging Services

Payment Services

DB

DB

DB

Front End

O.K, those are the benefits of what we have done:

- Each Microservice is relatively small.

- Each service can be deployed independently of other services

- Easier to scale development.

- Improved fault isolation.

- Each service can be developed and deployed independently.

- Eliminates any long-term commitment to a technology stack.

Enter our #ROCKSTAR, yes you are right, it’s Gopher the one and only.

Implementing Microservice Architecture using Go

The powerful net/http package lets you write performant web services in a very quick way.

Basically you want to have many independent worker applications that are running separately, each with their own responsibility of doing certain tasks.

In real world scenarios you are going to have hundreds of applications running. You want to talk with them securely (and also authenticated).

You can implement your own MS architecture, using the core package of Go without looking on what’s already out there.

One of the most impressive packages which helps you building your infrastructure of your MS architecture is Kite.

Bla, Bla, Bla, show me some code man!!!

You asked for it, and I listend ;)First let me show you a Kite in the most simple form:

Here we just created a kite with the name first and version 1.0.0. The Run() method is running a server, which is blocking (just like http.Serve).

This kite is now capable of receiving requests.

Because no port number is assigned the OS has picked one for us automatically.

Writing a kite

Let us assign a port now, so we can connect to it from another kite (otherwise you need to pick the assigned URL from the logs).

To change the configuration of a Kite, such as Port number, the properties (such as Environment, Region, etc… you’ll need to modify the Config fields:

The configuration values can be also overridden via environment variables if needed.

Let us create a second kite to talk with the first kite:

The response of should be something like this:

Adding some methods to make things a little bit interesting

Let us add our first custom method. This simple method is going to accept a name and return a welcome message. The name of the method will be “sayHello”.

To assign a function to a method just be sure it’s satisfies the kite.Handler interface (http://godoc.org/github.com/koding/kite#Handler):

Let’s call it via our “second” kite:

As you see the only thing that has changed is the method call. When we call the “sayHello” method we also send the name “Marco Reus” with as an arguments. You can send any JSON compatible Go type.

That was super easy and AWESOME :)))

“Dude!! Where is my car?? ”, Service Discovery To be discovered by others they need to know your real identity. Basically you need to be authenticated. “kitectl” is a handy CLI program which can be used to manage kites easily via command line. We can use it (via “kitectl register” command) to authenticate our machine to Kontrol, so every kite running on our host will be authenticated by default.

Kontrol Configs:

Install Kontrol:

Using Kontrol

Using Kontrol

That’s it Thank you and keep up rocking with Go

Moath QasimLOVOO GmbH

Resources:

http://microservices.io/patterns/microservices.html https://godoc.org/github.com/koding/kite http://blog.gopheracademy.com/birthday-bash-2014/kite-microservice-library/

@MoadQassem