How to use Consul in Production Server for Service Discoveries

HashiCorp's consul is the good pick for service mesh and service discovery. While developing microservices, we need some service discoveries mechanism so that our services/Web-APIs get registered themselves with a centralized service-repository. A Web API gateway (like ocelot etc.) then can use the consul to serve the request.

Using Consul in Production Deployment

In a production deployment, we need to install consul on each node(host/machine/container) where our services are and we want them to register with consul. The Consul can run in two modes i.e.

  1. server-agent (maintains the state) and
  2. client-agent (light-weight process, must be installed on each & every node where our microservices are running).

The Consul, needs to be running up constantly for the Production servers and for that have two ways-

  1. let the consul up and running from a command prompt window
  2. install consul as a windows service

The first solution looks very awkward. If by accidently someone closes that command prompt window, consul will stop working. So, we can think for the second approach that is installing consul as a windows service.

How to install consul as a Windows Service?

  1. Download the consul from here-
  2. Unzip the consul in a folder where you want to keep the consul and its config
  3. Go to that folder and create a folder lets name it "config"
  4. Inside the "config" folder, create a "config.json" file and put below content-
    "datacenter": "east-aws",
    "data_dir": "c:\\consul\\config",
    "log_level": "INFO",
    "node_name": "WebServer",
    "server": true,
    "bind_addr": "",
    "bootstrap": true,
    "ui_config": {"enabled": true},
    "log_file": "c:\\consul\\log\\consul.log"

  5. Run the below command from the command prompt to install the consul as a windows service-
     sc.exe create "Consul" binPath= "c:\\consul\\Consul.exe agent -config-dir=c:\\consul\\config" start= auto
  6. Then to start consul service, type below command-
     sc.exe start Consul
  7. Go the services and check if your consul is up and running


That's it!