MICROSERVICES .NET CORE WITH EXAMPLE — Part 7
We did talk about using Ocelot for API Gateway in previous article, however there are some disadvantages of registering each service downstream host and port. In the real cases, you can’t manually configure the actual endpoints and make sure they’re all correct. The services’ endpoints should be configured dynamically. That’s why we need Service Discovery.
What is Service Discovery?
Service discovery is the process of automatically detecting devices and services on a network. Service discovery aims to reduce the configuration efforts from users
There are two types of service discovery: server-side service discovery and client-side service discovery.
- Server-side service discovery: allow clients to find services through a load balancer or a router. The clients will contact the load balancer / router, then make a request that indicates which service type it needs. The load balancer / router will consult the service registry, then select the suitable service and route the request to it
- Client-side service discovery: contact the service registry, receive the available services detail, and then contact one of them through the load balancer / router algorithm
Service Discovery components
There are three components: service provider, service consumer and service registry.
- Service provider: registers itself with service registry when it enters and de-register itself when it leaves the system
- Service consumer: get the location of a provider from registry, and then talks to the provider
- Service registry: one of the most important part of Service Discovery because all transaction (register and de-register) will be stored on Service Registry. It needs to be highly available and up to date
Implement Service Discovery using Consul
Consul is a service networking solution to connect and secure services across any run-time platform and public or private cloud. It provides four main features
- Service discovery
- Health check
- Key value store
- Multi-data server
We will focus on Service discovery implementation in this article (other features will be mentioned in other topics. Be patient 😏)
Let’s see how the implementation is visualized partly in KCommerce project
We will implement client-side discovery for our services. Let’s start!
1. Install Consul
- Download Consul here
- Run Consul with
agent --dev
arguments. This will boot Consul in local service mode without the need of a configuration file and will be accessible on localhost only - Open http://localhost:8500 to view the Consul UI
2. Install Consul package for API Gateway and other services
We need one more package for API Gateway
3. Add Consul configuration to each service
Define one section in appsettings.json file
All name keys can be defined differently, this is just my suggestion for the names
- Host: the Consul server address
- ServiceName: registered name of the current service. Notice that of you define the same name for other services, that means they are other instances of the same service
- ServiceId: unique ID of the current service
4. Create an extension to register Consul for all services
Then register Consul in Startup.cs file in each service
5. Use Service discovery in API gateway project
Open ocelot.json file and add these configurations
As you see, we don’t have to define the downstream host and ports any more, instead we use service discovery by “Name” — catalogService
The following is required in the GlobalConfiguration. The Provider is required and if you do not specify a host and port the Consul default will be used
You can see more service discovery in Ocelot here.
6. Add Consul to Ocelot in Startup.cs file (API gateway)
And that’s all. Let’s see the result
1 . Run Catalog service
2. Check Consul server
You can see “catalogService” is register there.
3. Then let’s start the gateway and see whether we can call Catalog service through it
Wow, that’s cool, right?
You can get the source codes in Github
Conclusion
Service discovery is cool to build Microservices architecture. According to Consul, a great, flexible and stable tool, our works are reduced a lot. However you should understand what you want to build for your architecture in order to make it right. Cheer!
Part 8: Communication between microservices