peach

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

peach SDK

找到配置中心

为了更好地管理配置,尽可能避免由修改配置带来的失误。 我们引入配置中心模块化思想,可以引入apollo等远程配置中心。 为了区分环境,我们在apollo加入DEV(开发环境) UAT(测试环境) PRO(开发环境)

使用方式

peach 是一个config SDK客户端,包括了remote、file(开发中)几个抽象功能,方便使用本地文件或者远程配置中心,并且集成了对象自动reload功能(开发中)。

远程配置中心

需要配合引入环境变量 例如apollo:

    export APOLLO_META_ADDR=118.178.140.41:58079
    export APOLLO_APP_ID=backend_server
名称 说明
APOLLO_META_ADDR apollo配置中心地址
APOLLO_APP_ID apollo的appid
指定配置文件路径:
    export CONFIG_FILE_PATH=/usr/local
example apollo
import (
	"fmt"

    "github.com/zdao-pro/sky_blue/pkg/peach"
    // 使用apollo需要引入apollo包,实现自动注册驱动
	_ "github.com/zdao-pro/sky_blue/pkg/peach/apollo"
)
// main.go
func main() {
    // 初始化配置中心
    /*
       @param:
         peach.PeachDriverApollo : 配置驱动名,
         zdao_backend.sky_blue : apollo namespace,apollo需要指定namespace
    */
    peach.Init(peach.PeachDriverApollo, []string{"zdao_backend.sky_blue"})
    // Get用来获取key的值(value)
	a, _ := peach.Get("test.yaml").String()
	fmt.Println(a)
}

Documentation

Index

Constants

View Source
const (
	//PeachDriverApollo
	PeachDriverApollo = "apollo"
)

Variables

View Source
var (
	ErrNotExist      = errors.New("peach: value key not exit")
	ErrDifferentType = errors.New("peach: value type different")
	ErrTypeAssertion = errors.New("paladin: value type assertion no match")
)

ErrNotExist value key not exist.

Functions

func Init

func Init(driverName string, args ...interface{}) error

Init 初始化配置中心

func KeyNamed added in v1.2.3

func KeyNamed(key string) string

KeyNamed key naming to lower case.

func RegistDriver

func RegistDriver(name string, driver Driver)

RegistDriver regist driver

Types

type ApoClient added in v1.3.3

type ApoClient struct {
	// contains filtered or unexported fields
}

ApoClient 实例化的apollo配置中心

func (ApoClient) Get added in v1.3.3

func (ac ApoClient) Get(key string) *Value

Get 获取NameSpace下key的值

type Client

type Client interface {
	Get(k string) *Value
}

Client 配置客户端通用接口

var (

	//ErrInitConfigException 配置中心初始化异常
	ErrInitConfigException = errors.New("Init Config Center Error")
	//DefaultClient default config client
	DefaultClient Client
)

type Config added in v1.3.3

type Config struct {
	// contains filtered or unexported fields
}

Config apollo config struct

type Driver

type Driver interface {
	New(args ...interface{}) (Client, error)
}

Driver defined paladin remote client impl each remote config center driver must do 1. implements `New` method

func GetDriver

func GetDriver(name string) (Driver, error)

GetDriver return Driver by DriverName

type Map added in v1.2.3

type Map struct {
	// contains filtered or unexported fields
}

Map is config map, key(filename) -> value(file).

func (*Map) Exist added in v1.2.3

func (m *Map) Exist(key string) bool

Exist check if values map exist a key.

func (*Map) Get added in v1.2.3

func (m *Map) Get(key string) *Value

Get return get value by key.

func (*Map) Keys added in v1.2.3

func (m *Map) Keys() []string

Keys return map keys.

func (*Map) Load added in v1.2.3

func (m *Map) Load() map[string]*Value

Load returns the value set by the most recent Store.

func (*Map) Store added in v1.2.3

func (m *Map) Store(values map[string]*Value)

Store sets the value of the Value to values map.

type TomlClient

type TomlClient struct {
	// contains filtered or unexported fields
}

TomlClient 解析toml文件的客户端

func (*TomlClient) Get

func (t *TomlClient) Get(k string) *Value

Get 获取key

func (*TomlClient) NewToml

func (t *TomlClient) NewToml(text []byte) (*TomlClient, error)

NewToml 新建toml

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value is config value

func Get

func Get(key string) *Value

Get return value by key.

func NewValue

func NewValue(val interface{}, raw string) *Value

NewValue return new value

func (*Value) Bool

func (v *Value) Bool() (bool, error)

Bool return bool value

func (*Value) Float32

func (v *Value) Float32() (float32, error)

Float32 return float32 value.

func (*Value) Float64

func (v *Value) Float64() (float64, error)

Float64 return float64 value.

func (*Value) Int

func (v *Value) Int() (int, error)

Int return Int value

func (*Value) Int32

func (v *Value) Int32() (int32, error)

Int32 return int32 value.

func (*Value) Int64

func (v *Value) Int64() (int64, error)

Int64 return int64 value.

func (*Value) Raw

func (v *Value) Raw() (string, error)

Raw return raw value.

func (*Value) String

func (v *Value) String() (string, error)

String return string value.

func (*Value) UnmarshalJSON added in v1.2.3

func (v *Value) UnmarshalJSON(dst interface{}) error

UnmarshalJSON unmarhsal json to struct.

func (*Value) UnmarshalYAML

func (v *Value) UnmarshalYAML(dst interface{}) error

UnmarshalYAML unmarshal yaml to struct.

Jump to

Keyboard shortcuts

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