agollo

package module
v4.1.7 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: MIT Imports: 21 Imported by: 0

README

agollo is a golang client for apollo 🚀

Go Report Card codebeat badge Coverage Status golang GoDoc GitHub release

v1:goproxy.cn v3:goproxy.cn v4:goproxy.cn

Feature

  • Multiple namespace support
  • Fail tolerant
  • Zero dependency
  • Realtime change notification
  • API to get contents of namespace

Required

go 1.11 or later

Installation

go get -u github.com/FarmerChillax/agollo/v4

Usage

Import agollo
import "github.com/FarmerChillax/agollo/v4"
In order to use agollo, issue a client or use the built-in default client
to use the default global client

for namespaces with the format of properties, you need to specific the full name

agollo.Start(&agollo.Conf{
    AppID:          "your app id",
    Cluster:        "default",
    NameSpaceNames: []string{"application.properties"},
    MetaAddr:       "your apollo meta addr",
})
or to issue a new client to embedded into your program
apollo := agollo.New(&agollo.Conf{
                            AppID:          "your app id",
                            Cluster:        "default",
                            NameSpaceNames: []string{"application.properties"},
                            MetaAddr:       "your apollo meta addr",
                        })
apollo.Start()
Set config update callback
agollo.OnUpdate(func(event *ChangeEvent) {
    // do your business logic to handle config update
})
Get apollo values
// get values in the application.properties default namespace
val := agollo.GetString(Key)
// or indicate a namespace
other := agollo.GetString(key, agollo.WithNamespace("other namespace"))
Get namespace file contents

to get namespace of with a format of properties, you need to specific the full name of the namespace, e.g. namespace.properties in both options and configs

namespaceContent := agollo.GetContent(agollo.WithNamespace("application.properties"))
Get all keys
allKyes := agollo.GetAllKeys(namespace)
Subscribe to new namespaces
agollo.SubscribeToNamespaces("newNamespace1", "newNamespace2")

License

agollo is released under MIT license

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorStatusNotOK = errors.New("http resp code not ok")

Functions

func GetAllKeys

func GetAllKeys(opts ...OpOption) []string

GetAllKeys return all config keys in given namespace

func GetContent

func GetContent(opts ...OpOption) string

GetNameSpaceContent get contents of namespace

func GetPropertiesContent

func GetPropertiesContent(opts ...OpOption) string

GetPropertiesContent for properties namespace

func GetReleaseKey

func GetReleaseKey(opts ...OpOption) string

GetReleaseKey return release key for namespace

func GetString

func GetString(key string, opts ...OpOption) string

GetString get value from given namespace

func OnUpdate

func OnUpdate(handler func(*ChangeEvent))

OnUpdate get all updates

func Start

func Start(conf *Conf, opts ...ClientOption) error

Start agollo client with Conf, start will block until fetch all config to local

func Stop

func Stop() error

Stop sync config

func SubscribeToNamespaces

func SubscribeToNamespaces(namespaces ...string) error

SubscribeToNamespaces fetch namespace config to local and subscribe to updates

Types

type Change

type Change struct {
	Key        string
	OldValue   string
	NewValue   string
	ChangeType ChangeType
}

Change represent a single key change

func (*Change) String

func (c *Change) String() string

type ChangeEvent

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

ChangeEvent change event

func (*ChangeEvent) String

func (e *ChangeEvent) String() string

type ChangeType

type ChangeType int

ChangeType for a key

const (
	// ADD a new value
	ADD ChangeType = iota
	// MODIFY a old value
	MODIFY
	// DELETE ...
	DELETE
)

func (ChangeType) String

func (c ChangeType) String() string

type Client

type Client interface {
	// Start fetch all config to local cache and run period poll to keep update to remote server
	Start() error
	// Stop period poll
	Stop() error

	OnUpdate(func(*ChangeEvent))

	// Get string value for key
	GetString(key string, opts ...OpOption) string
	// GetContent for namespace
	GetContent(opts ...OpOption) string
	// GetPropertiesContent for properties namespace
	GetPropertiesContent(opts ...OpOption) string
	// GetAllKeys return all keys
	GetAllKeys(opts ...OpOption) []string
	// GetReleaseKey return release key for namespace
	GetReleaseKey(opts ...OpOption) string
	// SubscribeToNamespaces will subscribe to new namespace and keep update
	SubscribeToNamespaces(namespaces ...string) error
}

func NewClient

func NewClient(conf *Conf, opts ...ClientOption) Client

NewClient create client from conf

type ClientOption

type ClientOption func(*client)

func SkipLocalCache

func SkipLocalCache() ClientOption

func WithLogger

func WithLogger(logger Logger) ClientOption

WithLogger set client logger

type Conf

type Conf struct {
	AppID              string   `json:"app_id"`
	Cluster            string   `json:"cluster,omitempty"`
	NameSpaceNames     []string `json:"namespace_names,omitempty"`
	CacheDir           string   `json:"cache_dir,omitempty"`
	MetaAddr           string   `json:"meta_addr,omitempty"`
	AccesskeySecret    string   `json:"accesskey_secret,omitempty"`
	InsecureSkipVerify bool     `json:"insecure_skip_verify,omitempty"`
	Retry              int      `json:"retry,omitempty"`        // retry count. 0 means no retry
	SyncTimeout        int64    `json:"sync_timeout,omitempty"` // sync request timeout, unit:ms
	PollTimeout        int64    `json:"poll_timeout,omitempty"` // poll request timeout, unit:ms
}

Conf ...

func NewConf

func NewConf(name string) (*Conf, error)

NewConf create Conf from file

type Logger

type Logger interface {
	Infof(format string, args ...interface{})
	Errorf(format string, args ...interface{})
}

type OpOption

type OpOption func(op *operation)

func WithNamespace

func WithNamespace(namespace string) OpOption

WithNamespace set namespace for operation

Directories

Path Synopsis
internal
properties
Package properties is used to read or write or modify the properties document.
Package properties is used to read or write or modify the properties document.

Jump to

Keyboard shortcuts

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