TarsConfigObserver

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2019 License: MIT Imports: 8 Imported by: 0

README

TarsConfigObserver

Make life easier, read tars remote config with spf13/viper

Usage

First, create an observer and add one or more remote config names:

    // Init remote config
    rconf_obs := TarsConfigObserver.NewObserver(60, "")
    _, err := rconf_obs.AddRemoteConfig("config.yaml")
    if err != nil {
        return err
    }

Second, use viper like local config:

    // any file in project
    viper.GetString("foo.bar")

Mutil configs

    rconf_obs := TarsConfigObserver.NewObserver(60, "")
    _, err := rconf_obs.AddRemoteConfig("config.yaml")
    if err != nil {
        return err
    }

    json_viper, err := rconf_obs.AddRemoteConfig("second_config.json")
    if err != nil {
        return err
    }

    // read second configs
    json_viper.GetString("key")

    // or get from GetViper()
    other_viper := rconf_obs.GetViper("second_config.json")
    other_viper.GetString("key")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigObserver

type ConfigObserver struct {
	ReloadInterval int // 检查远程配置的时间间隔,秒
	// contains filtered or unexported fields
}

ConfigObserver Taf远程配置监听服务

func NewObserver

func NewObserver(reloadInterval int, path string) *ConfigObserver

NewObserver 根据服务配置,初始化一个observer (每个App/Server只需要调用1次)

reload_interval - 检查远程配置变更的时间,建议值:60 (60秒)。可通过ReloadInterval动态调整
path - 传空字符串""则默认写入到`conf/`

func (*ConfigObserver) AddRemoteConfig

func (cob *ConfigObserver) AddRemoteConfig(filename string) (vpconf *viper.Viper, err error)

AddRemoteConfig 新增一个要监听的远程配置

func (*ConfigObserver) GetCRC32

func (cob *ConfigObserver) GetCRC32(filename string) uint32

GetCRC32 返回目标文件的CRC32

func (*ConfigObserver) GetViper

func (cob *ConfigObserver) GetViper(filename string) *viper.Viper

GetViper 返回目标文件的viper对象

Jump to

Keyboard shortcuts

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