hconfig

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

hconfig 插拔式配置读取工具可动态加载

  • 支持 etcd
  • 支持 kubernetes
  • 支持 apollo

hconfig 配置不同的源

//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("/home/app/conf/kube_config/local_kube.yaml"))
     
c, err := kubernetes.NewKubernetesConfig(cli, 
	kubernetes.WithNamespace("im"),
	kubernetes.WithPaths("im-test-conf", "im-test-conf2"))
	
//apollo
c, err := apollo.NewApolloConfig(
    apollo.WithAppid("test"),
    apollo.WithNamespace("test.yaml"),
    apollo.WithAddr("http://127.0.0.1:32001"),
    apollo.WithCluster("dev"),
    )

hconfig 使用

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())
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HConfig

type HConfig interface {
	Load() error
	Get(key string) (HVal, error)
	Watch(event WatchEvent) error
	Close() error
}

func NewHConfig

func NewHConfig(opts ...Option) (HConfig, error)

type HVal

type HVal []byte

func (HVal) Bytes

func (h HVal) Bytes() []byte

func (HVal) FormatJson

func (h HVal) FormatJson(v interface{}) error

func (HVal) FormatYaml

func (h HVal) FormatYaml(v interface{}) error

func (HVal) String

func (h HVal) String() string

type Option

type Option func(opt *options)

func UseLocal

func UseLocal() Option

func WithDataSource

func WithDataSource(d hconf.DataSource) Option

type WatchEvent

type WatchEvent func(path string, v HVal)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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