Trivializing Microservice Communication
A couple of months ago I embarked on my journey of developing a Microservice architecture base web application with all (most) of the bells and whistles enterprise applications incorporate in today’s software landscape. I set out on this journey with the hopes of learning new technologies and techniques that would improve my development skills for the better. With microservices comes it’s own set of complex problems to cater for, having to implement various small services to communicate well and reliably with one another can be tricky. Whilst I was developing (still am) I came across this neat little library that makes this task communicating between microservices easy and fun to implement.
To get started, create a public interface which represents the endpoint in your API you wish to invoke requests on. For a little background, I have an API gateway project that acts as a single point of entry for my application. This API would then interact with my many microservices to handle requests. Below is an example of an interface I implemented for my API project to forward these customer specific requests to my Customers microservice using RestEase.We add the Get attribute to the GetCustomer method to indicate the verb and the HTTP endpoint path that will be used by this method. This is not much different from creating an endpoint in an API as well as specifying query parameters.
Once you have you interfaces setup, just create an instance with the URL of the service you would like to use:
Or, if you like to keep things tidy and readable, like me, I created a couple of extension methods to do so.
Using the methods above, you could use DI to the save keystrokes on creating the clients every time you want to use them and instead do so by injecting each interface.
The following line could then be added to you Startup.cs file:
In addition to these basic functionality I described above, RestEase is extremely configurable. Next time you’re consuming an API, try this library and you’ll end up spending getting down to business rather that just the fluff.