Microphone

Introducing Microphone | Microservices with Service Discovery for .NET

When I started reading about microservices a few years ago, I had a hard time understanding how such services were intended to communicate with or find each other.
If you use config files describing where to find another service, how can that possibly deal with things like redundancy?

Let’s say that you need to spin up more than one instance of a given service, maybe you want to run 10 instances of your Ticket service and 5 instances of your Payment service.
And things become even more problematic in a cloud scenario, maybe you need to spin up new virtual machines on demand, how do you make your services find each other in a dynamic topology like that?

One answer to that question is Service Discovery.

Service Discovery lets you register your services in some sort of registry, that registry can then be used to locate instances of your services.
For those of you who are familiar with Dependency Injection containers, when you fetch an object from a DI container like sovar ticketService = container.Get<ITicketService>(), the concepts are similar, but on a distributed level, you want to get access to some sort of capability but you don’t want to know exactly where or what it is.

Another difference is that in service discovery you could very well have multiple service instances bound to the same service at the same time for redundancy.
e.g. asking the service registry for the TicketService you might get back zero to many URI’s as there could be zero to many active instances at the time when you make the query.

So how can we achieve this?

The wrong way

Can we just create a table in let’s say SQL Server use that for our registry?

Well you could, but it will not work out that great for you.
The first reason is that if you run a single instance of your database, then it is a single point of failure, if the DB dies, so does the rest of your system.
But even if you spin up a cluster of servers, things will still not be that great, multiple SQL Servers are expensive and the consistency model and overhead of data access is not right for this kind of problem.

A better way

Instead, there are specialized tools for this problem.Eureka (Netflix), Consul.io, Etcd, ZooKeeper, Doozer and many others.

Some are made specifically for service discovery and some are distributed key value stores.
You have to find the tool that fits your needs the best.

I personally like Consul.io, as it is designed for this problem.
Services can register themselves using either a REST API or via DNS, and if a service becomes unresponsive, Consul can mark this service as unavailable and not include it in any search results.

Introducing Microphone

The last few days, have been building a mini framework, “Microphone”, that allows you to run self hosting services using either WebApi or NancyFx ontop of a Consul.io cluster.
You can find the code here https://github.com/rogeralsing/Microphone

When using it, each service will start out by allocating a free port to run on, once the service is started, it will register itself in the local Consul agent.

Microphone is available on Nuget:

Install from Nuget

WebApi bootstrapper

1

2

PM>Install-Package Microphone.WebApi

NancyFx bootstrapper

1

2

PM>Install-Package Microphone.Nancy

Create a service

When creating a self hosted service, you need to tell Microphone to bootstrap it for you.
This will register your service in the Consul cluster and start the host for either NancyFx or WebApi;

Web Api

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

classProgram

{

staticvoidMain(string[]args)

{

Bootstrap.Start("WebApiService","v1");

Console.ReadLine();

}

}

publicclassDefaultController:ApiController

{

publicstringGet()

{

return"WebApi Service";

}

}

NancyFx

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

classProgram

{

privatestaticvoidMain(string[]args)

{

Bootstrap.Start("NancyService","v1");

Console.ReadLine();

}

}

publicclassMyService:NancyModule

{

publicMyService()

{

Get["/"]=_=>"Nancy Service";

}

}

Service Discovery

If one of your services needs to communicate with another service in the same Consul cluster you can query it for active instances.

7 Responses

[…] Introducing Microphone | Microservices with Service Discovery for .NET – Roger Alsing announces Microphone a .NET based Microservices framework which provides service discovery on Consul.io clusters with the service implementations being self hosted running code which is WebAPI or NancyFx based […]

Just wondering, you say to choose a random endpoint to load the balance accross multiple servers. Is there a pre-existing solution to load balanced baced on currently active clients? Or would that be somthing that would end up being a custom solution? One such solution could be resource monitoring.