Microservices in Golang
-
Upload
moath-qasim -
Category
Technology
-
view
653 -
download
2
Transcript of Microservices in Golang
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
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
, 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.
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.
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.
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.
“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:
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