Documentation
¶
Index ¶
- func MatchPattern(pattern, key string) bool
- type ApolloConfig
- type Client
- func (c *Client) Close() error
- func (c *Client) ConnectHotReload(manager *hotreload.Manager) error
- func (c *Client) GetAllConfigs() (map[string]string, error)
- func (c *Client) GetConfig(key string) (string, error)
- func (c *Client) LoadToTarget(allowedPrefixes []string) error
- func (c *Client) Name() string
- func (c *Client) RegisterChangeHandler(pattern string, handler ConfigChangeHandler) error
- func (c *Client) SetAllowedPrefixes(prefixes []string)
- func (c *Client) WatchChanges() error
- type Config
- type ConfigCenter
- type ConfigChangeHandler
- type ConfigWatcher
- type ConsulConfig
- type NacosConfig
- type Options
- type Type
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MatchPattern ¶
MatchPattern 匹配配置键模式 支持的模式:
- 精确匹配:pattern == key
- 前缀匹配:pattern 以 "." 结尾,匹配所有以 pattern 开头的 key
- 单级通配符:如 "business.api.*" 匹配 "business.api.timeout",但不匹配 "business.api.retry.count"
- 多级通配符:如 "business.**" 匹配所有以 "business." 开头的配置
Types ¶
type ApolloConfig ¶
type ApolloConfig struct {
Enabled bool `yaml:"enabled" env:"APOLLO_ENABLED" default:"false"`
AppID string `yaml:"app_id" env:"APOLLO_APP_ID" required:"true"`
Cluster string `yaml:"cluster" env:"APOLLO_CLUSTER" default:"default"`
Namespaces pkgConfig.StringSlice `yaml:"namespaces" env:"APOLLO_NAMESPACE" default:"application"`
IP string `yaml:"ip" env:"APOLLO_IP" required:"true"`
SecretKey string `yaml:"secret_key" env:"APOLLO_SECRET_KEY"`
SyncInterval string `yaml:"sync_interval" env:"APOLLO_SYNC_INTERVAL" default:"5s"`
LongPollTimeout string `yaml:"long_poll_timeout" env:"APOLLO_LONG_POLL_TIMEOUT" default:"90s"`
NotificationTimeout string `yaml:"notification_timeout" env:"APOLLO_NOTIFICATION_TIMEOUT" default:"1s"`
MaxRetries int `yaml:"max_retries" env:"APOLLO_MAX_RETRIES" default:"3"`
RetryInterval string `yaml:"retry_interval" env:"APOLLO_RETRY_INTERVAL" default:"1s"`
EnableTrace bool `yaml:"enable_trace" env:"APOLLO_ENABLE_TRACE" default:"true"`
}
ApolloConfig Apollo 配置
type Client ¶
type Client struct {
Type Type
// contains filtered or unexported fields
}
Client 统一的配置中心客户端
func (*Client) ConnectHotReload ¶
ConnectHotReload 连接到热加载管理器 当配置中心发生变更时,会自动触发热加载管理器处理
func (*Client) GetAllConfigs ¶
GetAllConfigs 获取所有配置(带 trace)
func (*Client) LoadToTarget ¶
LoadToTarget 从配置中心加载配置并触发热重载 所有配置变更统一通过 watcher.Handle 处理,由 hotreload.Manager 统一管理 allowedPrefixes: 允许的配置前缀列表(如 ["server.features.", "gateway.features."])
func (*Client) RegisterChangeHandler ¶
func (c *Client) RegisterChangeHandler(pattern string, handler ConfigChangeHandler) error
RegisterChangeHandler 注册配置变更处理器
func (*Client) SetAllowedPrefixes ¶
SetAllowedPrefixes 设置允许的配置前缀列表 用于过滤配置变更,只处理匹配前缀的配置
type Config ¶
type Config struct {
Type string `yaml:"type" env:"CONFIG_CENTER_TYPE" default:"none"`
Apollo *ApolloConfig `yaml:"apollo,omitempty"`
Nacos *NacosConfig `yaml:"nacos,omitempty"`
Consul *ConsulConfig `yaml:"consul,omitempty"`
}
Config 配置结构体(用于从配置文件创建) 对应 config_center.yaml 文件结构
type ConfigCenter ¶
type ConfigCenter interface {
// Name 返回配置中心名称(如 "apollo", "nacos", "consul")
Name() string
// GetConfig 获取配置值
GetConfig(key string) (string, error)
// GetAllConfigs 获取所有配置
GetAllConfigs() (map[string]string, error)
// WatchChanges 开始监听配置变更
// callback: 配置变更回调函数,参数为 (key, oldValue, newValue)
WatchChanges(callback func(key, oldValue, newValue string)) error
// Close 关闭配置中心连接
Close() error
}
ConfigCenter 配置中心接口(底层实现) 抽象不同配置中心的通用操作
type ConfigChangeHandler ¶
ConfigChangeHandler 配置变更处理器 key: 配置键 oldValue: 旧值 newValue: 新值 返回错误时,配置变更不会生效,并记录错误日志
type ConfigWatcher ¶
type ConfigWatcher struct {
// contains filtered or unexported fields
}
ConfigWatcher 配置监听器(管理热更新)
func (*ConfigWatcher) Handle ¶
func (w *ConfigWatcher) Handle(key, oldValue, newValue string)
Handle 处理配置变更
func (*ConfigWatcher) Register ¶
func (w *ConfigWatcher) Register(pattern string, handler ConfigChangeHandler)
Register 注册配置变更处理器
type ConsulConfig ¶
type ConsulConfig struct {
Enabled bool `yaml:"enabled" env:"CONSUL_CONFIG_CENTER_ENABLED" default:"false"`
Address string `yaml:"address" env:"CONSUL_CONFIG_CENTER_ADDRESS" default:"localhost:8500"`
Datacenter string `yaml:"datacenter" env:"CONSUL_CONFIG_CENTER_DATACENTER"`
Token string `yaml:"token" env:"CONSUL_CONFIG_CENTER_TOKEN"`
Prefix string `yaml:"prefix" env:"CONSUL_CONFIG_CENTER_PREFIX" default:"config/"`
Scheme string `yaml:"scheme" env:"CONSUL_CONFIG_CENTER_SCHEME" default:"http"`
Timeout string `yaml:"timeout" env:"CONSUL_CONFIG_CENTER_TIMEOUT" default:"10s"`
RetryInterval string `yaml:"retry_interval" env:"CONSUL_CONFIG_CENTER_RETRY_INTERVAL" default:"1s"`
MaxRetries int `yaml:"max_retries" env:"CONSUL_CONFIG_CENTER_MAX_RETRIES" default:"3"`
EnableTrace bool `yaml:"enable_trace" env:"CONSUL_CONFIG_CENTER_ENABLE_TRACE" default:"true"`
}
ConsulConfig Consul 配置
type NacosConfig ¶
type NacosConfig struct {
Enabled bool `yaml:"enabled" env:"NACOS_ENABLED" default:"false"`
ServerAddr string `yaml:"server_addr" env:"NACOS_SERVER_ADDR" required:"true"`
Namespace string `yaml:"namespace" env:"NACOS_NAMESPACE"`
Group string `yaml:"group" env:"NACOS_GROUP" default:"DEFAULT_GROUP"`
DataID pkgConfig.StringSlice `yaml:"data_id" env:"NACOS_DATA_ID" default:"application"`
Username string `yaml:"username" env:"NACOS_USERNAME"`
Password string `yaml:"password" env:"NACOS_PASSWORD"`
Timeout string `yaml:"timeout" env:"NACOS_TIMEOUT" default:"10s"`
RetryInterval string `yaml:"retry_interval" env:"NACOS_RETRY_INTERVAL" default:"1s"`
MaxRetries int `yaml:"max_retries" env:"NACOS_MAX_RETRIES" default:"3"`
EnableTrace bool `yaml:"enable_trace" env:"NACOS_ENABLE_TRACE" default:"true"`
}
NacosConfig Nacos 配置
type Options ¶
type Options struct {
// 通用选项
Type Type
EnableTrace bool
// Apollo 选项
ApolloAppID string
ApolloCluster string
ApolloNamespaces []string
ApolloIP string
ApolloSecretKey string
ApolloSyncInterval time.Duration
ApolloLongPollTimeout time.Duration
ApolloNotificationTimeout time.Duration
ApolloMaxRetries int
ApolloRetryInterval time.Duration
// Nacos 选项
NacosServerAddr string
NacosNamespace string
NacosGroup string
NacosDataIDs []string
NacosUsername string
NacosPassword string
NacosTimeout time.Duration
NacosRetryInterval time.Duration
NacosMaxRetries int
// Consul 选项
ConsulAddress string
ConsulDatacenter string
ConsulToken string
ConsulPrefix string
ConsulScheme string
ConsulTimeout time.Duration
ConsulRetryInterval time.Duration
ConsulMaxRetries int
}
Options 统一的配置中心配置选项