Giter Site home page Giter Site logo

microphone's Introduction

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.AspNet

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

Key/Value storage

    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    class Program
    {
        private static void Main(string[] args)
        {
            Cluster.Bootstrap(new WebApiProvider(), new ConsulProvider(), "orders", "v1");
            Cluster.KVPutAsync("val1", new Person { Name = "Name", Age = 25 }).Wait();
            var res = Cluster.KVGetAsync<Person>("val1").Result;
            Console.ReadLine();
        }
    }

microphone's People

Contributors

alexpantyukhin avatar antoinega avatar dan-mcelroy avatar madhon avatar rogeralsing avatar thecodejunkie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.