consulstructure

Decode Consul data into Go (Golang) structures and watch for updates

  • 所有者: mitchellh/consulstructure
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

consulstructure

consulstructure is a Go library for decoding Consul
data into a Go structure and keeping it in sync with Consul.

The primary use case for this library is to be able to build native
Consul-based configuration into your Go applications without needing
glue such as consul-template.

Installation

Standard go get:

$ go get github.com/mitchellh/consulstructure

Features

Below is a high-level feature list:

  • Watch a key prefix in Consul KV to populate a Go structure.

  • Notification on a channel when configuration is updated.

  • Configuration structures support all Go primitive types, maps, and structs.
    Slices and arrays aren't supported since they don't mean anything in
    the data model of Consul KV.

  • Nested and embedded structs in configuration structures work.

  • Set quiescence periods to avoid a stampede of configuration updates
    when many keys are updated in a short period of time.

  • Supports all connection features of Consul: multi-datacenter, encryption,
    and ACLs.

Usage & Example

For docs see the Godoc.

An example is shown below:

import (
    "fmt"

    "github.com/mitchellh/consulstructure"
)

// Create a configuration struct that'll be filled by Consul.
type Config struct {
    Addr     string
    DataPath string `consul:"data_path"`
}

// Create our decoder
updateCh := make(chan interface{})
errCh := make(chan error)
decoder := &consulstructure.Decoder{
    Target:   &Config{},
    Prefix:   "services/myservice",
    UpdateCh: updateCh,
    ErrCh:    errCh,
}

// Run the decoder and wait for changes
go decoder.Run()
for {
    select {
    case v := <-updateCh:
        fmt.Printf("Updated config: %#v\n", v.(*Config))
    case err := <-errCh:
        fmt.Printf("Error: %s\n", err)
    }
}

But Why Not a File?

A file is the most portable and technology agnostic way to get configuration
into an application. I'm not advocating this instead of using files for the
general case.

For organizations that have chosen Consul as their technology for configuration,
services, etc. being able to build services that can be started without
any further configuration and immediately start running is very attractive.
You no longer have a configuration step where you have to setup services
and file templates and so on with tools like
consul-template.

You just install the Go binary, start it, and it is going. To update it,
you just update the settings in Consul, and the application automatically
updates. No more SIGHUP necessary, no more manual restarts.

主要指标

概览
名称与所有者mitchellh/consulstructure
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2016-03-24 21:04:59
推送于2019-03-29 23:18:41
最后一次提交2019-03-29 16:18:41
发布数0
用户参与
星数172
关注者数7
派生数13
提交数5
已启用问题?
问题数1
打开的问题数0
拉请求数1
打开的拉请求数0
关闭的拉请求数0
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?