HashiCorp
Consul was developed by HashiCorp, a company focused on DevOps tool chain with star products including Vagrant, Terraform, Vault, Nomad, and Consul throughout the continuous delivery process.
download
Consul’s official website provides information about download methods for different operating systems
$ brew tap hashicorp/tap
$ brew install hashicorp/tap/consu
Copy the code
Windows provides compressed packages of binary-executable programs in 32-bit and 64-bit
The Linux system provides the corresponding package manager download method
- Ubuntu/Debian
$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
$ sudo apt-get update && sudo apt-get install consul
Copy the code
- CentOS/RHEL
$ sudo yum install -y yum-utils
$ sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
$ sudo yum -y install consul
Copy the code
- Fedora
$ sudo dnf install -y dnf-plugins-core
$ sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
$ sudo dnf -y install consul
Copy the code
- Amazon Linux
$ sudo yum install -y yum-utils
$ sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
$ sudo yum -y install consul
Copy the code
- Homebrew
$ brew tap hashicorp/tap
$ brew install hashicorp/tap/consul
Copy the code
Binary download links are also provided for the corresponding operational architecture
- 32-bit
- 64-bit
- Arm64
- Armelv5
- Armhfv6
Consul is also open source software written by Golang, which can be found on Github and compiled by yourself.
$ git clone https://github.com/hashicorp/consul.git -depth=1
$ cd consul
$ make tools
$ make dev
Copy the code
Run Server&Client
Looking back at Consul getting Started, we know that Consul is a C/S architecture with server-side capabilities running Server
$ consul agent --server=true --ui=true --data-dir=s-data --node=server1 --dev
==> Starting Consul agent...
Version: '1.9.4'
Node ID: '34d5bfeb-3ed4-6aa1-f68d-d30950450fc9'
Node name: 'server1'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
==> Log data will now stream in as it occurs:
...
Copy the code
From the output, you can see some key information about the Server node and the successful election of the Leader (after all, it is the only one) to run the Client
$Consul agent --data-dir=c-data --join=127.0.0.1:8301 --serf-lan-port=8303 --serf-wan-port=8305 --dns-port=8601 --server-port=8304 --http-port=8503 --server=false --enable-script-checks --node=client1 --config-dir=config==> Starting Consul agent... Version: '1.9.4' Node ID: '2b05EE18-1b09-3b0b-7431-ef79783D9758' Node name: 'client1' Datacenter: 'dC1' (Segment: ") Server: false (Bootstrap: false) Client Addr: [127.0.0.1] (HTTP: 8503, HTTPS: -1, gRPC: -1, DNS: 8601) Cluster Addr: 10.8.124.122 (LAN: 8303, WAN: 8305) Encrypt: Gossip: false, TLS-outgoing: false, TLS-incoming: false, Auto-Encrypt-TLS: false ...Copy the code
The program also displays some information about the Client. Since the Server and Client are running on the same machine, the default port conflicts with the Server. So the parameter –serf-lan-port=8303 — Serf-wan-port =8305 — dnS-port =8601 –server-port=8304 –http-port=8503 is specified.
The connection information between Client1 and the Server is as follows:
2021-08-29T16:47:10.385+0800 [DEBUG] Agent. Router. Manager: Rebalanced Servers, new Active Server: Number_of_servers =1 Active_server ="server1 (Addr: TCP /127.0.0.1:8300) (DC: Dc1)" 2021-08-29T16:47:10.385+0800 [DEBUG] Agent. Router. Manager: Rebalanced Servers, new Active Server: Number_of_servers =1 Active_server ="server1.dc1 (Addr: TCP /127.0.0.1:8300) (DC: [DEBUG] Agent: Skipping Remote Check since it is automatically managed: Check = serfHealth T16 2021-08-29:48:04. 265 + 0800 / DEBUG agent: [DEBUG] Agent: Skipping Remote Check since it is managed automatically: Check = serfHealth T16 2021-08-29: overthrow. 849 + 0800 / DEBUG agent: Node info in sync the T16 2021-08-29: overthrow. 993 + 0800 / DEBUG agent. The server memberlist. LAN: memberlist: Stream Connection from=127.0.0.1:54711 2021-08-29t16:49:18.993 +0800 [INFO] agent.server.serf. LAN: serf: EventMemberJoin: Client1 10.8.124.122 2021-08-29T16:49:18.993+0800 [INFO] Agent. Server: client1 10.8.124.122 2021-08-29T16:49:18.993+0800 Member joined, marking Health Alive: member= Client1 2021-08-29T16:49:19.176+0800 [DEBUG] Agent.server.serf. LAN: serf: [DEBUG] Agent.server.serf. LAN: Serf: messageJoinType: client1 2021-08-29T16:49:19.192+0800 Client1 2021-08-29T16:49:19.294+0800 [DEBUG] Agent.server.serf. LAN: Serf: messageJoinType: client1 2021-08-29T16:49:19.294+0800 Client1 2021-08-29T16:49:19.393+0800 [DEBUG] Agent.server.serf. LAN: Serf: messageJoinType: client1Copy the code
Consul command to view the current membership status:
$ consul members
Node Address Status Type Build Protocol DC Segment
server1 127.0.0.1:8301 alive server 1.9.4 2 dc1 <all>
client1 10.8.124.122:8303 alive client 1.9.4 2 dc1 <default>
Copy the code
Sign up for a service
Let’s pause the Client code with Ctrl-C and add the file redis. Json to the config folder
{
"service": [{"name": "redis"."tags": ["master"]."address": "127.0.0.1"."port": 6379,}}]Copy the code
Then run it again and you can see that the server Redis service has been registered
2021-08-29T17:03:04.556+0800 [INFO] Agent: Synced node INFO 2021-08-29T17:03:04.556+0800 [INFO] Agent: Synced node INFO service=redisCopy the code
To verify that it can be found on Consul’s DNS service
$Dig @127.0.0.1 -p 8601 redis.sevice. Consul; <<>> DiG 9.10.6 <<>> @127.0.0.1 -p 8601 redis.sevice. Consul; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 39225 ;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ; redis.sevice.consul. IN A ;; AUTHORITY SECTION: consul. 0 IN SOA ns.consul. hostmaster.consul. 1630227874 3600 600 86400 0 ;; Query time: 0 msec ;; SERVER: 127.0.0.1 # 8601 (127.0.0.1);; WHEN: Sun Aug 29 17:04:34 CST 2021 ;; MSG SIZE rcvd: 98Copy the code
In addition, captured by the DNS service can be accessed through HTTP service http://localhost:8503/v1/health/service/redis
conclusion
This article describes the company information behind Consul, Consul installation and Consul operation mode, finally through the registration of Redis service example, experience a simple service registration and discovery process.