apollo

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2022 License: Apache-2.0 Imports: 22 Imported by: 5

README

介绍

基于携程apollo的go客户端

功能

  • 动态实时更新通知 通过Watch订阅对应的namespace变更,当有变更时,保证推送一次给业务
  • 支持默认值 当配置值无法获取时,支持业务配置的默认值
  • 支持灰度发布 自动获取业务本地ip,当apollo配置中心配置了灰度,会获取灰度配置
  • 支持自定义认证和自定义http请求参数
  • 支持多namespace 支持watch多个namespace
  • 支持多格式 支持properties,yaml,json等格式
  • 支持本地文件备份 当配置中心出现问题时,通过备份文件加载
  • 支持notification方式更新
  • 支持自定义日志组件

TODO

  • 支持负载均衡
  • 支持获取单个key

使用

  • Init 初始化apollo配置
  • Watch 监听namespace结构体

具体使用可以参考apollo-go-demo

看下面例子

package main


type TestConfig struct {
	Map     map[string]string `json:"map"`
	Struct  Person            `json:"struct"`
	Strings []string          `json:"strings"`
	Ints    []int32           `json:"ints"`
	String  string            `json:"string"`
	Int     int64             `json:"int"`
	Float   float32           `json:"float"`
}

type Person struct {
	Name string `json:"name"`
	Age  int64  `json:"age"`
}

var (
	apolloApp = &Application{
		Addr:    "http://81.68.181.139:8080",
		AppId:   "apollo-go",
		Secret:  "",
		Cluster: "DEV",
	}
	mPtr  unsafe.Pointer
    // 默认值配置,配置获取不到时,会用默认值
	mDeft = &TestConfig{
		Float: 0.12,
		Ints:  []int32{2, 3},
	}
)

// DC 返回动态配置,在需要的时候用这个函数获取配置结构体
func DC() *TestConfig {
	p := atomic.LoadPointer(&mPtr)
	if nil != p {
		return (*TestConfig)(p)
	}
	return mDeft
}

func main() {
    c, _ := apollo.NewClient(apolloApp)
	err := c.Watch("application", mDeft, &mPtr)
    for i := 0; i < 100; i++ {
        fmt.Printf("dconf:%+v", DC())
        time.Sleep(1 * time.Second)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Apollo

type Apollo struct {
	AppId         string `json:"appId"`
	Cluster       string `json:"cluster"`
	NamespaceName string `json:"namespaceName"`
	ReleaseKey    string `json:"releaseKey"`

	Configurations map[string]json.RawMessage `json:"configurations"`
}

type Application

type Application struct {
	AppId   string `json:"appId"`
	Cluster string `json:"cluster"`
	Secret  string `json:"secret"`
	Addr    string `json:"addr"`
}

type Client

type Client struct {
	App *Application // application config
	// contains filtered or unexported fields
}

Client apollo client

func NewClient

func NewClient(c *Application, opt ...Option) (*Client, error)

NewClient new apollo client

func (*Client) Watch

func (c *Client) Watch(namespace string, deft interface{}, ptr *unsafe.Pointer) error

Watch watch namespace struct

type Notifcation

type Notifcation struct {
	NamespaceName string `json:"namespaceName"`
	NotifcationID int64  `json:"notificationId"`
}

type Option

type Option func(o *Options)

func Auth

func Auth(a auth.Auth) Option

Auth custom auth

func Backup

func Backup(b backup.Backup) Option

Backup custom backup read and write

func BackupPath

func BackupPath(p string) Option

BackupPath backup path

func EnableBackup

func EnableBackup(enable bool) Option

EnableBackup enable backup

func Logger

func Logger(l log.Logger) Option

func Transport

func Transport(t transport.Transport) Option

Transport custom request transport

func WatchInterval

func WatchInterval(t time.Duration) Option

WatchInterval watch interval

type Options

type Options struct {
	Auth          auth.Auth           // auth interface
	Transport     transport.Transport // transport interface
	WatchInterval time.Duration       // watch interval
	Backup        backup.Backup       // backup interface
	EnableBackup  bool                // enable backup
	BackupPath    string              // backup path
}

type WatchCallback

type WatchCallback func(ctx context.Context, apol *Apollo) error

WatchCallback watch callback define

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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