TIPS
- This article is based on Consul 1.5.3, The theory applies to Consul 1.6 And earlier .
- Install stand-alone Consul See :《 Install stand-alone Consul》
Knowledge preheating
Consul Common commands
| command | explain | Example |
|---|---|---|
| agent | Run one consul agent | consul agent -dev |
| join | take agent Add to consul colony | consul join IP |
| members | List consul cluster In the cluster members | consul members |
| leave | Remove the node from the cluster | consul leave |
Reference documents
Here are just a few common commands ,consul There are nearly 20 An order , This article is not expanded , See :https://www.consul.io/docs/commands/index.html
consul agent Detailed command
Common options
consul agent Common options for commands are as follows :
- -data-dir
- effect : Appoint agent A directory of stored data
- This is all. agent It's all necessary
- about server Especially important , Because they have to persist the state of the cluster
- -config-dir
- effect : Appoint service The location of the configuration file and check definition for
- It is usually designated as ” A certain path /consul.d”( Usually ,.d Represents the directory where a series of configuration files are stored )
- -config-file
- effect : Specify a profile to load
- This option can be configured multiple times , And then configure multiple configuration files ( The back one will merge the front one , The same value covers )
- -dev
- effect : Create a development environment server node
- Under this parameter configuration , There will be no persistence operations , That is, no data is written to the disk
- This pattern cannot be used in production environments ( Because the second one )
- -bootstrap-expect
- effect : Order notification consul server We are now ready to join server Number of nodes , This parameter is used to delay the start of log replication until we specify the number of server Start after the node successfully joins .
- -node
- effect : Specify the name of the node in the cluster
- The name must be unique in the cluster ( The default is machine host)
- recommend : Direct use of machine IP
- -bind
- effect : Specify the IP Address
- Sometimes no binding is specified IP, Will be submitted to the
Failed to get advertise address: Multiple private IPs found. Please configure one.It's abnormal
- -server
- effect : Specify the node as server
- Every data center (DC) Of server The number recommended is at least 1, At most 5
- be-all server All use raft Consistency algorithm to ensure the consistency and linearization of transactions , The transaction modifies the state of the cluster , And the state of the cluster is saved in each server To ensure availability
- server Also with other DC The facade of interaction (gateway)
- -client
- effect : Specify the node as client, Specify the binding address of the client interface , Include :HTTP、DNS、RPC
- The default is 127.0.0.1, Only loopback interfaces are allowed to access
- If not specified as -server, In fact, that is -client
- -join
- effect : Add nodes to the cluster
- -datacenter( The old version is called -dc,-dc It's invalid )
- effect : Specify which data center the machine will join
Example 1: Command parameter mode execution
1 |
consul agent -data-dir /tmp/node2 -node=node2 -bind=192.168.11.145 -datacenter=dc1 -ui -client=192.168.11.145 |
Example 2: Profile mode execution
Prepare a profile , Any name , for example consul.json, The contents are as follows :
1 |
{ |
Startup time , Give Way consul Read configuration file :
1 |
consul agent -dev -config-file="consul-config/consul.json" |
Reference documents
consul agent Yes 20 Here's an option , This article lists only the common options , Other options are not expanded , See : https://www.consul.io/docs/agent/options.html
Consul Cluster building

Hosting plan
Prepare three CentOS 7 Virtual machine , The host is planned as follows :
| Host name | IP | effect | Whether remote access is allowed |
|---|---|---|---|
| node0 | 192.168.11.143 | consul server | yes |
| node1 | 192.168.11.144 | consul client | no |
| node2 | 192.168.11.145 | consul client | yes |
Steps to build
- start-up node0 On the machine Consul(node0 Executed on machine ):
1 |
consul agent -data-dir /tmp/node0 -node=node0 -bind=192.168.11.143 -datacenter=dc1 -ui -client=192.168.11.143 -server -bootstrap-expect 1 |
- start-up node1 On the machine Consul(node1 Executed on machine ):
1 |
consul agent -data-dir /tmp/node1 -node=node1 -bind=192.168.11.144 -datacenter=dc1 -ui |
- start-up node2 On the machine Consul(node2 Executed on machine ):
1 |
consul agent -data-dir /tmp/node2 -node=node2 -bind=192.168.11.145 -datacenter=dc1 -ui -client=192.168.11.145 |
- take node1 Node join to node0 On (node1 Executed on machine ):
1 |
consul join 192.168.11.143 |
- take node2 Node join to node0 On (node2 Executed on machine ):
1 |
consul join -rpc-addr=192.168.11.145:8400 192.168.11.143 |
- It's a simple one Consul The cluster is set up , stay node1 View the current cluster node on :
1 |
consul members -rpc-addr=192.168.11.143:8400 |
give the result as follows :
1 |
Node Address Status Type Build Protocol DC |
It indicates that the cluster has been successfully built .
Let's analyze , Why the 5 And the first step 6 Step needs to add -rpc-addr Options , And the first 4 Step does not need to add any options ? as a result of -client The binding address of the client interface is specified , Include :HTTP、DNS、RPC, and consul join 、consul members It's all through RPC And Consul The interaction of .
Access test
Above , We've added... To all three nodes -ui Parameters start the built-in interface . We can go through :http://192.168.11.143:8500/ui/ perhaps http://192.168.11.145:8500/ui/ Visit , It can also be in node1 Machine pass http://127.0.0.1:8500/ui/ Visit , as a result of node1 Remote access is not turned on , The results of the three access methods are the same .
I have to read the article
Related articles
- Use Consul Implementing service discovery :instance-id Customize
- Install stand-alone Consul
- use git2consul from Git Synchronize configuration to Consul
- Spring Cloud Ecological configuration server most complete contrast paste
- Follow me Spring Cloud(Finchley edition )-03- monitor : Powerful Spring Boot Actuator







