/Microphone

Primary LanguageC#Apache License 2.0Apache-2.0

Microphone - Self announcing services

Microphone is a lightweight framework to run self hosting REST services using Web Api or NancyFx ontop of a Consul or ETCD cluster. 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 cluster provider.

Install from Nuget

WebApi bootstrapper

PM> Install-Package Microphone.WebApi

NancyFx bootstrapper

PM> Install-Package Microphone.Nancy

Create a service

Web Api

    class Program
    {
        static void Main(string[] args)
        {
            Cluster.Bootstrap(new WebApiProvider(), new ConsulProvider(), "orders", "v1");
            Console.ReadLine();
        }
    }

    public class OrdersController : ApiController
    {
        public string Get()
        {
            return "WebApi Service";
        }
    }

NancyFx

    class Program
    {
        private static void Main(string[] args)
        {
            Cluster.Bootstrap(new NancyProvider(), new ConsulProvider(), "customers", "v1");
            Console.ReadLine();
        }
    }

    public class CustomersService : NancyModule
    {
        public MyService()
        {
            Get["/"] = _ => "Nancy Service";
        }
    }

Cluster providers

Consul

Cluster.Bootstrap(new WebApiProvider(), new ConsulProvider(), "my-service", "v1");

The Consul provider also works together with Ebays "Fabio" load balancer https://github.com/eBay/fabio

Cluster.Bootstrap(new WebApiProvider(), new ConsulProvider(useEbayFabio:true), "my-service", "v1");

ETCD

Cluster.Bootstrap(new WebApiProvider(), new EtcdProvider(), "my-service", "v1");

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.

//inside some WebApi/Nancy endpoint:

//automatically load balanced over service instances
var instance = await Cluster.FindServiceInstanceAsync("orders"); 

//Use Rest# or similar to call into the remote service
MakeSomeCall("/api/orders",instance.ServiceAddress, instance.ServicePort);

Running your services

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

Consul Cluster

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

consul agent -server -bootstrap -data-dir /tmp/consul

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

http://localhost:8500/v1/agent/checks

Check all services registered on Consul agent:

GET

http://localhost:8500/v1/agent/services

ETCD Cluster

If you are using the ETCD cluster provider, make sure you have a local ETCD cluster running on your dev machine.

etcd.exe