使用 Docker 部署 Nacos

本文主要分享如何使用 Docker 部署 Nacos

如果您是第一次阅读该系列文章,建议优先阅读 Docker 部署项目说明及索引

Docker Compose

以下是 Docker Compose 示例:(基于 Nacos 3.2.3)

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

services:
  nacos:
    image: nacos/nacos-server:latest
    container_name: nacos
    hostname: nacos
    restart: unless-stopped
    ports:
      - "8080:8080"
      - "8848:8848"
      - "9848:9848"
    volumes:
#      - /docker/nacos/conf/application.properties:/home/nacos/conf/application.properties  # 如需外置数据源则打开此配置
      - /docker/nacos/data:/home/nacos/data
      - /docker/nacos/logs:/home/nacos/logs
    environment:
      - TZ=Asia/Shanghai
      - PREFER_HOST_MODE=hostname
      - MODE=standalone
      - FUNCTION_MODE=microservice
      - NACOS_AUTH_IDENTITY_KEY=${your_nacos_server_identity_key}
      - NACOS_AUTH_IDENTITY_VALUE=${your_nacos_server_identity_value}
      - NACOS_AUTH_TOKEN=${your_nacos_auth_secret_token}
networks:
  default:
    external: true
    name: rod

配置说明

  • NACOS_AUTH_TOKEN: Nacos 用于生成 JWT Token 的密钥,使用长度大于 32 字符的字符串,再经过 Base64 编码。
  • NACOS_AUTH_IDENTITY_KEY: Nacos Server 端之间 Inner API 的身份标识的 Key,必填。
  • NACOS_AUTH_IDENTITY_VALUE: Nacos Server 端之间 Inner API 的身份标识的 Value,必填。

更多配置参考官网文档:系统参数

外置数据源说明

以 PostgreSQL 数据库为例:

  1. 创建 PostgreSQL 用户名及数据库
  2. 根据 nacos/conf/pg-schema.sql (Nacos 3.2.3 可根据附录: PostgreSQL 查看,其他版本可下载官方 nacos 安装包查看),手动创建数据库表等数据
  3. 创建 application.properties 文件,并将上述 yml 中 volumes 相关注释打开。

配置文件主要修改如下内容:(注意:不同 nacos 版本相关配置可能不同,具体请参考官方文档。)

1
2
3
4
5
6
7
spring.sql.init.platform=postgresql
db.num=1
db.url.0=jdbc:postgresql://127.0.0.1:5432/nacos
db.user=nacos
db.password=nacos
db.pool.config.driverClassName=org.postgresql.Driver
db.pool.config.connectionTestQuery=SELECT 1

端口说明

Nacos 3.0 起,控制台端口与服务器端口解耦。默认端口如下:

端口 用途 暴露建议
8080 Nacos Console 端口 只向管理网络开放
8848 HTTP API 端口,用于 OpenAPI、Admin API 和部分插件请求 只向内部客户端、控制台或网关开放
9848 客户端 gRPC 端口 只向内部客户端开放,VIP/SLB 需要 TCP 转发
9849 服务端 gRPC 通信端口 只允许 Nacos Server 节点互通
7848 Raft 通信端口 只允许 Nacos Server 节点互通

使用 VIP、SLB 或 Nginx 时,9848 必须按 TCP 转发,不要按 HTTP 或 HTTP/2 代理。更多端口说明请参考部署手册概览

验证 Nacos 服务是否启动成功

通过 docker logs -f $container_id 命令,查看 Nacos 服务启动日志,如果看到如下日志,说明服务启动成功。

1
Nacos started successfully in xxx mode. use xxx storage

可通过如下方式,快速检验 Nacos 的功能。

Nacos 控制台页面

打开链接 http://localhost:8080,即可进入 Nacos 控制台页面。

注意:首次打开会要求初始化管理员用户 nacos 的密码。

服务注册

1
curl -X POST 'http://127.0.0.1:8848/nacos/v3/client/ns/instance?serviceName=quickstart.test.service&ip=127.0.0.1&port=8080'

服务发现

1
curl -X GET 'http://127.0.0.1:8848/nacos/v3/client/ns/instance/list?serviceName=quickstart.test.service'

