hconfig 一个可插拔的 Golang 配置管理工具 支持(etcd/k8s(kubernetes)/apollo)

201 阅读3分钟

hconfig 一个可插拔的 Golang 配置管理工具

支持(etcd/k8s(kubernetes)/apollo)

什么是可插拔式程序
  • 一个统计的可插拔内核
  • 各个组件相互独立

可插拔式程序

  1. 在设计一个可插拔式程序时我们应该想到的是怎么把我们的需求给实现了,然后我们再搞一波抽象(统计的可插拔内核
    不同的模块只要实现了这内核中的方法,我们的主程序就能去调用模块中对应的方法
  2. 假设我们打算修一个房子睡觉,我们只需要把大体的框架修好(这个就是一个统计的可插拔内核),然后我们在里面规划 不同的模块比如次卧,主卧,客卧,(各个组件相互独立),对于我们需求来说都是用来睡觉的只是实现的方式不同而已,而且相互独立

hconfig 详解

hconfig 在设计上主要有2个功能

  • 读取配置信息
  • 监听配置信息的变化
conf, err := NewHConfig(
   WithDataSource(c),//c 不同的源
)

// 加载配置
conf.Load() 

//读取配置
val, err := conf.Get("test.yaml")
t.Logf("val %+v\n", val.String())

//监听配置变化
conf.Watch(func(path string, v HVal) {
   t.Logf("path %s val %+v\n", path, v.String())
})

在了解到什么是可插拔程序时,我们再来看一看hconfig的结构

  • 一个统计的可插拔内核
type DataSource interface {
   Load() ([]*Data, error)
   Watch() (DataWatcher, error)
}

type DataWatcher interface {
   Change() ([]*Data, error)
   Close() error
}
  • 各个组件相互独立
  • etcd
cli, err := clientv3.New(clientv3.Config{
    Endpoints: []string{"127.0.0.1:2379"},})

c, err := etcd.NewEtcdConfig(cli,
etcd.WithRoot("/hconf"),
etcd.WithPaths("app", "mysql"))
  • kubernetes
cli, err := kubernetes.NewK8sClientset(
     kubernetes.KubeConfigPath("./kube.yaml"))
     
c, err := kubernetes.NewKubernetesConfig(cli, 
   kubernetes.WithNamespace("test"),
   kubernetes.WithPaths("conf", "conf2"))
  • apollo
c, err := apollo.NewApolloConfig(
    apollo.WithAppid("test"),
    apollo.WithNamespace("test.yaml"),
    apollo.WithAddr("http://127.0.0.1:32001"),
    apollo.WithCluster("dev"),
    )

讲解基于etcd的

etcd(读作 et-see-dee)是一种开源的分布式统一键值存储,用于分布式系统或计算机集群的共享配置、服务发现和的调度协调。

  1. 假定我们如下配置文件 conf.yaml conf1.yaml conf2.yaml 但是在一个etcd集群中可能不止我们使用这个集群在很大程度上可能会出现相同的名字 所以我们需要定义个根路径(这里后面监听变化也能用到),来区分不同的服务,根路径我们配置为 "/hconf" 所以我们的配置文件路径变为
    • /hconf/conf.yaml
    • /hconf/conf1.yaml
    • /hconf/conf2.yaml
  2. 在etcd中我们可以监听一个目录下的所有子目录的变化需要配置 clientv3.WithPrefix(),刚刚我们定义的那个根路径就派上了用途 我们只需要监听/hconf这个目录就能知道 conf.yaml conf1.yaml conf2.yaml 的文件变化

在etcd中实现 hconfig 的统一内核

Load() ([]*Data, error)

这个接口定义的是把所有需要配置文件读取到并返回 我们可以通过etcd get 方法来读取到 /hconf/conf.yaml, /hconf/conf1.yaml , /hconf/conf2.yaml 返回

func (c *etcdConfig) Load() ([]*hconf.Data, error) {
   data := make([]*hconf.Data, 0)
   for _, v := range c.options.paths {
      loadData, err := c.loadPath(v)
      if err != nil {
         return nil, err
      }
      data = append(data, loadData)
   }
   return data, nil
}

func (c *etcdConfig) loadPath(path string) (*hconf.Data, error) {
   rsp, err := c.client.Get(c.options.ctx, fmt.Sprintf("%s/%s", c.options.root, path))
   if err != nil {
      return nil, err
   }
   data := new(hconf.Data)
   for _, item := range rsp.Kvs {
      k := string(item.Key)
      k = strings.ReplaceAll(strings.ReplaceAll(k, c.options.root, ""), "/", "")
      if k == path {
         data.Key = k
         data.Val = item.Value
         break
      }
   }
   return data, nil
}
Watch() (DataWatcher, error)
func (c *etcdConfig) Watch() (hconf.DataWatcher, error) {
   return newWatcher(c), nil
}
  • DataWatcher.Change() ([]*Data, error) //监听配置文件的变化 在etcd中我们可以通过监听我们的根目录来实现监听在这个根目录下所有文件的变化,所有我们可以通过 etcd Watch 我们定义的根节点 并配置clientv3.WithPrefix()来实现该功能
func newWatcher(s *etcdConfig) *watcher {
   w := &watcher{
      etcdConfig: s,
      ch:         nil,
      closeChan:  make(chan struct{}),
   }
   w.ch = s.client.Watch(s.options.ctx, s.options.root, clientv3.WithPrefix())
   return w
}

func (w *watcher) Change() ([]*hconf.Data, error) {
   select {
   case <-w.closeChan:
      return nil, nil
   case kv, ok := <-w.ch:
      if !ok {
         return nil, nil
      }
      var data []*mvccpb.KeyValue
      for _, v := range kv.Events {
         if v.Type == mvccpb.DELETE {
            continue
         }
         data = append(data, v.Kv)
      }
      return w.etcdConfig.kvsToData(data)
   }
}
  • DataWatcher.Close() error // 取消监听配置文件
func (w *watcher) Close() error {
   close(w.closeChan)
   return nil
}

hconfig 完整代码地址