Learn about Consul as an infrastructure component for service discovery and show its main functionality.
- [Instructor] And welcome back to Advanced Cloud Native Go. My name's Leander Reimer and I'm happy to be your host. So this section of the course will be all about Service Discovery and Configuration. And this is what we will learn. First of all, I will introduce Consul as an infrastructure component for service discovery and I will show you the basic usage of Consul. Next up, I will introduce you to the key value store of Consul and how it can be used as a central configuration server.
Then, we'll implement a very small microservice and register it programmatically with Consul. Next up, we'll implement a small client application that will use Consul to perform service lookups, and finally, we'll implement a pure Kubernetes version of service registration and lookup. So we'll change the microservice and implement discovery and configuration with Kubernetes mechanisms only. So, as usual, a lot of ground to cover in this section.
We better get started. Using Consul for Microservice Discovery. So in this video, we're going to take a look at the following three steps. First of all, we'll start and run Consul using Docker compose. Next up, we will re-register some services with Consul using the REST API, and in the final step, we'll perform a service lookup by using the Consul UI and also the REST API. So let's get started then.
So as usual, I have prepared my development environment. And in here, I've prepared now a Docker compose file. So what you see here is the required definition to start up Consul by using Docker compose. So I create a service definition, I specify the Consul Docker image, I need to specify the following three ports, I link Consul to Demo Microservices, and I link it to a network, and as you see down here, I am using the microservice from the previous section as an example, and we will register this service with Consul later on.
So just here I fire up the same microservice from the last section on Port 8080 and a second time on Port 9090. So, that's that. So let's open a Consul within this directory and I can say Docker compose up. So let's fire everything up.
So you see here the Consul is starting as well as our two microservices. Well, not much here, right? So what we can do now is open up a browser and navigate to the UI of Consul and this is Consul.
So you have services, notes, key values, ACL, and some more function. For now, we are only concerned with the services up here. Currently, only Consul itself is registered. So what we want to do next is use the REST API of Consul itself to register a few services. So let's open a REST client. So you see up here, this currently is the URL to access the service catalog.
If I do that, you see that we currently only have Consul registered. This is basically the same as the web view I just showed us. We can also access the services by the Agent API and you see here, you have some more information, so have an ID, the service name, the address, and the Port. So, OK, let's register a service for the first microservice.
I'm using the Agent Service Register API, and I'm hosting the following body. I give it an ID, I give it a name. You can specify additional tags. I specify an address, and in this case, I specify the host name where this microservice is running it. So this is gin-web-01, Port 8080, and I'm also specifying a live checks.
So in this case, I'm calling the ping http endpoint my microservices offering. Now this is really essential in a cloud native system, because the service registry needs to know if your service is healthy or not. So everything worked out, status OK, and if we go back, aha, see here? So now query for all the services, I do get the registered service back.
So if I go back to the web UI, you see I now have gin-web registered. So hopefully everything working here. All right. So let's register an additional service. Now I've registered here the second gin-web instance.
So if I go back here to the web UI, you now see that I have two instances running under the same name. Now if you have a look at the Consul, you see what's going on here. You see that Consul is constantly calling the ping URL of service-01 and service-02. So what I can do now in a second window, is for example, say, So I'm stopping one of the containers now.
And you should see up here that this one is failing now, OK? So if we go back to the UI, you see that one of the services became unhealthy. We can also use in REST API to get all information on the health checks, and you see here that this health check is failing, and we can use this parsing REST API to only get return the services where the health is OK.
You can also query for the critical states. Now, of course, using the REST API of Consul this way, is not the way we go from here, right? This is just to demonstrate how things work. So later on, we will use an API to register and de-register services instead. So in the next video, we're going to use Consul for Central Microservice Configuration. I hope to see you there. Bye-bye.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Cloud native application platforms
- Go frameworks and libraries for microservices
- Using Docker for containerization
- Using Kubernetes for orchestration
- Using Consul for microservice discovery and configuration
- Registration and lookup
- Implementing service discovery using Kubernetes
- Microservice communication patterns: Sync and async
- Using circuit breakers for resilient communication
- Implementing message queuing with Rabbitmq
- Using Apache Kafka for publish/subscribe