发布配置

1
2
3
4
# 登陆获取access token
curl -X POST 'http://127.0.0.1:8848/nacos/v3/auth/user/login' -d 'username=nacos' -d 'password=${your_password}'
# 使用access token,创建配置
curl -X POST 'http://127.0.0.1:8848/nacos/v3/admin/cs/config?dataId=quickstart.test.config&groupName=test&content=HelloWorld' -H "accessToken:${your_access_token}"

注意:访问 Admin API,默认需要使用 access token,access token 可通过 POST /nacos/v3/auth/user/login 接口获得

获取配置

1
curl -X GET 'http://127.0.0.1:8848/nacos/v3/client/cs/config?dataId=quickstart.test.config&groupName=test'

NPM 反向代理

如果需要通过 NPM 设置反向代理,可参考如下方式:

注意:不推荐将 Nacos 映射到公网访问,仅做学习参考。

此处以 https://nacos.example.com 为例

如果未部署 NPM,可参考文章:使用 Docker 部署 Nginx Proxy Manager

Details

  • Domain Names: nacos.example.com
  • Scheme: http
  • Forward Hostname/IP: 127.0.0.1 (如果 NPM 未使用 HOST,则使用容器的 hostname: nacos)
  • Forward Port: 8080
  • Cache Assets: ✅

SSL

  • 选择你已经配置好的 SSL Certificate,如 *.example.com, example.com
  • Force SSL: ✅
  • HTTP/2 Support: ✅
  • HSTS Enabled: ✅
  • HSTS Subdomains: ✅

注意:勾选 HSTS 设置,默认访问 https://nacos.example.com 时会跳转到 https://nacos.example.com/next ,如果不勾选 HSTS,会跳转到 http://nacos.example.com/next

Advanced

1
2
3
4
5
6
7
8
location / {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_pass $forward_scheme://$server:$port;
}

参考网址

附录

PostgreSQL DDL

pg-schema.sql

  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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

-- ----------------------------
-- Table structure for config_info
-- ----------------------------
DROP TABLE IF EXISTS "config_info";
CREATE TABLE "config_info" (
  "id" bigserial NOT NULL,
  "data_id" varchar(255)  NOT NULL,
  "group_id" varchar(255) ,
  "content" text  NOT NULL,
  "md5" varchar(32) ,
  "gmt_create" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "gmt_modified" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "src_user" text ,
  "src_ip" varchar(20) ,
  "app_name" varchar(128) ,
  "tenant_id" varchar(128) NOT NULL DEFAULT '',
  "c_desc" varchar(256) ,
  "c_use" varchar(64) ,
  "effect" varchar(64) ,
  "type" varchar(64) ,
  "c_schema" text ,
  "encrypted_data_key" text  NOT NULL
)
;

COMMENT ON COLUMN "config_info"."id" IS 'id';
COMMENT ON COLUMN "config_info"."data_id" IS 'data_id';
COMMENT ON COLUMN "config_info"."content" IS 'content';
COMMENT ON COLUMN "config_info"."md5" IS 'md5';
COMMENT ON COLUMN "config_info"."gmt_create" IS '创建时间';
COMMENT ON COLUMN "config_info"."gmt_modified" IS '修改时间';
COMMENT ON COLUMN "config_info"."src_user" IS 'source user';
COMMENT ON COLUMN "config_info"."src_ip" IS 'source ip';
COMMENT ON COLUMN "config_info"."tenant_id" IS '租户字段';
COMMENT ON COLUMN "config_info"."encrypted_data_key" IS '秘钥';
COMMENT ON TABLE "config_info" IS 'config_info';

-- ----------------------------
-- Table structure for config_info_gray
-- ----------------------------
DROP TABLE IF EXISTS "config_info_gray";
CREATE TABLE "config_info_gray" (
  "id" bigserial NOT NULL,
  "data_id" varchar(255) NOT NULL,
  "group_id" varchar(128) NOT NULL,
  "content" text NOT NULL,
  "md5" varchar(32),
  "src_user" text,
  "src_ip" varchar(100),
  "gmt_create" timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "gmt_modified" timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "app_name" varchar(128),
  "tenant_id" varchar(128) NOT NULL DEFAULT '',
  "gray_name" varchar(128) NOT NULL,
  "gray_rule" text NOT NULL,
  "encrypted_data_key" varchar(256) NOT NULL DEFAULT ''
);

COMMENT ON COLUMN "config_info_gray"."id" IS 'id';
COMMENT ON COLUMN "config_info_gray"."data_id" IS 'data_id';
COMMENT ON COLUMN "config_info_gray"."group_id" IS 'group_id';
COMMENT ON COLUMN "config_info_gray"."content" IS 'content';
COMMENT ON COLUMN "config_info_gray"."md5" IS 'md5';
COMMENT ON COLUMN "config_info_gray"."src_user" IS 'source user';
COMMENT ON COLUMN "config_info_gray"."src_ip" IS 'source ip';
COMMENT ON COLUMN "config_info_gray"."gmt_create" IS '创建时间';
COMMENT ON COLUMN "config_info_gray"."gmt_modified" IS '修改时间';
COMMENT ON COLUMN "config_info_gray"."app_name" IS 'app_name';
COMMENT ON COLUMN "config_info_gray"."tenant_id" IS '租户字段';
COMMENT ON COLUMN "config_info_gray"."gray_name" IS '灰度名称';
COMMENT ON COLUMN "config_info_gray"."gray_rule" IS '灰度规则';
COMMENT ON COLUMN "config_info_gray"."encrypted_data_key" IS '秘钥';
COMMENT ON TABLE "config_info_gray" IS 'config_info_gray';

-- 创建索引
CREATE UNIQUE INDEX "uk_configinfogray_datagrouptenantgray" ON "config_info_gray" USING btree ("data_id", "group_id", "tenant_id", "gray_name");
CREATE INDEX "idx_dataid_gmt_modified_gray" ON "config_info_gray" USING btree ("data_id", "gmt_modified");
CREATE INDEX "idx_gmt_modified_gray" ON "config_info_gray" USING btree ("gmt_modified");

ALTER TABLE "config_info_gray" ADD CONSTRAINT "config_info_gray_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Records of config_info_gray
-- ----------------------------
BEGIN;
COMMIT;


-- ----------------------------
-- Table structure for config_tags_relation
-- ----------------------------
DROP TABLE IF EXISTS "config_tags_relation";
CREATE TABLE "config_tags_relation" (
  "id" bigint NOT NULL,
  "tag_name" varchar(128)  NOT NULL,
  "tag_type" varchar(64) ,
  "data_id" varchar(255)  NOT NULL,
  "group_id" varchar(128)  NOT NULL,
  "tenant_id" varchar(128) NOT NULL DEFAULT '',
  "nid" bigserial NOT NULL
)
;
COMMENT ON COLUMN "config_tags_relation"."id" IS 'id';
COMMENT ON COLUMN "config_tags_relation"."tag_name" IS 'tag_name';
COMMENT ON COLUMN "config_tags_relation"."tag_type" IS 'tag_type';
COMMENT ON COLUMN "config_tags_relation"."data_id" IS 'data_id';
COMMENT ON COLUMN "config_tags_relation"."group_id" IS 'group_id';
COMMENT ON COLUMN "config_tags_relation"."tenant_id" IS 'tenant_id';
COMMENT ON TABLE "config_tags_relation" IS 'config_tag_relation';

-- ----------------------------
-- Records of config_tags_relation
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for group_capacity
-- ----------------------------
DROP TABLE IF EXISTS "group_capacity";
CREATE TABLE "group_capacity" (
  "id" bigserial NOT NULL,
  "group_id" varchar(128)  NOT NULL,
  "quota" int4 NOT NULL,
  "usage" int4 NOT NULL,
  "max_size" int4 NOT NULL,
  "max_aggr_count" int4 NOT NULL,
  "max_aggr_size" int4 NOT NULL,
  "max_history_count" int4 NOT NULL DEFAULT 0,
  "gmt_create" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "gmt_modified" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
)
;
COMMENT ON COLUMN "group_capacity"."id" IS '主键ID';
COMMENT ON COLUMN "group_capacity"."group_id" IS 'Group ID,空字符表示整个集群';
COMMENT ON COLUMN "group_capacity"."quota" IS '配额,0表示使用默认值';
COMMENT ON COLUMN "group_capacity"."usage" IS '使用量';
COMMENT ON COLUMN "group_capacity"."max_size" IS '单个配置大小上限,单位为字节,0表示使用默认值';
COMMENT ON COLUMN "group_capacity"."max_aggr_count" IS '聚合子配置最大个数,,0表示使用默认值';
COMMENT ON COLUMN "group_capacity"."max_aggr_size" IS '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值';
COMMENT ON COLUMN "group_capacity"."max_history_count" IS '最大变更历史数量';
COMMENT ON COLUMN "group_capacity"."gmt_create" IS '创建时间';
COMMENT ON COLUMN "group_capacity"."gmt_modified" IS '修改时间';
COMMENT ON TABLE "group_capacity" IS '集群、各Group容量信息表';

-- ----------------------------
-- Records of group_capacity
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for his_config_info
-- ----------------------------
DROP TABLE IF EXISTS "his_config_info";
CREATE TABLE "his_config_info" (
  "id" int8 NOT NULL,
  "nid" bigserial NOT NULL,
  "data_id" varchar(255)  NOT NULL,
  "group_id" varchar(128)  NOT NULL,
  "app_name" varchar(128) ,
  "content" text  NOT NULL,
  "md5" varchar(32) ,
  "gmt_create" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "gmt_modified" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "src_user" text ,
  "src_ip" varchar(20) ,
  "op_type" char(10) ,
  "tenant_id" varchar(128) NOT NULL DEFAULT '',
  "encrypted_data_key" text  NOT NULL,
  "publish_type" varchar(50) DEFAULT 'formal',
  "gray_name" varchar(50),
  "ext_info" text
)
;
COMMENT ON COLUMN "his_config_info"."app_name" IS 'app_name';
COMMENT ON COLUMN "his_config_info"."tenant_id" IS '租户字段';
COMMENT ON COLUMN "his_config_info"."encrypted_data_key" IS '秘钥';
COMMENT ON COLUMN "his_config_info"."publish_type" IS 'publish type gray or formal';
COMMENT ON COLUMN "his_config_info"."gray_name" IS 'gray name';
COMMENT ON COLUMN "his_config_info"."ext_info" IS 'ext info';
COMMENT ON TABLE "his_config_info" IS '多租户改造';


-- ----------------------------
-- Table structure for permissions
-- ----------------------------
DROP TABLE IF EXISTS "permissions";
CREATE TABLE "permissions" (
  "role" varchar(50)  NOT NULL,
  "resource" varchar(512)  NOT NULL,
  "action" varchar(8)  NOT NULL
)
;

-- ----------------------------
-- Records of permissions
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for roles
-- ----------------------------
DROP TABLE IF EXISTS "roles";
CREATE TABLE "roles" (
  "username" varchar(50)  NOT NULL,
  "role" varchar(50)  NOT NULL
)
;

-- ----------------------------
-- Records of roles
-- ----------------------------
-- No seed rows: MySQL schema also leaves `roles` empty so the console can run first-time admin setup.
-- A row like ('nacos', 'ROLE_ADMIN') blocks that flow when the DB account name is `nacos` (common JDBC user).
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for tenant_capacity
-- ----------------------------
DROP TABLE IF EXISTS "tenant_capacity";
CREATE TABLE "tenant_capacity" (
  "id" bigserial NOT NULL,
  "tenant_id" varchar(128)  NOT NULL,
  "quota" int4 NOT NULL,
  "usage" int4 NOT NULL,
  "max_size" int4 NOT NULL,
  "max_aggr_count" int4 NOT NULL,
  "max_aggr_size" int4 NOT NULL,
  "max_history_count" int4 NOT NULL DEFAULT 0,
  "gmt_create" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "gmt_modified" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
)
;
COMMENT ON COLUMN "tenant_capacity"."id" IS '主键ID';
COMMENT ON COLUMN "tenant_capacity"."tenant_id" IS 'Tenant ID';
COMMENT ON COLUMN "tenant_capacity"."quota" IS '配额,0表示使用默认值';
COMMENT ON COLUMN "tenant_capacity"."usage" IS '使用量';
COMMENT ON COLUMN "tenant_capacity"."max_size" IS '单个配置大小上限,单位为字节,0表示使用默认值';
COMMENT ON COLUMN "tenant_capacity"."max_aggr_count" IS '聚合子配置最大个数';
COMMENT ON COLUMN "tenant_capacity"."max_aggr_size" IS '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值';
COMMENT ON COLUMN "tenant_capacity"."max_history_count" IS '最大变更历史数量';
COMMENT ON COLUMN "tenant_capacity"."gmt_create" IS '创建时间';
COMMENT ON COLUMN "tenant_capacity"."gmt_modified" IS '修改时间';
COMMENT ON TABLE "tenant_capacity" IS '租户容量信息表';

