- Docs
- Extensions
- Config
配置中心
基础介绍
配置中心( config )区别于启动配置( etc )主要用于业务逻辑配置的读取、修改和监听。用法和启动配置(etc)完全一致,提供了 file 、 consul 、 etcd 、 nacos 等多种配置源方案。你可以根据自身业务特点选择不同的配置源,也可以同时组合使用多种配置源来构建自己的配置方案。
支持格式
配置中心默认支持 toml 、 yaml 、 json 、 xml 等多种文件格式。你可以根据自身喜好自由选择文件格式。
注:yaml 格式的配置支持.yaml 和.yml 两种后缀名
读写规则
框架中的配置文件均是以 文件名[.参数名 1[.参数名 2...[.参数名 n]]] 的方式进行读写的。
注:例如 a.b.c.d.toml 这样的配置文件同样也是被支持的。读写时你只需要将 a.b.c.d 看成一个完整的文件名即可。
如何使用
以下以 nacos 配置中心为例,介绍如何使用。其它配置中心使用方法请具体示例。
- 安装依赖
$ go get github.com/dobyte/due/v2@v2.4.2
$ go get github.com/dobyte/due/config/nacos/v2@e5cd009- 导入依赖
import (
"github.com/dobyte/due/v2/config"
"github.com/dobyte/due/config/nacos/v2"
)- 设置配置中心
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
- 所有实现
file 配置源示例
以下完整示例详见: config-file-example
- 创建项目
$ mkdir config-file-example- 安装依赖
$ cd config-file-example
$ go mod init config-file-example
$ go get github.com/dobyte/due/v2@v2.4.2- 启动配置
文件位置: config-file-example/etc/etc.toml
[config.file]
# 配置文件或配置目录路径
path = "./config"
# 读写模式。可选:read-only | write-only | read-write,默认为read-only
mode = "read-write"- 编写示例
文件位置: config-file-example/main.go
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)
}- 创建配置目录
$ cd config-file-example
$ mkdir config- 运行示例
$ 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: UTCetcd 配置源示例
以下完整示例详见: config-etcd-example
- 创建项目
$ 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- 启动配置
文件位置: config-etcd-example/etc/etc.toml
[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"- 编写示例
文件位置: config-etcd-example/main.go
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)
}- 运行示例
$ 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: UTCnacos 配置源示例
以下完整示例详见: config-nacos-example
- 创建项目
$ 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- 启动配置
文件位置: config-nacos-example/etc/etc.toml
[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"- 编写示例
文件位置: config-nacos-example/main.go
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)
}- 运行示例
$ 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: UTCconsul 配置源示例
以下完整示例详见: config-consul-example
- 创建项目
$ 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- 启动配置
文件位置: config-consul-example/etc/etc.toml
[config.consul]
# 客户端连接地址
addr = "127.0.0.1:8500"
# 路径。默认为config
path = "config"
# 读写模式。可选:read-only | write-only | read-write,默认为read-only
mode = "read-write"- 编写示例
文件位置: config-consul-example/main.go
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)
}- 运行示例
$ 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