Skip to content

Latest commit

 

History

History
153 lines (115 loc) · 3.52 KB

README_CN.md

File metadata and controls

153 lines (115 loc) · 3.52 KB

agollo 是携程 apollo 配置中心的 golang 客户端 🚀 CircleCI

Go Report Card codebeat badge Coverage Status golang GoDoc GitHub release

功能

  • 多 namespace 支持
  • 容错,本地缓存
  • 实时更新通知
  • 支持Umarshal

依赖

go 1.9 或更新

安装

    go get -u github.com/philchia/agollo

使用

1, 启动

使用 app.properties 配置文件启动

    agollo.Start()

使用自定义配置启动

    agollo.StartWithConfFile(name)

使用自定义结构启动

    agollo.StartWithConf(yourConf)

2, 热更新

监听配置更新(这种方式即将被弃用)

    events := agollo.WatchUpdate()
    changeEvent := <-event
    bytes, _ := json.Marshal(changeEvent)
    fmt.Println("event:", string(bytes))

或者,使用OnConfigChange接口以回调方式监听配置更新

	agollo.OnConfigChange(func(e *agollo.ChangeEvent) {
        bytes, _ := json.Marshal(e)
        fmt.Println("event:", string(bytes))
	})

3, 多种方式获取配置

获取properties配置

    // default namespace: application
    agollo.GetStringValue(Key, defaultValue)

    // user specify namespace
    agollo.GetStringValueWithNameSapce(namespace, key, defaultValue)

获取文件内容

    agollo.GetNameSpaceContent(namespace, defaultValue)

获取配置中所有的键

    agollo.GetAllKeys(namespace)

用Unmarshal获取配置

假设配置中心是这样配置的:

那么,我们的元配置(app.properties)应该这样写:

{
    "appId": "001",
    "cluster": "default",
    "namespaceNames": ["application","dnspod1","dnspod2.yaml","db"],
    "ip": "localhost:8080"
}

然后像这样定义一个struct去获取所有的配置:

package main

import (
	"fmt"
	"log"

	"github.com/philchia/agollo"
)

type config struct {
    // dns 配置
    DNS1 struct {
        ID     string `mapstructure:"id"`
        Token  string `mapstructure:"token"`
        Domain string `mapstructure:"domain"`
    } `mapstructure:"dnspod1"`
    DNS2 struct {
        ID     int    `mapstructure:"id"`
        Token  string `mapstructure:"token"`
        Domain string `mapstructure:"domain"`
    } `mapstructure:"dnspod2.yaml"`

    // DB
    DB struct {
        DSN     string `mapstructure:"dsn"`
        MaxConn string `mapstructure:"max_conn"`
    } `mapstructure:"db"`
}

func main(){
    agollo.Start()

    // 第一次读取
    var c config
    agollo.Unmarshal(&c)
    fmt.Printf("%v", c)

    // 热更新
	agollo.OnConfigChange(func(e *agollo.ChangeEvent) {
		var c config
		agollo.Unmarshal(&c)
		fmt.Println(c)
	})
}