Introducing Microphone | Microservices with Service Discovery for .NET

with 7 Comments

TLDR; links

Microphone
Nancy
Consul.io

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 so
var 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

NancyFx bootstrapper

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

NancyFx

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.

Running your services

Before you start your services, make sure you have an active Consul cluster running on the host machine.

If you are new to Consul, you can bootstrap your test environment using this command:

This will give you a single server Consul cluster, this is not recommended for production usage, but it will allow you to use service discovery on your dev machine.

Diagnostics using Consul REST API

Check service health on Consul agent:

GET

Check all services registered on Consul agent:

GET

Follow Roger Alsing:

Developer, Mentor and Architect. Co-Founder of the Akka.NET actor model framework. akka.nethouse.se

7 Responses

  1. […] 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 […]

  2. […] This post is a follow up on Introducing Microphone […]

  3. Michael
    | Reply

    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.

  4. […] Microphone – this is awesome! and consul.io is easy to setup using docker 😉 this will be something we could use. […]

  5. nen
    | Reply

    Hi Rojer,

    are you using Consul as a key value store to register the services ??

    Thanks -Nen

  6. Muhammad Rehan Saeed
    | Reply

    Do you recommend Consul.io for API to API calls only or can you use it with external clients too?

Leave a Reply