apollo

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 28, 2021 License: MIT Imports: 8 Imported by: 0

README

agollo

apollo golang client

based on github.com/shima-park/agollo

get config from apollo( with watch) and unmarshal to a struct
 apollo.StartAndUnmarshal(&c, apollo.BackFile("/tmp/test.log"))
get config from apollo( with watch)
apollo.Start(apollo.BackFile("/tmp/test.log"))
full example
package main

import (
	"flag"
	"fmt"
	"time"

	"github.com/ainiaa/go-apollo"
	"github.com/shima-park/agollo"
)

type TestConf struct {
	A string
	B string
	C int
}

func getApolloUnmarshal(c *TestConf)(agollo.Agollo, error){
	return apollo.StartAndUnmarshal(&c, apollo.BackFile("/tmp/test.log"))
}
func getApollo()(agollo.Agollo, error){
	return apollo.Start(apollo.BackFile("/tmp/test.log"))
}

/**
 * 执行需要添加命令行参数 test abc def --env TE --apollo_server http://apollocfgv2.xxx.xxx --apollo_appid xxx-service --unmarshal 0
 */
func main() {
	var unmarshal string
	var c TestConf
	flag.StringVar(&unmarshal, "unmarshal", "1", "Unmarshal")
	if unmarshal == "1" {
		_, err := getApolloUnmarshal(&c)
		if err != nil {
			fmt.Printf("apollo.Start error:%s \n", err.Error())
		} else {
			fmt.Println("apollo.Start success")
			fmt.Printf("conf:%+v\n", c)
		}
		go func() {
			for {
				fmt.Printf("[%s]t:%+v\n", time.Now().Format("2006-01-02 15:04:05"), c)
				time.Sleep(2 * time.Second)
			}
		}()
	} else {
		_, err := getApollo()
		if err != nil {
			fmt.Printf("apollo.Start error:%s \n", err.Error())
		} else {
			fmt.Println("apollo.Start success")
		}
	}

	fmt.Printf("apollo.StartAndUnmarshal:%+v\n", c)



	time.Sleep(100 * time.Hour)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetApollo

func GetApollo() agollo.Agollo

func GetApolloEnv

func GetApolloEnv() (e string, appId string, server string)

func Start

func Start(opts ...Option) (agollo.Agollo, error)

Start 启动goroutine去轮训apollo通知接口 param: opts return:

func StartAndUnmarshal

func StartAndUnmarshal(cs interface{}, opts ...Option) (agollo.Agollo, error)

StartAndUnmarshal 启动goroutine去轮训apollo通知接口 序列化到对应的结构体

Types

type Conf

type Conf struct {
	Endpoint           string // apollo 服务地址
	DefaultNamespace   string // 默认命名空间
	AppId              string // app_id
	Cluster            string // 默认的集群名称,默认:default
	LongPollerInterval int64  // 轮训间隔时间,默认:1s
	BackupFile         string // 备份文件存放地址,默认:.go-apollo
}

type Option

type Option func(*Options)

func BackFile

func BackFile(backFile string) Option

func DefaultNamespace

func DefaultNamespace(namespace string) Option

func WithoutConvertStruct

func WithoutConvertStruct() Option

type Options

type Options struct {
	ConvertStruct      bool   //自动转为struct
	BackFile           string //备份文件目录
	LongPollerInterval int64  //轮训时间间隔
	DefaultNamespace   string //默认namespace
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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