Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Consul + Megaphone: Monitoring Your Microservices

DZone's Guide to

Consul + Megaphone: Monitoring Your Microservices

Learn how you can use the Consul and Megaphone tools to monitor your microservice applications in this quick tutorial.

· Microservices Zone ·
Free Resource

Containerized Microservices require new monitoring. See why a new APM approach is needed to even see containerized applications.

Many applications are being migrated or developed in microservice architecture, leaving the monolithic structure behind. There are several advantages and disadvantages to using microservices; it all depends on your scenario.

At the end of this post, I will link to interesting content to better understand the structure of microservices and the comparison between that and the monolithic structure.

The purpose of this article is to demonstrate how to easily publish and make discovery of your microservices in your environment.

Consul

First let’s talk about consul.io, a tool for

  • Service discovery
  • Failure detection
  • Multi-datacenter
  • KV storage

Imagine one scenario with 50 microservices, 5 application servers, and one server running Consul.

First, monitoring these services manually is not an easy task. Let’s run Consul so we can move on to the next steps.

Download Consul for your platform (you can also use it in Docker) and upload the service by running this command (varies according to your OS):

 consul.exe agent -dev 

Good - our service is running and you can check it on your browser at this address: http://localhost:8500.

Megaphone

Let’s go to the part where you need to publish these services. Will you have to hardcode address and port in each one? No need! That’s where Megaphone comes in. Megaphone is a tool that I inherited from Microphone. Because it is not being maintained anymore and the author is no longer doing bug fixes, I got the code base, applied some corrections, refactoring, and improvements, and published it.

Let’s see how it works! I’ll write an example of code that will be easier to understand:

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

            Console.ReadLine();
        }
    }

    public class CustomerService : NancyModule
    {
        public CustomerService() : base("/customers")
        {
            Get["/"] = _ =>
            {
                var customer = new Customer
                {
                    CustomerId = 123,
                    CustomerName = "Acme Inc",
                };

                return Response.AsJson(customer);
            };
        }
    }


In the example above, I’m using NancyFX to publish my service.

Notice that I have an endpoint called / customers and a Get in the “/” that will return a JSON of the Customer object.

I ran my service - which port it is? And the address?

 Cluster.Bootstrap(new NancyProvider(), new ConsulProvider(), “customers”, “v1”);

Now you can return to the Consul and check that things have changed there:

Notice that our service now is running on port 59588 of localhost, which is the node who published the service.

Then:

Great again! Now how to discover this service via C#?


private static void DiscoverService()
{
    var instance = Cluster.FindServiceInstanceAsync("customers").Result;
    Console.WriteLine($"{instance.Address} {instance.Port}");
    Console.ReadLine();
}

So, we send a request to Consul asking for the service name, and it returns the address and port. Awesome!

Important note:

To run the example, it’s important you run VisualStudio under Admin rights.

Source code on GitHub ❤

thiagoloureiro/Megaphone

Megaphone — A framework to self register services on Consul.IOgithub.com

Some articles about microservices:

Arquitetura de microserviços ou monolítica? — Blog da Caelum: desenvolvimento, web, mobile, UX e…
Dentro da Caelum, temos a experiência de termos criado várias aplicações web, seja para nós mesmos, para clientes ou…blog.caelum.com.br

De SOA a Microserviços: a evolução Arquitetural — Sensedia
A cada dia no universo da tecnologia são criadas novas teorias e tendências para conseguir construir e manter um novo…sensedia.com

Introdução à Arquitetura de Micro Serviços — LuizTools
Tem uns 2 anos que passei a ouvir falar de micro serviços (microservices) mas faz poucos meses que passei a me…www.luiztools.com.br

See you!

Automatically manage containers and microservices with better control and performance using Instana APM. Try it for yourself today.

Topics:
consul ,microservices ,c# ,monitoring ,megaphone ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}