Documentation
¶
Index ¶
- Variables
- func LoadConfig[T any](params LoaderParams, localLoad LocalLoaderFunc, remoteLoad RemoteLoaderFunc, ...) (T, error)
- func LoadStoreConfig[T any](ctx context.Context, store Store, params StoreParams, ...) (T, error)
- type BootstrapConfig
- type Codec
- type Encryptor
- type EventType
- type Key
- type LoaderParams
- type LocalLoaderFunc
- type LoggerConfig
- type Meta
- type Option
- type Options
- type PayloadDecodeFunc
- type Raw
- type RemoteLoaderFunc
- type Store
- type StoreParams
- type TelemetryConfig
- type WatchEvent
- type Watcher
Constants ¶
This section is empty.
Variables ¶
var ( // ErrLocalLoaderIsNil 表示本地加载函数为空。 ErrLocalLoaderIsNil = errors.New("config local loader is nil") // ErrRemoteLoaderIsNil 表示远程加载函数为空。 ErrRemoteLoaderIsNil = errors.New("config remote loader is nil") // ErrPayloadDecoderIsNil 表示配置内容解码函数为空。 ErrPayloadDecoderIsNil = errors.New("config payload decoder is nil") // ErrStoreIsNil 表示存储实现为空。 ErrStoreIsNil = errors.New("config store is nil") // ErrWatcherIsNil 表示监听实现为空。 ErrWatcherIsNil = errors.New("config watcher is nil") // ErrCodecIsNil 表示编解码实现为空。 ErrCodecIsNil = errors.New("config codec is nil") // ErrEncryptorIsNil 表示加解密实现为空。 ErrEncryptorIsNil = errors.New("config encryptor is nil") // ErrInvalidKey 表示配置键不合法。 ErrInvalidKey = errors.New("invalid config key") // ErrInvalidRaw 表示配置内容不合法。 ErrInvalidRaw = errors.New("invalid config raw") // ErrResourceNotFound 表示配置资源不存在。 ErrResourceNotFound = errors.New("config resource not found") // ErrVersionConflict 表示写入版本冲突。 ErrVersionConflict = errors.New("config version conflict") // ErrUnsupportedLoadMode 表示配置加载模式不支持。 ErrUnsupportedLoadMode = errors.New("unsupported config load mode") )
Functions ¶
func LoadConfig ¶ added in v1.3.0
func LoadConfig[T any](params LoaderParams, localLoad LocalLoaderFunc, remoteLoad RemoteLoaderFunc, payloadDecode PayloadDecodeFunc) (T, error)
LoadConfig 按 local / remote 规则加载并解析后端配置。
func LoadStoreConfig ¶ added in v1.3.0
func LoadStoreConfig[T any](ctx context.Context, store Store, params StoreParams, payloadDecode PayloadDecodeFunc) (T, error)
LoadStoreConfig 从 Store 读取当前配置并解析为目标类型 T。 加密处理规则: - 当 Raw.Encrypted=false 时,直接 JSON 解析 Content - 当 Raw.Encrypted=true 时,必须先通过 payloadDecode 解密整份 Content,再解析为目标结构 - 不支持字段级加密,一份配置要加密就整份加密
Types ¶
type BootstrapConfig ¶ added in v1.3.3
type BootstrapConfig interface {
GetAppId() string
GetAppSecret() string
GetAppName() string
GetAppVersion() string
GetServiceEndpoint() string
GetServiceAuthToken() string
GetServiceNamespace() string
GetServiceInstanceId() string
GetSystemName() string
GetSystemType() uint32
GetSystemVersion() string
GetGatewayEndpoint() string
GetGatewayAuthToken() string
GetServerPort() uint
GetManagementPort() uint
LoggerConfig
TelemetryConfig
}
type Codec ¶
type Codec interface {
// Marshal 把结构化对象编码为字节。
Marshal(v any) ([]byte, error)
// Unmarshal 把字节解码到目标对象。
Unmarshal(data []byte, dst any) error
}
Codec 定义配置内容序列化与反序列化能力。
type Encryptor ¶
type Encryptor interface {
// Encrypt 使用给定密钥加密数据。
Encrypt(data []byte, key []byte) ([]byte, error)
// Decrypt 使用给定密钥解密数据。
Decrypt(data []byte, key []byte) ([]byte, error)
}
Encryptor 定义配置内容加解密能力。
type Key ¶
type Key struct {
// TenantId 表示租户标识,用于多租户隔离。
TenantId string `json:"tenant_id"`
// AppId 表示应用标识。
AppId string `json:"app_id"`
// Env 表示环境,如 dev/staging/prod。
Env string `json:"env"`
// Group 表示配置分组。
Group string `json:"group"`
// Name 表示具体配置名。
Name string `json:"name"`
}
Key 描述一条配置在存储中的业务主键。
type LoaderParams ¶ added in v1.3.0
type LoaderParams struct {
// Mode 指定加载模式:local / remote。
Mode string
// AppId 远程模式下用于定位配置所属应用。
AppId string
// AppSecret 远程模式下用于解密整份配置内容(如果是密文)。
AppSecret []byte
// Group 配置分组(如 database)。
Group string
// Key 配置名(如 consul / etcd / k8s)。
Key string
// FileName 本地模式下使用的文件名(如 consul.json)。
FileName string
}
LoaderParams 描述加载后端配置对象所需参数。 该结构把 local / remote 两条加载链路的输入参数统一起来。
type LocalLoaderFunc ¶ added in v1.3.0
LocalLoaderFunc 定义本地配置加载函数签名。 典型实现是按 fileName 读取配置文件并反序列化到 target。
type LoggerConfig ¶ added in v1.3.3
type Meta ¶
type Meta struct {
// CurrentVersion 表示当前生效版本。
CurrentVersion string `json:"current_version"`
// LatestVersion 表示最新发布版本。
LatestVersion string `json:"latest_version"`
}
Meta 表示一组配置的版本游标信息。
type Options ¶
type Options struct {
// Namespace 用于区分配置键空间。
Namespace string
// Timeout 用于单次请求超时控制。
Timeout time.Duration
// Retry 用于失败重试次数控制。
Retry uint32
// WatchBuffer 用于监听事件通道缓冲区大小。
WatchBuffer int
// Codec 为可选编解码器。
Codec Codec
// Encryptor 为可选加解密器。
Encryptor Encryptor
}
Options 定义 config 组件的通用运行参数。
type PayloadDecodeFunc ¶ added in v1.3.0
PayloadDecodeFunc 定义配置内容解码函数签名。 用于处理整份配置为密文的场景:先解密完整内容,再反序列化到 target。
type Raw ¶ added in v1.3.3
type Raw struct {
// Meta 记录扩展元信息。
Meta map[string]string `json:"meta"`
// Version 表示配置版本号。
Version string `json:"version"`
// Content 是当前整份配置的原始内容。
Content []byte `json:"content"`
// Encrypted 标识当前整份 Content 是否已加密。
// 当值为 true 时,读取方必须先解密整份内容,再做反序列化。
Encrypted bool `json:"encrypted"`
// CreatedAt 表示创建时间。
CreatedAt time.Time `json:"created_at"`
// UpdatedAt 表示最近更新时间。
UpdatedAt time.Time `json:"updated_at"`
// UpdatedBy 表示最近更新人。
UpdatedBy string `json:"updated_by"`
}
Raw 表示一条可发布、可读取的配置内容。
type RemoteLoaderFunc ¶ added in v1.3.0
RemoteLoaderFunc 定义远程配置读取函数签名。 典型实现是从配置中心按 appId + group + key 获取配置文本。
type Store ¶
type Store interface {
// Get 按配置键读取当前生效配置。
// 读取到的 Raw.Encrypted 标识整份配置是否为密文。
// 当 Encrypted=true 时,调用方需要先解密整份 Content,再解析目标结构。
Get(ctx context.Context, key Key) (*Raw, error)
// Put 写入当前生效配置。
// Raw.Encrypted 标识整份配置是否为密文。
// 一份配置要加密就整份加密,不做字段级加密。
Put(ctx context.Context, key Key, raw *Raw) error
// Delete 删除当前配置。
Delete(ctx context.Context, key Key) error
// PutVersion 写入版本快照并返回版本号。
// 若 Raw.Version 为空,实现层可自动生成版本号。
PutVersion(ctx context.Context, key Key, raw *Raw) (string, error)
// GetVersion 读取指定版本快照。
GetVersion(ctx context.Context, key Key, version string) (*Raw, error)
// ListVersions 列出版本号列表。
// limit 为 0 时返回所有版本,否则返回最新的 limit 个版本。
ListVersions(ctx context.Context, key Key, limit int) ([]string, error)
// GetMeta 读取配置元信息。
GetMeta(ctx context.Context, key Key) (*Meta, error)
// PutMeta 写入配置元信息。
PutMeta(ctx context.Context, key Key, meta *Meta) error
}
Store 定义统一配置存储抽象,供 consul/k8s 实现对齐。
type StoreParams ¶ added in v1.3.0
type StoreParams struct {
// Key 是优先使用的业务键。
Key Key
// AppId 用于回填 Key.AppId。
AppId string
// Env 用于回填 Key.Env。
Env string
// Group 用于回填 Key.Group。
Group string
// Name 用于回填 Key.Name。
Name string
// AppSecret 当读取到密文配置项时用于解码。
AppSecret []byte
}
StoreParams 描述从 Store 读取一条配置并解析所需参数。 当 Key 某些字段为空时,会用 AppId / Env / Group / Name 回填。
type TelemetryConfig ¶ added in v1.3.3
type WatchEvent ¶
type WatchEvent struct {
// Type 表示事件类型。
Type EventType `json:"type"`
// Key 表示事件对应的配置键。
Key Key `json:"key"`
// Raw 表示事件携带的配置内容。
Raw *Raw `json:"raw"`
}
WatchEvent 表示 watch 通知事件。