-- ----------------------------
-- Records of tenant_capacity
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for tenant_info
-- ----------------------------
DROP TABLE IF EXISTS "tenant_info";
CREATE TABLE "tenant_info" (
  "id" bigserial NOT NULL,
  "kp" varchar(128)  NOT NULL,
  "tenant_id" varchar(128) ,
  "tenant_name" varchar(128) ,
  "tenant_desc" varchar(256) ,
  "create_source" varchar(32) ,
  "gmt_create" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "gmt_modified" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
)
;
COMMENT ON COLUMN "tenant_info"."id" IS 'id';
COMMENT ON COLUMN "tenant_info"."kp" IS 'kp';
COMMENT ON COLUMN "tenant_info"."tenant_id" IS 'tenant_id';
COMMENT ON COLUMN "tenant_info"."tenant_name" IS 'tenant_name';
COMMENT ON COLUMN "tenant_info"."tenant_desc" IS 'tenant_desc';
COMMENT ON COLUMN "tenant_info"."create_source" IS 'create_source';
COMMENT ON COLUMN "tenant_info"."gmt_create" IS '创建时间';
COMMENT ON COLUMN "tenant_info"."gmt_modified" IS '修改时间';
COMMENT ON TABLE "tenant_info" IS 'tenant_info';

-- ----------------------------
-- Records of tenant_info
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS "users";
CREATE TABLE "users" (
  "username" varchar(50)  NOT NULL,
  "password" varchar(500)  NOT NULL,
  "enabled" boolean NOT NULL
)
;

-- ----------------------------
-- Records of users
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Indexes structure for table config_info
-- ----------------------------
CREATE UNIQUE INDEX "uk_configinfo_datagrouptenant" ON "config_info" ("data_id","group_id","tenant_id");

-- ----------------------------
-- Primary Key structure for table config_info
-- ----------------------------
ALTER TABLE "config_info" ADD CONSTRAINT "config_info_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Indexes structure for table config_tags_relation
-- ----------------------------
CREATE INDEX "idx_tenant_id" ON "config_tags_relation" USING btree (
  "tenant_id"
);
CREATE UNIQUE INDEX "uk_configtagrelation_configidtag" ON "config_tags_relation" USING btree (
  "id",
  "tag_name",
  "tag_type"
);

-- ----------------------------
-- Primary Key structure for table config_tags_relation
-- ----------------------------
ALTER TABLE "config_tags_relation" ADD CONSTRAINT "config_tags_relation_pkey" PRIMARY KEY ("nid");

-- ----------------------------
-- Indexes structure for table group_capacity
-- ----------------------------
CREATE UNIQUE INDEX "uk_group_id" ON "group_capacity" USING btree (
  "group_id"
);

-- ----------------------------
-- Primary Key structure for table group_capacity
-- ----------------------------
ALTER TABLE "group_capacity" ADD CONSTRAINT "group_capacity_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Indexes structure for table his_config_info
-- ----------------------------
CREATE INDEX "idx_did" ON "his_config_info" USING btree (
  "data_id"
);
CREATE INDEX "idx_gmt_create" ON "his_config_info" USING btree (
  "gmt_create"
);
CREATE INDEX "idx_gmt_modified" ON "his_config_info" USING btree (
  "gmt_modified"
);

