我们都知道使用 Docker Compose 进行容器编排时, depends_on 属性可以设置两个容器的依赖关系,只有当被依赖的容器启动后,依赖的容器才会去启动。例如:在 novel-cloud 项目中,novel-monitor 监控服务需要等待其它所有的微服务启动成功并注册到 nacos 后,才能在启动时读取到 nacos 中注册的服务信息,所以需要设置 novel-monitor 服务依赖于其它的所有微服务。

然而 depends_on 属性默认设置的是 Docker 容器启动的依赖关系,也就是只能保证当其它微服务的 Docker 容器启动后,才去启动 novel-monitor 容器,不能保证其它微服务的 Docker 容器内部的 java 进程完全启动后再去启动 novel-monitor 容器。

所以这个时候需要设置每个微服务的健康检查,检查内部的 java 进程是否已经成功启动,只有当每个微服务容器内部的 java 进程成功启动后才能去启动 novel-monitor 容器。

没有设置健康检查时的编排文件如下:

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
version: '3.9'

services:
# ...省略若干服务

novel-nacos-server:
container_name: novel-nacos-server
image: nacos/nacos-server:${NACOS_VERSION}
restart: always
hostname: novel-nacos-server
environment:
- PREFER_HOST_MODE=hostname
- MODE=standalone
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=novel-mysql
- MYSQL_SERVICE_DB_NAME=nacos
- MYSQL_SERVICE_PORT=3306
- MYSQL_SERVICE_USER=root
- MYSQL_SERVICE_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
- NACOS_AUTH_IDENTITY_KEY=xxyopen
- NACOS_AUTH_IDENTITY_VALUE=xxyopen
- NACOS_AUTH_TOKEN=SecretKey012345678901234567890123456789012345678901234567890123456789
- JVM_XMS=125M
- JVM_XMX=125M
- JVM_XMN=50M
- JVM_MS=50M
- JVM_MMS=50M
ports:
- "8848:8848"
- "9848:9848"
depends_on:
- novel-mysql
networks:
- novelnet


novel-gateway:
build: ./novel-gateway
image: novel-gateway:${NOVEL_CLOUD_VERSION}
container_name: novel-gateway
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "8888:8888"
depends_on:
- novel-nacos-server
networks:
- novelnet

novel-home-service:
build: ./novel-home-service
image: novel-home-service:${NOVEL_CLOUD_VERSION}
container_name: novel-home-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9001:9001"
depends_on:
- novel-nacos-server
networks:
- novelnet

novel-news-service:
build: ./novel-news-service
image: novel-news-service:${NOVEL_CLOUD_VERSION}
container_name: novel-news-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9030:9030"
depends_on:
- novel-nacos-server
networks:
- novelnet

novel-book-service:
build: ./novel-book-service
image: novel-book-service:${NOVEL_CLOUD_VERSION}
container_name: novel-book-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9020:9020"
depends_on:
- novel-nacos-server
networks:
- novelnet

novel-user-service:
build: ./novel-user-service
image: novel-user-service:${NOVEL_CLOUD_VERSION}
container_name: novel-user-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9060:9060"
depends_on:
- novel-nacos-server
networks:
- novelnet

novel-search-service:
build: ./novel-search-service
image: novel-search-service:${NOVEL_CLOUD_VERSION}
container_name: novel-search-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9050:9050"
depends_on:
- novel-nacos-server
networks:
- novelnet

novel-resource-service:
build: ./novel-resource-service
image: novel-resource-service:${NOVEL_CLOUD_VERSION}
container_name: novel-resource-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9040:9040"
depends_on:
- novel-nacos-server
networks:
- novelnet

novel-author-service:
build: ./novel-author-service
image: novel-author-service:${NOVEL_CLOUD_VERSION}
container_name: novel-author-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9010:9010"
depends_on:
- novel-nacos-server
networks:
- novelnet

novel-monitor:
build: ./novel-monitor
image: novel-monitor:${NOVEL_CLOUD_VERSION}
container_name: novel-monitor
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "8898:8898"
depends_on:
- novel-gateway
- novel-home-service
- novel-author-service
- novel-book-service
- novel-search-service
- novel-user-service
- novel-resource-service
- novel-news-service
networks:
- novelnet

nacos 注册中心服务注册情况如下:

novel-monitor 服务注册情况如下:

设置健康检查后的编排文件如下:

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
version: '3.9'

services:
# ...省略若干服务

