agollo

package module
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

README

Agollo - Go Client for Apollo

golang Build Status Go Report Card codebeat badge Coverage Status License GoDoc GitHub release 996.icu

方便Golang接入配置中心框架 Apollo 所开发的Golang版本客户端。

其他语言 : 可使用 agollo-agent 做本地agent接入。

Installation

如果还没有安装Go开发环境,请参考以下文档Getting Started ,安装完成后,请执行以下命令:

gopm get github.com/coocood/freecache -v -g

或者

go get -u github.com/coocood/freecache

或者

chmod u+x ./depend.sh
./depend.sh

请注意: 最好使用Go 1.8进行开发

Features

  • 实时同步配置
  • 灰度配置
  • 客户端容灾
  • 配置文件容灾 (v1.6.0+)
  • 自定义日志组件 (因开源协议原因,从v1.8.0不再存在默认日志组件。如还需使用seelog,请参考:如何使用seelog

Usage

  • 异步启动agollo

场景:启动程序不依赖加载Apollo的配置。

func main() {
	 go agollo.Start()
}
  • 同步启动agollo(v1.2.0+)

场景:启动程序依赖加载Apollo的配置。例:初始化程序基础配置。

func main() {
	 agollo.Start()
}
  • 启动agollo - 自定义logger控件(感谢 @Adol1111 提供)
func main() {
	 agollo.StartWithLogger(loggerInterface)
}
  • 启动agollo - 自定义cache控件 (v1.7.0+)
func main() {
	 agollo.StartWithCache(cacheInterface)
}
  • 启动agollo - 自定义各种控件 (v1.8.0+)
func main() {
    agollo.SetLogger(loggerInterface)
	agollo.SetCache(cacheInterface)
	agollo.Start()
}
  • 监听变更事件(阻塞)
func main() {
	event := agollo.ListenChangeEvent()
	changeEvent := <-event
	bytes, _ := json.Marshal(changeEvent)
	fmt.Println("event:", string(bytes))
}
  • 获取Apollo的配置

    • String
    agollo.GetStringValue(Key,DefaultValue)
    
    • Int
    agollo.GetIntValue(Key,DefaultValue)
    
    • Float
    agollo.GetFloatValue(Key,DefaultValue)
    
    • Bool
    agollo.GetBoolValue(Key,DefaultValue)
    

 后续可支持更多类型

 欢迎查阅 Wiki 或者 godoc 获取更多有用的信息

 如果你觉得该工具还不错或者有问题,一定要让我知道,可以发邮件或者留言

User

Contribution

License

The project is licensed under the Apache 2 license.

Reference

Apollo : https://github.com/ctripcorp/apollo

Documentation

Index

Constants

View Source
const FILE = "apolloConfig.json"

Variables

This section is empty.

Functions

func GetBoolValue

func GetBoolValue(key string, defaultValue bool) bool

func GetFloatValue

func GetFloatValue(key string, defaultValue float64) float64

func GetIntValue

func GetIntValue(key string, defaultValue int) int

func GetStringValue

func GetStringValue(key string, defaultValue string) string

func InitCustomConfig

func InitCustomConfig(loadAppConfig func() (*AppConfig, error))

init config by custom

func ListenChangeEvent

func ListenChangeEvent() <-chan *ChangeEvent

list config change event

func SetCache

func SetCache(cacheInterface CacheInterface)

func SetLogger

func SetLogger(loggerInterface LoggerInterface)

func Start

func Start() error

start apollo

func StartRefreshConfig

func StartRefreshConfig(component AbsComponent)

func StartWithCache

func StartWithCache(cacheInterface CacheInterface) error

func StartWithLogger

func StartWithLogger(loggerInterface LoggerInterface) error

Types

type AbsComponent

type AbsComponent interface {
	Start()
}

type ApolloConfig

type ApolloConfig struct {
	ApolloConnConfig
	Configurations map[string]string `json:"configurations"`
}

type ApolloConnConfig

type ApolloConnConfig struct {
	AppId         string `json:"appId"`
	Cluster       string `json:"cluster"`
	NamespaceName string `json:"namespaceName"`
	ReleaseKey    string `json:"releaseKey"`
	sync.RWMutex
}

func GetCurrentApolloConfig

func GetCurrentApolloConfig() *ApolloConnConfig

type AppConfig

type AppConfig struct {
	AppId            string `json:"appId"`
	Cluster          string `json:"cluster"`
	NamespaceName    string `json:"namespaceName"`
	Ip               string `json:"ip"`
	NextTryConnTime  int64  `json:"-"`
	BackupConfigPath string `json:"backupConfigPath"`
}

func GetAppConfig

func GetAppConfig(newAppConfig *AppConfig) *AppConfig

type CacheInterface

type CacheInterface interface {
	Set(key, value []byte, expireSeconds int) (err error)

	EntryCount() (entryCount int64)

	Get(key []byte) (value []byte, err error)

	Del(key []byte) (affected bool)

	NewIterator() *freecache.Iterator

	TTL(key []byte) (timeLeft uint32, err error)

	Clear()
}

func GetApolloConfigCache

func GetApolloConfigCache() CacheInterface

type CallBack

type CallBack struct {
	SuccessCallBack   func([]byte) (interface{}, error)
	NotModifyCallBack func() error
}

type ChangeEvent

type ChangeEvent struct {
	Namespace string
	Changes   map[string]*ConfigChange
}

config change event

type ConfigChange

type ConfigChange struct {
	OldValue   string
	NewValue   string
	ChangeType ConfigChangeType
}

type ConfigChangeType

type ConfigChangeType int

config change type

const (
	ADDED ConfigChangeType = iota
	MODIFIED
	DELETED
)

type ConnectConfig

type ConnectConfig struct {
	//设置到http.client中timeout字段
	Timeout time.Duration
	//连接接口的uri
	Uri string
}

type DefaultLogger

type DefaultLogger struct {
}

func (*DefaultLogger) Debug

func (this *DefaultLogger) Debug(v ...interface{})

func (*DefaultLogger) Debugf

func (this *DefaultLogger) Debugf(format string, params ...interface{})

func (*DefaultLogger) Error

func (this *DefaultLogger) Error(v ...interface{}) error

func (*DefaultLogger) Errorf

func (this *DefaultLogger) Errorf(format string, params ...interface{}) error

func (*DefaultLogger) Info

func (this *DefaultLogger) Info(v ...interface{})

func (*DefaultLogger) Infof

func (this *DefaultLogger) Infof(format string, params ...interface{})

func (*DefaultLogger) Warn

func (this *DefaultLogger) Warn(v ...interface{}) error

func (*DefaultLogger) Warnf

func (this *DefaultLogger) Warnf(format string, params ...interface{}) error

type LoggerInterface

type LoggerInterface interface {
	Debugf(format string, params ...interface{})

	Infof(format string, params ...interface{})

	Warnf(format string, params ...interface{}) error

	Errorf(format string, params ...interface{}) error

	Debug(v ...interface{})

	Info(v ...interface{})

	Warn(v ...interface{}) error

	Error(v ...interface{}) error
}

type NotifyConfigComponent

type NotifyConfigComponent struct {
}

func (*NotifyConfigComponent) Start

func (this *NotifyConfigComponent) Start()

Jump to

Keyboard shortcuts

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