NuGet Package of the Week: Microphone registers and discovers Web APIs and REST services with Consul
I'm sitting on a plane on the way back from a lovely time in Europe. I attended and spoke at some great conferences and met some cool people - some of which you'll hear on the podcast soon. Anyway, one of the things that I heard mentioned by attendees more than once was the issue of (micro) service discovery for RESTful APIs. Now if you lived through the WS*.* years you'll perhaps feel a lot of this is familiar or repeated territory, but the new stuff definitely fits together more effortlessly than in the past.
Consul is a system that does service discovery, configuration management, and health checking for your services. You can write Web APIs in lots of things, Rails, Python, and ASP.NET with WebAPI or NancyFX.
Microphone is a library by Roger Johansson that plugs into both WebAPI and Nancy and very simply and easily registers your services with Consul. It's recently been expanded to support CoreOs-ETCD as well, so it's really a general purpose framework.
I made a little .NET 4.6 console app that self hosts a WebAPI like this.
namespace ConsulSelfHostedWebAPIService
{
class Program
{
static void Main(string[] args)
{
Cluster.Bootstrap(new WebApiProvider(), new ConsulProvider(), "HanselWebApiService", "v1");
Console.ReadLine();
}
}
public class DefaultController : ApiController
{
public string Get()
{
return "Hey it's my personal WebApi Service";
}
}
}
Now my Web API is registered with Consul, and now Consul itself is a RESTful Web API where I can hit http://localhost:8500/v1/agent/services and get a list of registered services. It's the Discovery Service.
Then later in a client or perhaps another Web API, I can ask for it by name and I'll get back the address and port that it's on, then call it.
var instance = await Cluster.FindServiceInstanceAsync("Heycool");
return String.Format("Look there's a service at {0}:{1}", instance.Address, instance.Port);
Here's an active debug session showing the address and port in the instance:
It will be interesting to see what will happen with Consul and systems like it if the Azure Service Fabric gains traction. Service Fabric offers a lot more, but I wonder if there is a use case for both, with Service Fabric managing lifecycles and Consul doing discovery.
This is all early days, but it's interesting. What do you think about these new discovery services for Web APIs?
Sponsor: Big thanks to Infragistics for sponsoring the feed this week. Quickly & effortlessly create advanced, stylish, & high performing UIs for ASP.NET MVC with Ignite UI. Leverage the full power of Infragistics’ JavaScript-based jQuery/HTML5 control suite today.
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
"A single server deployment is highly discouraged as data loss is inevitable in a failure scenario."
Would be great to get a Consul cluster option into the Azure Marketplace... anyone game? Hashicorp?
They highly discourage single server deployments because you'd be introducing a single point of failure to a network of microservices that will presumably come to depend on the service discovery that consul provides. Fault tolerance is the name of the game when you're spreading your business logic all over the place with microservices (for better or worse...) as networks will blip or partition, machines will reboot or crash, etc.
@scottt732 it would be much better if the service catalog was communicated peer to peer and as such need no central storage thereby removing all single points of failure. Of course this isn't possible without true service discovery or atleast a few nodes deemed superpeers that you can expect to be online to find atleast 1 other service.
As somebody you has starred at this problem before and blinked, I commend Roger for taking the time to write a solution, opensource it and then making an effort to evangelise it.
I'm sure Roger takes pull requests..?
Nice read!
Comments are closed.