novel-nacos-server:
container_name: novel-nacos-server
image: nacos/nacos-server:${NACOS_VERSION}
restart: always
hostname: novel-nacos-server
environment:
- PREFER_HOST_MODE=hostname
- MODE=standalone
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=novel-mysql
- MYSQL_SERVICE_DB_NAME=nacos
- MYSQL_SERVICE_PORT=3306
- MYSQL_SERVICE_USER=root
- MYSQL_SERVICE_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
- NACOS_AUTH_IDENTITY_KEY=xxyopen
- NACOS_AUTH_IDENTITY_VALUE=xxyopen
- NACOS_AUTH_TOKEN=SecretKey012345678901234567890123456789012345678901234567890123456789
- JVM_XMS=125M
- JVM_XMX=125M
- JVM_XMN=50M
- JVM_MS=50M
- JVM_MMS=50M
ports:
- "8848:8848"
- "9848:9848"
depends_on:
- novel-mysql
networks:
- novelnet
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -I http://localhost:8848 | grep -q 'HTTP/1.1 404'",
]
interval: 30s
timeout: 2s
retries: 120

novel-gateway:
build: ./novel-gateway
image: novel-gateway:${NOVEL_CLOUD_VERSION}
container_name: novel-gateway
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "8888:8888"
depends_on:
novel-nacos-server:
condition: service_healthy
networks:
- novelnet
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -I http://localhost:8888 | grep -q 'HTTP/1.1 404 Not Found'",
]
interval: 10s
timeout: 2s
retries: 120

novel-home-service:
build: ./novel-home-service
image: novel-home-service:${NOVEL_CLOUD_VERSION}
container_name: novel-home-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9001:9001"
depends_on:
novel-nacos-server:
condition: service_healthy
networks:
- novelnet
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -I http://localhost:9001 | grep -q 'HTTP/1.1 404 Not Found'",
]
interval: 10s
timeout: 2s
retries: 120

novel-news-service:
build: ./novel-news-service
image: novel-news-service:${NOVEL_CLOUD_VERSION}
container_name: novel-news-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9030:9030"
depends_on:
novel-nacos-server:
condition: service_healthy
networks:
- novelnet
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -I http://localhost:9030 | grep -q 'HTTP/1.1 404 Not Found'",
]
interval: 10s
timeout: 2s
retries: 120

novel-book-service:
build: ./novel-book-service
image: novel-book-service:${NOVEL_CLOUD_VERSION}
container_name: novel-book-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9020:9020"
depends_on:
novel-nacos-server:
condition: service_healthy
networks:
- novelnet
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -I http://localhost:9020 | grep -q 'HTTP/1.1 404 Not Found'",
]
interval: 10s
timeout: 2s
retries: 120

novel-user-service:
build: ./novel-user-service
image: novel-user-service:${NOVEL_CLOUD_VERSION}
container_name: novel-user-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9060:9060"
depends_on:
novel-nacos-server:
condition: service_healthy
networks:
- novelnet
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -I http://localhost:9060 | grep -q 'HTTP/1.1 404 Not Found'",
]
interval: 10s
timeout: 2s
retries: 120

novel-search-service:
build: ./novel-search-service
image: novel-search-service:${NOVEL_CLOUD_VERSION}
container_name: novel-search-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9050:9050"
depends_on:
novel-nacos-server:
condition: service_healthy
networks:
- novelnet
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -I http://localhost:9050 | grep -q 'HTTP/1.1 404 Not Found'",
]
interval: 10s
timeout: 2s
retries: 120

novel-resource-service:
build: ./novel-resource-service
image: novel-resource-service:${NOVEL_CLOUD_VERSION}
container_name: novel-resource-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9040:9040"
depends_on:
novel-nacos-server:
condition: service_healthy
networks:
- novelnet
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -I http://localhost:9040 | grep -q 'HTTP/1.1 404 Not Found'",
]
interval: 10s
timeout: 2s
retries: 120

novel-author-service:
build: ./novel-author-service
image: novel-author-service:${NOVEL_CLOUD_VERSION}
container_name: novel-author-service
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "9010:9010"
depends_on:
novel-nacos-server:
condition: service_healthy
networks:
- novelnet
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -I http://localhost:9010 | grep -q 'HTTP/1.1 404 Not Found'",
]
interval: 10s
timeout: 2s
retries: 120

novel-monitor:
build: ./novel-monitor
image: novel-monitor:${NOVEL_CLOUD_VERSION}
container_name: novel-monitor
restart: "always"
environment:
- JAR_VERSION=${NOVEL_CLOUD_VERSION}
- NACOS_ADDR=novel-nacos-server:8848
ports:
- "8898:8898"
depends_on:
novel-gateway:
condition: service_healthy
novel-home-service:
condition: service_healthy
novel-author-service:
condition: service_healthy
novel-book-service:
condition: service_healthy
novel-search-service:
condition: service_healthy
novel-user-service:
condition: service_healthy
novel-resource-service:
condition: service_healthy
novel-news-service:
condition: service_healthy
networks:
- novelnet

novel-monitor 服务注册情况如下: