conf

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterFactory

func RegisterFactory(scheme string, fn FactoryFunc)

RegisterFactory 注册一个 scheme 对应的 ConfigCenter 工厂。 在各 infra 子包的 init() 中调用,重复注册同一 scheme 会 panic。

Types

type ConfigCenter

type ConfigCenter interface {
	// Get 获取配置内容
	Get(ctx context.Context, key string) (string, error)
	// Watch 监听 key 变更,变更时以新值回调 onChange。
	// ctx 取消或调用返回的 cancel 均可停止监听。
	Watch(ctx context.Context, key string, onChange func(key, value string)) (context.CancelFunc, error)
}

ConfigCenter 统一配置中心接口,nacos/etcd/polaris 均实现此接口。 key 的含义由实现决定:nacos=dataID,etcd=完整路径,polaris=文件名。

type FactoryFunc

type FactoryFunc func(u *url.URL) (ConfigCenter, error)

FactoryFunc 从解析好的 URL 构造一个 ConfigCenter。 scheme 由各 infra 包在 init() 中注册。

type Loader

type Loader interface {
	Unmarshal(dst any) error
	Watch(ctx context.Context, fn func())
}

Loader 统一配置加载接口。 Unmarshal 将当前配置反序列化到 dst(热加载后再次调用可拿到新值)。 Watch 在每次配置变更时异步调用 fn,ctx 取消后停止监听。

func New

func New(rawURL string) (Loader, error)

New 根据 rawURL 构造 Loader。

  • 无 scheme 或 scheme == "file":读取本地文件,支持 fsnotify 热加载。
  • 其他 scheme(etcd / nacos / consul / polaris …): 需提前 import 对应 infra 子包(触发 init 注册工厂), URL 格式由各工厂决定,key 取自 URL Path(去掉前导 /)。

远程示例:

conf.New("etcd://127.0.0.1:2379/myapp/config.yaml")
conf.New("nacos://127.0.0.1:8848/myapp.yaml?namespace=dev&group=DEFAULT_GROUP")
conf.New("consul://127.0.0.1:8500/myapp/config.yaml")

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL