NOTE

consul

Consul provides service registration and discovery. These notes include server and client configuration and examples of registration from Go programs.

Go1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What It Is

A component for service registration and discovery.

2. Setting Up a Single-Machine Cluster

2.1. Download

2.2. Configuration Files

2.2.1. Create Directories

mkdir -p conf/{node1,node2,node3}
mkdir -p data/{node1,node2,node3}

2.2.2. Configuration Files

  • server1
{
  "datacenter": "dc1",
  "data_dir": "C:\\software\\consul_1.7.2\\data\\node1",
  "log_level": "INFO",
  "server": true,
  "node_name": "node1",
  "ui": true,
  "bind_addr": "127.0.0.1",
  "client_addr": "127.0.0.1",
  "advertise_addr": "127.0.0.1",
  "bootstrap_expect": 3,
  "ports":{
    "http": 8500,
    "dns": 8600,
    "server": 8300,
    "serf_lan": 8301,
    "serf_wan": 8302
    }
}
  • server2
{
  "datacenter": "dc1",
  "data_dir": "C:\\software\\consul_1.7.2\\data\\node2",
  "log_level": "INFO",
  "server": true,
  "node_name": "node2",
  "bind_addr": "127.0.0.1",
  "client_addr": "127.0.0.1",
  "advertise_addr": "127.0.0.1",
  "bootstrap_expect": 3,
  "ports":{
    "http": 8510,
    "dns": 8610,
    "server": 8310,
    "serf_lan": 8311,
    "serf_wan": 8312
    }
}
  • server3
{
  "datacenter": "dc1",
  "data_dir": "C:\\software\\consul_1.7.2\\data\\node3",
  "log_level": "INFO",
  "server": true,
  "node_name": "node3",
  "bind_addr": "127.0.0.1",
  "client_addr": "127.0.0.1",
  "advertise_addr": "127.0.0.1",
  "bootstrap_expect": 3,
  "ports":{
    "http": 8520,
    "dns": 8620,
    "server": 8320,
    "serf_lan": 8321,
    "serf_wan": 8322
    }
}

2.3. Start

  • server1
consul agent  -config-dir C:\\software\\consul_1.7.2\conf\\node1
  • server2
consul agent  -config-dir C:\\software\\consul_1.7.2\conf\\node2 -retry-join=127.0.0.1:8301
  • server3
consul agent  -config-dir C:\\software\\consul_1.7.2\conf\\node3 -retry-join=127.0.0.1:8301

2.4. Test

  • Add a service configuration file
{
  "service": {
     "name": "web",
     "tags": ["master"],
     "address": "127.0.0.1",
     "port": 10000,
     "checks": [
       {
         "http": "http://localhost:10000/health",
         "interval": "10s"
       }
     ]
  }
}
  • Add a service
package main

import (
	"fmt"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Println("hello Web3! This is n3 or n2")
	fmt.Fprintf(w, "Hello Web3! This is n3 or n2")
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Println("health check! n3 or n2")
}
func main() {
	http.HandleFunc("/", handler)
	http.HandleFunc("/health", healthHandler)
	http.ListenAndServe(":10000", nil)
}
  • web UI

3. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub