1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
[root@iZcnl3jspkqf2xZ temp]# 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.
-alt-domain=<value>
Alternate domain to use for DNS interface.
-bind=0.0.0.0
指定consul所在机器的ip地址
-bootstrap
Sets server to bootstrap mode.
-bootstrap-expect=<value>
Sets server to expect bootstrap mode.
-check_output_max_size=<value>
Sets the maximum output size for checks on this agent
-client=127.0.0.1
哪些机器可以访问consul,默认是本机,0.0.0.0所有机器均可访问
-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>
储存所有注册过来srv机器详细信息
-dev
开发者模式,直接以默认配置启动consul
-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=8500
consul自带web访问的默认端口
-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
-log-rotate-max-files=<value>
Maximum number of log file archives to keep
-node=hostname
服务发现的名字,默认是hostname,服务发现也可以是集群
-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
启动的时候,加入到的consul集群
-server
以服务方式开启consul,允许其他的consul连接到开启的consul上,形成集群,如果不加-server,以客户端方式开启,不能bei
-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-port=<value>
Sets the server port to listen on.
-syslog
Enables logging to syslog.
-ui
Enables the built-in static web UI server.
-ui-content-path=<value>
Sets the external UI path to a string. Defaults to: /ui/
-ui-dir=<value>
Path to directory containing the web UI resources.
|
Windows
consul agent -server -bootstrap-expect 1 -data-dir C:\Temp\consul\data -node=n1 -bind=127.0.0.1 -ui -rejoin -config-dir=C:\Temp\consul\config -client 0.0.0.0
Linux
1
|
consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=n1 -bind=172.25.51.181 -ui -rejoin -config-dir=/etc/consul.d/ -client 0.0.0.0
|
[root@iZcnl3jspkqf2xZ ~]# consul members
Node Address Status Type Build Protocol DC Segment
n1 172.25.51.181:8301 alive server 1.5.3 2 dc1 <all>
-
consul info查看当前
-
consul leave 优雅关闭
/etc/consul.d
1
2
3
4
5
6
7
|
{
"service":{
"name":"test",
"tags":["ttt111","eee222"],
"port":9000
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
{
"service":{
"name":"mgv2reboot",
"tags":["mg","v2"],
"port":8848,
"address":"127.0.0.1",
"check":{
"id":"checkmgv2reboot",
"name":"MGV2Reboot API on port 8848",
"http":"http://127.0.0.1:8848/v0/4gtc200v1/reboot?sn=c",
"interval":"60s",
"timeout": "1s"
}
}
}
|
手动重启:consul reload
健康检查类型: Script HTTP TCP TTL
protoc --go_out=plugins=grpc:./ *.proto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
func main() {
// 初始化服务器对象
srv := service.New(
service.Name("xxx"), //服务器名
service.Version("latest"), // 版本
)
// 注册服务
pb.RegisterXxxHandler(srv.Server(), new(handler.Xxx))
// 运行服务
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}
|
stream和pingpong不用
1
2
3
4
5
6
7
8
|
type Xxx struct{}
// Call is a single request handler called via client.Call or the generated client code
func (e *Xxx) Call(ctx context.Context, req *xxx.Request, rsp *xxx.Response) error {
log.Info("Received Xxx.Call request")
rsp.Msg = "Hello " + req.Name
return nil
}
|