[root@localhost ~]# consul --help
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
acl Interact with Consul's ACLs
agent Runs a Consul agent
catalog Interact with the catalog
config Interact with Consul's Centralized Configurations
connect Interact with Consul Connect
debug Records a debugging archive for operators
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
intention Interact with Connect service intentions
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
login Login to Consul using an auth method
logout Destroy a Consul token created with login
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
services Interact with services
snapshot Saves, restores and inspects snapshots of Consul server state
tls Builtin helpers for creating CAs and certificates
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
[root@localhost ~]# consul agent -h
Usage: consul agent [options]
Starts the Consul agent and runs until an interrupt is received. The
agent represents a single node in a cluster.
HTTP API Options
-datacenter=<value>
Datacenter of the agent.
Command Options
-advertise=<value>
Sets the advertise address to use.
-advertise-wan=<value>
Sets address to advertise on WAN instead of -advertise address.
-allow-write-http-from=<value>
Only allow write endpoint calls from given network. CIDR format,
can be specified multiple times.
-bind=<value>
Sets the bind address for cluster communication.
-bootstrap
Sets server to bootstrap mode.
-bootstrap-expect=<value>
Sets server to expect bootstrap mode.
-client=<value>
Sets the address to bind for client access. This includes RPC, DNS,
HTTP, HTTPS and gRPC (if configured).
-config-dir=<value>
Path to a directory to read configuration files from. This
will read every file ending in '.json' as configuration in this
directory in alphabetical order. Can be specified multiple times.
-config-file=<value>
Path to a file in JSON or HCL format with a matching file
extension. Can be specified multiple times.
-config-format=<value>
Config files are in this format irrespective of their extension.
Must be 'hcl' or 'json'
-data-dir=<value>
Path to a data directory to store agent state.
-dev
Starts the agent in development mode.
-disable-host-node-id
Setting this to true will prevent Consul from using information
from the host to generate a node ID, and will cause Consul to
generate a random node ID instead.
-disable-keyring-file
Disables the backing up of the keyring to a file.
-dns-port=<value>
DNS port to use.
-domain=<value>
Domain to use for DNS interface.
-enable-local-script-checks
Enables health check scripts from configuration file.
-enable-script-checks
Enables health check scripts.
-encrypt=<value>
Provides the gossip encryption key.
-grpc-port=<value>
Sets the gRPC API port to listen on (currently needed for Envoy xDS
only).
-hcl=<value>
hcl config fragment. Can be specified multiple times.
-http-port=<value>
Sets the HTTP API port to listen on.
-join=<value>
Address of an agent to join at start time. Can be specified
multiple times.
-join-wan=<value>
Address of an agent to join -wan at start time. Can be specified
multiple times.
-log-file=<value>
Path to the file the logs get written to
-log-level=<value>
Log level of the agent.
-log-rotate-bytes=<value>
Maximum number of bytes that should be written to a log file
-log-rotate-duration=<value>
Time after which log rotation needs to be performed
-node=<value>
Name of this node. Must be unique in the cluster.
-node-id=<value>
A unique ID for this node across space and time. Defaults to a
randomly-generated ID that persists in the data-dir.
-node-meta=<key:value>
An arbitrary metadata key/value pair for this node, of the format
`key:value`. Can be specified multiple times.
-non-voting-server
(Enterprise-only) This flag is used to make the server not
participate in the Raft quorum, and have it only receive the data
replication stream. This can be used to add read scalability to
a cluster in cases where a high volume of reads to servers are
needed.
-pid-file=<value>
Path to file to store agent PID.
-protocol=<value>
Sets the protocol version. Defaults to latest.
-raft-protocol=<value>
Sets the Raft protocol version. Defaults to latest.
-recursor=<value>
Address of an upstream DNS server. Can be specified multiple times.
-rejoin
Ignores a previous leave and attempts to rejoin the cluster.
-retry-interval=<value>
Time to wait between join attempts.
-retry-interval-wan=<value>
Time to wait between join -wan attempts.
-retry-join=<value>
Address of an agent to join at start time with retries enabled. Can
be specified multiple times.
-retry-join-wan=<value>
Address of an agent to join -wan at start time with retries
enabled. Can be specified multiple times.
-retry-max=<value>
Maximum number of join attempts. Defaults to 0, which will retry
indefinitely.
-retry-max-wan=<value>
Maximum number of join -wan attempts. Defaults to 0, which will
retry indefinitely.
-segment=<value>
(Enterprise-only) Sets the network segment to join.
-serf-lan-bind=<value>
Address to bind Serf LAN listeners to.
-serf-lan-port=<value>
Sets the Serf LAN port to listen on.
-serf-wan-bind=<value>
Address to bind Serf WAN listeners to.
-serf-wan-port=<value>
Sets the Serf WAN port to listen on.
-server
Switches agent to server mode.
-server-port=<value>
Sets the server port to listen on.
-syslog
Enables logging to syslog.
-ui
Enables the built-in static web UI server.
-ui-dir=<value>
Path to directory containing the web UI resources.
dig @127.0.0.1 -p 8600 web.service.consul
mkdir consul.d
echo ‘{“service”:{“name”:“web”,“tags”:[“rails”],“port”:80}}’ >> /root/consul.d/web.json
consul agent -dev -config-dir=/root/consul.d -node=machine
[root@localhost ~]# dig @127.0.0.1 -p 8600 web.service.consul
[root@localhost ~]# dig @127.0.0.1 -p 8600 web.service.consul SRV
[root@localhost ~]# dig @127.0.0.1 -p 8600 rails.web.service.consul
[root@localhost ~]# curl http://localhost:8500/v1/catalog/service/web
[
{
"ID": "382ef20d-e753-ff1a-ace1-1527202241a6",
"Node": "machine",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceKind": "",
"ServiceID": "web",
"ServiceName": "web",
"ServiceTags": [
"rails"
],
"ServiceAddress": "",
"ServiceWeights": {
"Passing": 1,
"Warning": 1
},
"ServiceMeta": {},
"ServicePort": 80,
"ServiceEnableTagOverride": false,
"ServiceProxyDestination": "",
"ServiceProxy": {},
"ServiceConnect": {},
"CreateIndex": 10,
"ModifyIndex": 10
}
]
[root@localhost ~]# curl 'http://localhost:8500/v1/health/service/web?passing'
[
{
"Node": {
"ID": "382ef20d-e753-ff1a-ace1-1527202241a6",
"Node": "machine",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"Meta": {
"consul-network-segment": ""
},
"CreateIndex": 9,
"ModifyIndex": 10
},
"Service": {
"ID": "web",
"Service": "web",
"Tags": [
"rails"
],
"Address": "",
"Meta": null,
"Port": 80,
"Weights": {
"Passing": 1,
"Warning": 1
},
"EnableTagOverride": false,
"ProxyDestination": "",
"Proxy": {},
"Connect": {},
"CreateIndex": 10,
"ModifyIndex": 10
},
"Checks": [
{
"Node": "machine",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"ServiceTags": [],
"Definition": {},
"CreateIndex": 9,
"ModifyIndex": 9
}
]
}
]
什么是服务发现
客户端应用进程向注册中心发起查询,来获取服务的位置。服务发现的一个重要作用就是提供一个可用的服务列表,微服务的框架体系中,必须要存在的一个体系就是服务发现,对于服务进行管理,以及发现。
1,服务A-N把当前自己的网络位置注册到服务发现模块(服务注册)
2,健康检查服务发现模块定时的轮询查看这些服务能不能访问(健康检查)
3,客户端在调用服务A-N的时候,就先去服务发现模块查询它们的网络位置,然后再去调用它们的目标服务。
什么是consul
consul是一个支持多数据中分布式高可用的服务发现和配置共享的服务软件。
consul提供的一些关键特性:
service discovery:consul通过DNS或者HTTP接口使服务注册和服务发现。
health checking:健康检查使consul可以快速的警告在集群中的操作,和服务发现的集成,可以防止请求转发到故障的服务上。
key/value storage:一个用来存储动态配置的系统,提供简单的http接口,可以在任何地方操作。
consul的角色
client角色
consul的client模式,是consul节点的一种,在client模式下,所有注册到当前节点的服务会转发到server,本身是不持久化这些信息的。
特点:
1,只接受注册,并不会存储消息。
2,注册服务,转发到server。
服务注册到注册中心,当客户端请求的时候获取到健康的服务,返回健康服务的地址,客户端调用健康服务的地址。
server角色
server表示consul运行在server模式,功能和client模式一样,唯一不同的是,它会把所有的信息持久化到本地,这样遇到故障,信息是可以被保留。
特点:
1,持久化信息。
server-leader角色
server下面有leader字眼,表明这个server是它们的老大,它和其他server不一样的一点是,它需要负责同步注册的信息给其他server,同时负责各个节点的健康检查。
特点:
1,同步注册服务。
2,健康检查。
3,持久化信息。