golang微服务框架go-micro使用 (六) Config And Errors

本文详细介绍了Go微服务框架go-micro中Config的特性,如动态加载、资源可插拔、配置合并及监控变化等。同时,讨论了Error的使用,包括错误类型及其在服务中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Config

特征
  • 1 动态加载:根据需要动态加载多个资源文件。 go config 在后台管理并监控配置文件,并自动更新到内存中
  • 2 资源可插拔: 从任意数量的源中进行选择以加载和合并配置。后台资源源被抽象为内部使用的标准格式,并通过编码器进行解码。源可以是环境变量,标志,文件,etcd,k8s configmap等
  • 3 可合并的配置:如果指定了多种配置,它们会合并到一个视图中。
  • 4 监控变化:可以选择是否监控配置的指定值,热重启
  • 5 安全恢复: 万一配置加载错误或者由于未知原因而清除,可以指定回退值进行回退
Sources

Source 是资源加载来源,支持以下形式:

  • cli 命令行
  • consul
  • env
  • etcd
  • file
  • flag
  • memory
    也有一些社区支持的插件:
  • configmap - nread from k8s configmap
  • grpc - read from grpc server
  • runtimevar - read from Go Cloud Development Kit runtime variable
  • url - read from URL
  • vault - read from Vault server
ChangeSet

Source返回ChangeSet,是多个后端的单例内部抽象

// ChangeSet represents a set of changes from a source
type ChangeSet struct {
   
	// Raw encoded config data
	Data      []byte
	// MD5 checksum of the data
	Checksum  string
	// Encoding format e.g json, yaml, toml, xml
	Format    string
	// Source of the config e.g file, consul, etcd
	Source    string
	// Time of loading or update
	Timestamp time.Time
}
Encoder

Encoder 处理配置文件的编码与解码,支持:

  • json
  • yaml
  • toml
  • xml
  • xml
  • hcl
Config
// Config is an interface abstraction for dynamic configuration
type Config interface {
   
        // provide the reader.Values interface
        reader.Values
	// Stop the config loader/watcher
	Close() error
	// Load config sources
	Load(source ...source.Source) error
	// Force a source changeset sync
	Sync() error
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值