configcenter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 29 Imported by: 1

README

framework-configcenter

Go framework package for configcenter.

Installation

go get github.com/go-anyway/framework-configcenter@v1.0.0

Usage

See documentation for usage examples.

License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchPattern

func MatchPattern(pattern, key string) bool

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 NewClient

func NewClient(opts *Options) (*Client, error)

NewClient 根据配置创建统一的配置中心客户端

func NewFromConfig

func NewFromConfig(cfg *Config) (*Client, error)

NewFromConfig 从配置创建客户端

func (*Client) Close

func (c *Client) Close() error

Close 关闭客户端

func (*Client) ConnectHotReload

func (c *Client) ConnectHotReload(manager *hotreload.Manager) error

ConnectHotReload 连接到热加载管理器 当配置中心发生变更时,会自动触发热加载管理器处理

func (*Client) GetAllConfigs

func (c *Client) GetAllConfigs() (map[string]string, error)

GetAllConfigs 获取所有配置(带 trace)

func (*Client) GetConfig

func (c *Client) GetConfig(key string) (string, error)

GetConfig 获取配置值(带 trace)

func (*Client) LoadToTarget

func (c *Client) LoadToTarget(allowedPrefixes []string) error

LoadToTarget 从配置中心加载配置并触发热重载 所有配置变更统一通过 watcher.Handle 处理,由 hotreload.Manager 统一管理 allowedPrefixes: 允许的配置前缀列表(如 ["server.features.", "gateway.features."])

func (*Client) Name

func (c *Client) Name() string

Name 返回配置中心名称

func (*Client) RegisterChangeHandler

func (c *Client) RegisterChangeHandler(pattern string, handler ConfigChangeHandler) error

RegisterChangeHandler 注册配置变更处理器

func (*Client) SetAllowedPrefixes

func (c *Client) SetAllowedPrefixes(prefixes []string)

SetAllowedPrefixes 设置允许的配置前缀列表 用于过滤配置变更,只处理匹配前缀的配置

func (*Client) WatchChanges

func (c *Client) WatchChanges() error

WatchChanges 开始监听配置变更

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 文件结构

func LoadFromFile

func LoadFromFile(configDir string) (*Config, error)

LoadFromFile 从文件加载配置中心配置

func (*Config) ToOptions

func (c *Config) ToOptions() (*Options, error)

ToOptions 将 Config 转换为 Options

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证配置

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

type ConfigChangeHandler func(key, oldValue, newValue string) error

ConfigChangeHandler 配置变更处理器 key: 配置键 oldValue: 旧值 newValue: 新值 返回错误时,配置变更不会生效,并记录错误日志

type ConfigWatcher

type ConfigWatcher struct {
	// contains filtered or unexported fields
}

ConfigWatcher 配置监听器(管理热更新)

func NewConfigWatcher

func NewConfigWatcher() *ConfigWatcher

NewConfigWatcher 创建新的配置监听器

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 统一的配置中心配置选项

func (*Options) Validate

func (o *Options) Validate() error

Validate 验证配置选项

type Type

type Type string

Type 配置中心类型

const (
	// TypeNone 不使用配置中心
	TypeNone Type = "none"
	// TypeApollo Apollo 配置中心
	TypeApollo Type = "apollo"
	// TypeNacos Nacos 配置中心
	TypeNacos Type = "nacos"
	// TypeConsul Consul 配置中心
	TypeConsul Type = "consul"
)

func DetectType

func DetectType(configDir string) Type

DetectType 检测配置中心类型 优先级:环境变量 > 配置文件

Jump to

Keyboard shortcuts

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