# 4.3 配置中心
# 4.3.1 基础介绍
配置中心(config (opens new window))区别于启动配置(etc (opens new window))主要用于业务逻辑配置的读取、修改和监听。用法和启动配置(etc)完全一致,提供了 file (opens new window)、consul (opens new window)、etcd (opens new window)、nacos (opens new window) 等多种配置源方案。你可以根据自身业务特点选择不同的配置源,也可以同时组合使用多种配置源来构建自己的配置方案。
# 4.3.2 支持格式
配置中心默认支持 toml (opens new window)、yaml (opens new window)、json (opens new window)、xml (opens new window) 等多种文件格式。你可以根据自身喜好自由选择文件格式。
注:yaml格式的配置支持.yaml和.yml两种后缀名
# 4.3.3 读写规则
框架中的配置文件均是以 文件名[.参数名1[.参数名2...[.参数名n]]] 的方式进行读写的。
注:例如a.b.c.d.toml这样的配置文件同样也是被支持的。读写时你只需要将a.b.c.d看成一个完整的文件名即可。
# 4.3.4 如何使用
以下以nacos配置中心为例,介绍如何使用。其它配置中心使用方法请具体示例。
- 安装依赖
$ go get github.com/dobyte/due/v2@v2.4.2
$ go get github.com/dobyte/due/config/nacos/v2@e5cd009
2
- 导入依赖
import (
"github.com/dobyte/due/v2/config"
"github.com/dobyte/due/config/nacos/v2"
)
2
3
4
- 设置配置中心
config.SetConfigurator(config.NewConfigurator(config.WithSources(consul.NewSource())))
- 读取配置
timezone := config.Get("config.timezone", "UTC").String()
- 接口文档
https://pkg.go.dev/github.com/dobyte/due/v2/config (opens new window)
- 所有实现
# 4.3.5 file配置源示例
以下完整示例详见:config-file-example (opens new window)
- 创建项目
$ mkdir config-file-example
- 安装依赖
$ cd config-file-example
$ go mod init config-file-example
$ go get github.com/dobyte/due/v2@v2.4.2
2
3
- 启动配置
文件位置:config-file-example/etc/etc.toml (opens new window)
[config.file]
# 配置文件或配置目录路径
path = "./config"
# 读写模式。可选:read-only | write-only | read-write,默认为read-only
mode = "read-write"
2
3
4
5
- 编写示例
文件位置:config-file-example/main.go (opens new window)
package main
import (
"context"
"time"
"github.com/dobyte/due/v2/config"
"github.com/dobyte/due/v2/config/file"
"github.com/dobyte/due/v2/log"
)
const filename = "config.toml"
func main() {
// 设置file配置中心
config.SetConfigurator(config.NewConfigurator(config.WithSources(file.NewSource())))
// 更新配置
if err := config.Store(context.Background(), file.Name, filename, map[string]any{
"timezone": "Local",
}); err != nil {
log.Errorf("store config failed: %v", err)
return
}
// 等待配置文件写入
time.Sleep(time.Second)
// 读取配置
timezone := config.Get("config.timezone").String()
log.Infof("timezone: %s", timezone)
// 更新配置
if err := config.Store(context.Background(), file.Name, filename, map[string]any{
"timezone": "UTC",
}); err != nil {
log.Errorf("store config failed: %v", err)
return
}
// 等待配置文件写入
time.Sleep(time.Second)
// 读取配置
timezone = config.Get("config.timezone").String()
log.Infof("timezone: %s", timezone)
}
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
- 创建配置目录
$ cd config-file-example
$ mkdir config
2
- 运行示例
$ cd config-file-example
$ go run main.go
INFO[2025/10/29 18:59:14.685243] main.go:31 timezone: Local
INFO[2025/10/29 18:59:15.686111] main.go:46 timezone: UTC
2
3
4
# 4.3.6 etcd配置源示例
以下完整示例详见:config-etcd-example (opens new window)
- 创建项目
$ mkdir config-etcd-example
- 安装依赖
$ cd config-etcd-example
$ go mod init config-etcd-example
$ go get github.com/dobyte/due/v2@v2.4.2
$ go get github.com/dobyte/due/config/etcd/v2@e5cd009
2
3
4
- 启动配置
文件位置:config-etcd-example/etc/etc.toml (opens new window)
[config.etcd]
# 客户端连接地址,默认为["127.0.0.1:2379"]
addrs = ["127.0.0.1:2379"]
# 客户端拨号超时时间,支持单位:纳秒(ns)、微秒(us | µs)、毫秒(ms)、秒(s)、分(m)、小时(h)、天(d)。默认为5s
dialTimeout = "5s"
# 路径。默认为/config
path = "/config"
# 读写模式。可选:read-only | write-only | read-write,默认为read-only
mode = "read-write"
2
3
4
5
6
7
8
9
- 编写示例
文件位置:config-etcd-example/main.go (opens new window)
package main
import (
"context"
"time"
"github.com/dobyte/due/config/etcd/v2"
"github.com/dobyte/due/v2/config"
"github.com/dobyte/due/v2/log"
)
const filename = "config.toml"
func main() {
// 设置etcd配置中心
config.SetConfigurator(config.NewConfigurator(config.WithSources(etcd.NewSource())))
// 更新配置
if err := config.Store(context.Background(), etcd.Name, filename, map[string]any{
"timezone": "Local",
}); err != nil {
log.Errorf("store config failed: %v", err)
return
}
// 等待配置文件写入
time.Sleep(time.Second)
// 读取配置
timezone := config.Get("config.timezone").String()
log.Infof("timezone: %s", timezone)
// 更新配置
if err := config.Store(context.Background(), etcd.Name, filename, map[string]any{
"timezone": "UTC",
}); err != nil {
log.Errorf("store config failed: %v", err)
return
}
// 等待配置文件写入
time.Sleep(time.Second)
// 读取配置
timezone = config.Get("config.timezone").String()
log.Infof("timezone: %s", timezone)
}
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
- 运行示例
$ cd config-etcd-example
$ go run main.go
INFO[2025/10/29 19:55:02.575975] main.go:31 timezone: Local
INFO[2025/10/29 19:55:03.589461] main.go:46 timezone: UTC
2
3
4
# 4.3.7 nacos配置源示例
以下完整示例详见:config-nacos-example (opens new window)
- 创建项目
$ mkdir config-nacos-example
- 安装依赖
$ cd config-nacos-example
$ go mod init config-nacos-example
$ go get github.com/dobyte/due/v2@v2.4.2
$ go get github.com/dobyte/due/config/nacos/v2@e5cd009
2
3
4
- 启动配置
文件位置:config-nacos-example/etc/etc.toml (opens new window)
[config.nacos]
# 读写模式。可选:read-only | write-only | read-write,默认为read-only
mode = "read-write"
# 服务器地址 [scheme://]ip:port[/nacos]。默认为["http://127.0.0.1:8848/nacos"]
urls = ["http://127.0.0.1:8848/nacos"]
# 集群名称。默认为DEFAULT
clusterName = "DEFAULT"
# 群组名称。默认为DEFAULT_GROUP
groupName = "DEFAULT_GROUP"
# 请求Nacos服务端超时时间,支持单位:纳秒(ns)、微秒(us | µs)、毫秒(ms)、秒(s)、分(m)、小时(h)、天(d)。默认为3秒
timeout = "3s"
# ACM的命名空间Id。默认为空
namespaceId = ""
# 当使用ACM时,需要该配置,默认为空。详见:https://help.aliyun.com/document_detail/130146.html
endpoint = ""
# ACM&KMS的regionId,用于配置中心的鉴权。默认为空
regionId = ""
# ACM&KMS的AccessKey,用于配置中心的鉴权。默认为空
accessKey = ""
# ACM&KMS的SecretKey,用于配置中心的鉴权。默认为空
secretKey = ""
# 是否开启kms,同时DataId必须以"cipher-"作为前缀才会启动加解密逻辑。kms可以参考文档:https://help.aliyun.com/product/28933.html
openKMS = false
# 缓存service信息的目录。默认为./run/nacos/naming/cache
cacheDir = "./run/nacos/config/cache"
# Nacos服务端的API鉴权Username。默认为空
username = ""
# Nacos服务端的API鉴权Password。默认为空
password = ""
# 日志存储路径。默认为./run/nacos/naming/log
logDir = "./run/nacos/config/log"
# 日志输出级别,可选:debug、info、warn、error。默认为info
logLevel = "info"
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
- 编写示例
文件位置:config-nacos-example/main.go (opens new window)
package main
import (
"context"
"time"
"github.com/dobyte/due/config/nacos/v2"
"github.com/dobyte/due/v2/config"
"github.com/dobyte/due/v2/log"
)
const filename = "config.toml"
func main() {
// 设置nacos配置中心
config.SetConfigurator(config.NewConfigurator(config.WithSources(nacos.NewSource())))
// 更新配置
if err := config.Store(context.Background(), nacos.Name, filename, map[string]any{
"timezone": "Local",
}); err != nil {
log.Errorf("store config failed: %v", err)
return
}
// 等待配置文件写入
time.Sleep(time.Second)
// 读取配置
timezone := config.Get("config.timezone").String()
log.Infof("timezone: %s", timezone)
// 更新配置
if err := config.Store(context.Background(), nacos.Name, filename, map[string]any{
"timezone": "UTC",
}); err != nil {
log.Errorf("store config failed: %v", err)
return
}
// 等待配置文件写入
time.Sleep(time.Second)
// 读取配置
timezone = config.Get("config.timezone").String()
log.Infof("timezone: %s", timezone)
}
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
- 运行示例
$ cd config-nacos-example
$ go run main.go
INFO[2025/10/29 20:02:40.027839] main.go:31 timezone: Local
INFO[2025/10/29 20:02:41.059203] main.go:46 timezone: UTC
2
3
4
# 4.3.8 consul配置源示例
以下完整示例详见:config-consul-example (opens new window)
- 创建项目
$ mkdir config-consul-example
- 安装依赖
$ cd config-consul-example
$ go mod init config-consul-example
$ go get github.com/dobyte/due/v2@v2.4.2
$ go get github.com/dobyte/due/config/consul/v2@e5cd009
2
3
4
- 启动配置
文件位置:config-consul-example/etc/etc.toml (opens new window)
[config.consul]
# 客户端连接地址
addr = "127.0.0.1:8500"
# 路径。默认为config
path = "config"
# 读写模式。可选:read-only | write-only | read-write,默认为read-only
mode = "read-write"
2
3
4
5
6
7
- 编写示例
文件位置:config-consul-example/main.go (opens new window)
package main
import (
"context"
"time"
"github.com/dobyte/due/config/consul/v2"
"github.com/dobyte/due/v2/config"
"github.com/dobyte/due/v2/log"
)
const filename = "config.toml"
func main() {
// 设置consul配置中心
config.SetConfigurator(config.NewConfigurator(config.WithSources(consul.NewSource())))
// 更新配置
if err := config.Store(context.Background(), consul.Name, filename, map[string]any{
"timezone": "Local",
}); err != nil {
log.Errorf("store config failed: %v", err)
return
}
// 等待配置文件写入
time.Sleep(time.Second)
// 读取配置
timezone := config.Get("config.timezone").String()
log.Infof("timezone: %s", timezone)
// 更新配置
if err := config.Store(context.Background(), consul.Name, filename, map[string]any{
"timezone": "UTC",
}); err != nil {
log.Errorf("store config failed: %v", err)
return
}
// 等待配置文件写入
time.Sleep(time.Second)
// 读取配置
timezone = config.Get("config.timezone").String()
log.Infof("timezone: %s", timezone)
}
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
- 运行示例
$ cd config-consul-example
$ go run main.go
INFO[2025/10/29 20:07:57.659352] main.go:31 timezone: Local
INFO[2025/10/29 20:07:58.670298] main.go:46 timezone: UTC
2
3
4