-- ----------------------------
-- Primary Key structure for table his_config_info
-- ----------------------------
ALTER TABLE "his_config_info" ADD CONSTRAINT "his_config_info_pkey" PRIMARY KEY ("nid");

-- ----------------------------
-- Indexes structure for table permissions
-- ----------------------------
CREATE UNIQUE INDEX "uk_role_permission" ON "permissions" USING btree (
  "role",
  "resource",
  "action"
);

-- ----------------------------
-- Indexes structure for table roles
-- ----------------------------
CREATE UNIQUE INDEX "uk_username_role" ON "roles" USING btree (
  "username",
  "role"
);

-- ----------------------------
-- Indexes structure for table tenant_capacity
-- ----------------------------
CREATE UNIQUE INDEX "uk_tenant_id" ON "tenant_capacity" USING btree (
  "tenant_id"
);

-- ----------------------------
-- Primary Key structure for table tenant_capacity
-- ----------------------------
ALTER TABLE "tenant_capacity" ADD CONSTRAINT "tenant_capacity_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Indexes structure for table tenant_info
-- ----------------------------
CREATE UNIQUE INDEX "uk_tenant_info_kptenantid" ON "tenant_info" USING btree (
  "kp",
  "tenant_id"
);

-- ----------------------------
-- Table structure for pipeline_execution
-- ----------------------------
DROP TABLE IF EXISTS "pipeline_execution";
CREATE TABLE "pipeline_execution" (
  "execution_id"  varchar(64)  NOT NULL,
  "resource_type" varchar(32)  NOT NULL,
  "resource_name" varchar(256) NOT NULL,
  "namespace_id"  varchar(128) DEFAULT NULL,
  "version"       varchar(64)  DEFAULT NULL,
  "status"        varchar(32)  NOT NULL,
  "pipeline"      text         NOT NULL,
  "create_time"   bigint       NOT NULL,
  "update_time"   bigint       NOT NULL
);
COMMENT ON COLUMN "pipeline_execution"."execution_id" IS '执行ID';
COMMENT ON COLUMN "pipeline_execution"."resource_type" IS '资源类型';
COMMENT ON COLUMN "pipeline_execution"."resource_name" IS '资源名称';
COMMENT ON COLUMN "pipeline_execution"."namespace_id" IS '命名空间ID';
COMMENT ON COLUMN "pipeline_execution"."version" IS '版本';
COMMENT ON COLUMN "pipeline_execution"."status" IS '执行状态';
COMMENT ON COLUMN "pipeline_execution"."pipeline" IS 'pipeline节点结果JSON';
COMMENT ON COLUMN "pipeline_execution"."create_time" IS '创建时间';
COMMENT ON COLUMN "pipeline_execution"."update_time" IS '修改时间';
COMMENT ON TABLE "pipeline_execution" IS 'AI资源发布审核Pipeline执行记录';

-- ----------------------------
-- Primary Key structure for table pipeline_execution
-- ----------------------------
ALTER TABLE "pipeline_execution" ADD CONSTRAINT "pipeline_execution_pkey" PRIMARY KEY ("execution_id");

-- ----------------------------
-- Table structure for ai_resource
-- ----------------------------
DROP TABLE IF EXISTS "ai_resource";
CREATE TABLE "ai_resource" (
  "id" bigserial NOT NULL,
  "gmt_create" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "gmt_modified" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "name" varchar(256) NOT NULL,
  "type" varchar(32) NOT NULL,
  "c_desc" varchar(1024),
  "status" varchar(32),
  "namespace_id" varchar(128) NOT NULL DEFAULT '',
  "biz_tags" varchar(1024),
  "ext" text,
  "c_from" varchar(256) NOT NULL DEFAULT 'local',
  "version_info" text,
  "meta_version" bigint NOT NULL DEFAULT 1,
  "scope" varchar(16) NOT NULL DEFAULT 'PRIVATE',
  "owner" varchar(128) NOT NULL DEFAULT '',
  "download_count" bigint NOT NULL DEFAULT 0
)
;

