apollo

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2019 License: MIT Imports: 16 Imported by: 2

README

Gopkg.in/apollo

Build Status Go Report Card

Purpose

The goal of this project is to make the easiest way of using Ctrip apollo for golang applications. This project has been forked from philchia/agollo since 2018.8 but change a lot.

Contributing

Fork -> Patch -> Push -> Pull Request

Feature

  • ✅ Multiple namespace support
  • ✅ Fail tolerant
  • ✅ Custom logger
  • ❎ YML to struct

Required

go 1.10 or later

Build

If you want build this project,should use go 1.11+

GO111MODULE=on; go mod download

Usage

Installation
# go mod (only go 1.11+) or project in gopath(go 1.10 +)
go get -u gopkg.in/apollo.v0
# if you use dep as your golang dep tool (go 1.10)
dep ensure -add  gopkg.in/apollo.v0
Set custom logger(Optional)

go-apoll use gopkg.in/logger.v1 as default logger provider. Any logger implemented apollo.Logger interface can be use as apollo logger provider(such as logrus).

//Logger interface
type Logger interface {
	Warnf(format string, v ...interface{})
	Warn(v ...interface{})
	Errorf(format string, v ...interface{})
	Error(v ...interface{})
	Infof(format string, v ...interface{})
	Info(v ...interface{})
	Debugf(format string, v ...interface{})
	Debug(v ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
}

set logrus as log provider

var log = logrus.New()
log.Formatter = new(logrus.JSONFormatter)
log.Formatter = new(logrus.TextFormatter)                     //default
log.Formatter.(*logrus.TextFormatter).DisableColors = true    // remove colors
log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output
log.Level = logrus.TraceLevel
log.Out = os.Stdout

apollo.SetLogger(log)
Start use default app.yml config file
    apollo.Start()
Start use given config file path
    apollo.StartWithConfFile(name)
Subscribe to updates
    events := apollo.WatchUpdate()
    changeEvent := <-events
    bytes, _ := json.Marshal(changeEvent)
    fmt.Println("event:", string(bytes))
Get apollo values
    apollo.GetStringValue(Key, defaultValue)
    apollo.GetStringValueWithNameSapce(namespace, key, defaultValue)
    apollo.GetIntValue(Key, defaultValue)
    apollo.GetIntValueWithNameSapce(namespace, key, defaultValue)
Get namespace file contents
    apollo.GetNameSpaceContent(namespace, defaultValue)

License

apollo is released under MIT lecense

Documentation

Overview

Package apollo ctrip apollo go client

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetIntValue added in v0.2.2

func GetIntValue(key string, defaultValue int) int

GetIntValue from default namespace

func GetNameSpaceContent

func GetNameSpaceContent(namespace, defaultValue string) string

GetNameSpaceContent get contents of namespace

func GetStringValue

func GetStringValue(key, defaultValue string) string

GetStringValue from default namespace

func GetStringValueWithNameSpace added in v0.1.1

func GetStringValueWithNameSpace(namespace, key, defaultValue string) string

GetStringValueWithNameSpace get value from given namespace

func ListKeys added in v0.3.0

func ListKeys(namespace string) []string

ListKeys list all keys under given namespace

func SetLogger added in v0.3.0

func SetLogger(userLog Logger)

SetLogger set user custome logger

func Start

func Start() error

Start apollo

func StartWithConf

func StartWithConf(conf *Conf) error

StartWithConf run apollo with Conf

func StartWithConfFile

func StartWithConfFile(name string) error

StartWithConfFile run apollo with conf file

func Stop

func Stop() error

Stop sync config

func WatchUpdate

func WatchUpdate() <-chan *ChangeEvent

WatchUpdate get all updates

Types

type Change

type Change struct {
	OldValue   []byte
	NewValue   []byte
	ChangeType ChangeType
}

Change represent a single key change

type ChangeEvent

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

ChangeEvent change event

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 struct {
	// contains filtered or unexported fields
}

Client for apollo

func NewClient

func NewClient(conf *Conf) *Client

NewClient create client from conf

func (*Client) GetIntValue added in v0.2.2

func (c *Client) GetIntValue(key string, defaultValue int) int

GetIntValue from default namespace

func (*Client) GetIntValueWithNameSpace added in v0.2.2

func (c *Client) GetIntValueWithNameSpace(namespace, key string, defaultValue int) int

GetIntValueWithNameSpace get int value from given namespace

func (*Client) GetNameSpaceContent

func (c *Client) GetNameSpaceContent(namespace, defaultValue string) string

GetNameSpaceContent get contents of namespace

func (*Client) GetStringValue

func (c *Client) GetStringValue(key, defaultValue string) string

GetStringValue from default namespace

func (*Client) GetStringValueWithNameSpace added in v0.1.1

func (c *Client) GetStringValueWithNameSpace(namespace, key, defaultValue string) string

GetStringValueWithNameSpace get value from given namespace

func (*Client) ListKeys added in v0.3.0

func (c *Client) ListKeys(namespace string) []string

ListKeys list all keys under given namespace

func (*Client) Start

func (c *Client) Start() error

Start sync config

func (*Client) Stop

func (c *Client) Stop() error

Stop sync config

func (*Client) WatchUpdate

func (c *Client) WatchUpdate() <-chan *ChangeEvent

WatchUpdate get all updates

type Conf

type Conf struct {
	AppID      string   `yaml:"appId"`
	Cluster    string   `yaml:"cluster"`
	Namespaces []string `yaml:"namespaces,flow"`
	IP         string   `json:"ip"`
}

Conf ...

func NewConf

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

NewConf create Conf from file

type Logger added in v0.3.0

type Logger interface {
	Warnf(format string, v ...interface{})
	Warn(v ...interface{})
	Errorf(format string, v ...interface{})
	Error(v ...interface{})
	Infof(format string, v ...interface{})
	Info(v ...interface{})
	Debugf(format string, v ...interface{})
	Debug(v ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
}

Logger interface

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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