COMMENT ON COLUMN "ai_resource"."id" IS 'id';
COMMENT ON COLUMN "ai_resource"."gmt_create" IS '创建时间';
COMMENT ON COLUMN "ai_resource"."gmt_modified" IS '修改时间';
COMMENT ON COLUMN "ai_resource"."name" IS '资源名称';
COMMENT ON COLUMN "ai_resource"."type" IS '资源类型';
COMMENT ON COLUMN "ai_resource"."c_desc" IS '资源描述';
COMMENT ON COLUMN "ai_resource"."status" IS '资源状态';
COMMENT ON COLUMN "ai_resource"."namespace_id" IS '命名空间ID';
COMMENT ON COLUMN "ai_resource"."biz_tags" IS '业务标签';
COMMENT ON COLUMN "ai_resource"."ext" IS '扩展信息(JSON)';
COMMENT ON COLUMN "ai_resource"."c_from" IS '来源标识(导入/同步来源)';
COMMENT ON COLUMN "ai_resource"."version_info" IS '版本信息(JSON)';
COMMENT ON COLUMN "ai_resource"."meta_version" IS '元数据版本(乐观锁)';
COMMENT ON COLUMN "ai_resource"."scope" IS '可见性: PUBLIC/PRIVATE';
COMMENT ON COLUMN "ai_resource"."owner" IS '创建者用户名';
COMMENT ON COLUMN "ai_resource"."download_count" IS '下载次数';
COMMENT ON TABLE "ai_resource" IS 'AI资源元数据表';

-- ----------------------------
-- Primary Key structure for table ai_resource
-- ----------------------------
ALTER TABLE "ai_resource" ADD CONSTRAINT "ai_resource_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Indexes structure for table ai_resource
-- ----------------------------
CREATE UNIQUE INDEX "uk_ai_resource_ns_name_type" ON "ai_resource" USING btree (
  "namespace_id",
  "name",
  "type",
  "c_from"
);
CREATE INDEX "idx_ai_resource_name" ON "ai_resource" USING btree ("name");
CREATE INDEX "idx_ai_resource_type" ON "ai_resource" USING btree ("type");
CREATE INDEX "idx_ai_resource_gmt_modified" ON "ai_resource" USING btree ("gmt_modified");

-- ----------------------------
-- Table structure for ai_resource_version
-- ----------------------------
DROP TABLE IF EXISTS "ai_resource_version";
CREATE TABLE "ai_resource_version" (
  "id" bigserial NOT NULL,
  "gmt_create" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "gmt_modified" timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "type" varchar(32) NOT NULL,
  "author" varchar(128),
  "name" varchar(256) NOT NULL,
  "c_desc" varchar(1024),
  "status" varchar(32) NOT NULL,
  "version" varchar(64) NOT NULL,
  "namespace_id" varchar(128) NOT NULL DEFAULT '',
  "storage" text,
  "publish_pipeline_info" text,
  "download_count" bigint NOT NULL DEFAULT 0
)
;

COMMENT ON COLUMN "ai_resource_version"."id" IS 'id';
COMMENT ON COLUMN "ai_resource_version"."gmt_create" IS '创建时间';
COMMENT ON COLUMN "ai_resource_version"."gmt_modified" IS '修改时间';
COMMENT ON COLUMN "ai_resource_version"."type" IS '资源类型';
COMMENT ON COLUMN "ai_resource_version"."author" IS '作者';
COMMENT ON COLUMN "ai_resource_version"."name" IS '资源名称';
COMMENT ON COLUMN "ai_resource_version"."c_desc" IS '版本描述';
COMMENT ON COLUMN "ai_resource_version"."status" IS '版本状态';
COMMENT ON COLUMN "ai_resource_version"."version" IS '版本号';
COMMENT ON COLUMN "ai_resource_version"."namespace_id" IS '命名空间ID';
COMMENT ON COLUMN "ai_resource_version"."storage" IS '存储信息(JSON)';
COMMENT ON COLUMN "ai_resource_version"."publish_pipeline_info" IS '发布流水线信息(JSON)';
COMMENT ON COLUMN "ai_resource_version"."download_count" IS '下载次数';
COMMENT ON TABLE "ai_resource_version" IS 'AI资源版本表';

-- ----------------------------
-- Primary Key structure for table ai_resource_version
-- ----------------------------
ALTER TABLE "ai_resource_version" ADD CONSTRAINT "ai_resource_version_pkey" PRIMARY KEY ("id");

-- ----------------------------
-- Indexes structure for table ai_resource_version
-- ----------------------------
CREATE UNIQUE INDEX "uk_ai_resource_ver_ns_name_type_ver" ON "ai_resource_version" USING btree (
  "namespace_id",
  "name",
  "type",
  "version"
);
CREATE INDEX "idx_ai_resource_ver_name" ON "ai_resource_version" USING btree ("name");
CREATE INDEX "idx_ai_resource_ver_status" ON "ai_resource_version" USING btree ("status");
CREATE INDEX "idx_ai_resource_ver_gmt_modified" ON "ai_resource_version" USING btree ("gmt_modified");

pg-upgrade-null-tenant-id.sql

 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
/*
 * Copyright 1999-2026 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

-- PostgreSQL upgrade script for deployments that still contain nullable tenant_id
-- values in config-related tables.
--
-- IMPORTANT:
-- 1. Review and clean duplicate logical rows before replacing NULL tenant_id
--    values with '' on config_info, otherwise the UPDATE may conflict with the
--    existing unique index on (data_id, group_id, tenant_id).
-- 2. Apply this script before upgrading to a Nacos build that validates the
--    PostgreSQL tenant schema on startup.
--
-- Suggested pre-check for duplicate logical config rows:
-- SELECT data_id, group_id, COUNT(*)
-- FROM config_info
-- WHERE tenant_id IS NULL
-- GROUP BY data_id, group_id
-- HAVING COUNT(*) > 1;

ALTER TABLE config_info ALTER COLUMN tenant_id SET DEFAULT '';
UPDATE config_info SET tenant_id = '' WHERE tenant_id IS NULL;
ALTER TABLE config_info ALTER COLUMN tenant_id SET NOT NULL;

ALTER TABLE config_info_gray ALTER COLUMN tenant_id SET DEFAULT '';
UPDATE config_info_gray SET tenant_id = '' WHERE tenant_id IS NULL;
ALTER TABLE config_info_gray ALTER COLUMN tenant_id SET NOT NULL;

ALTER TABLE config_tags_relation ALTER COLUMN tenant_id SET DEFAULT '';
UPDATE config_tags_relation SET tenant_id = '' WHERE tenant_id IS NULL;
ALTER TABLE config_tags_relation ALTER COLUMN tenant_id SET NOT NULL;

ALTER TABLE his_config_info ALTER COLUMN tenant_id SET DEFAULT '';
UPDATE his_config_info SET tenant_id = '' WHERE tenant_id IS NULL;
ALTER TABLE his_config_info ALTER COLUMN tenant_id SET NOT NULL;

pg-grant-nacos-readwrite.sql

 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
/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Optional: grant read/write on existing Nacos tables to application role `nacos` only.
 *
 * When to use:
 *   Load pg-schema.sql as a DBA/superuser (tables then belong to that role). Create the app user:
 *     CREATE USER nacos WITH PASSWORD 'your-password';
 *   Then run this script while connected to the Nacos database as the same role that owns the tables
 *   (often superuser), so privileges apply to all current objects.
 *
 * What you get:
 *   USAGE on public schema, SELECT/INSERT/UPDATE/DELETE on all tables, USAGE+SELECT on sequences
 *   (covers bigserial / identity). No CREATE/DROP on schema, no superuser.
 *
 * CONNECT: use `psql -d your_nacos_db` so the session can open the DB, or from another database run:
 *   GRANT CONNECT ON DATABASE your_nacos_db TO nacos;
 */

GRANT USAGE ON SCHEMA public TO nacos;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO nacos;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO nacos;

-- Objects created later by the same owner (e.g. future migrations run as superuser):
ALTER DEFAULT PRIVILEGES IN SCHEMA public
  GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO nacos;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
  GRANT USAGE, SELECT ON SEQUENCES TO nacos;

如果本文对您有所帮助,欢迎打赏支持作者!

Licensed under CC BY-NC-SA 4.0
最后更新于 2026-08-12 13:24
使用 Hugo 构建
主题 StackJimmy 设计