Help us shape the future of Go! Take the 2026 Go Developer Survey

apolloclient

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2020 License: Apache-2.0 Imports: 11 Imported by: 1

README

apolloClient

Apollo Go Client

GitHub Release License

Feature

GetConfig(param *GetConfigParam) (ConfigData, error)
GetConfigCache(param *GetConfigParam) (ConfigData, error)
GetNotifications(param *GetNotificationsParam) (bool, []Notification, error)

// support set access_key for configService
signature(secret, uri string, timestamp time.Time) (string, error)

Example

https://github.com/2345tech/apollo-agent

Demo

poll
func TestPoll(t *testing.T) {
	client, err := NewClient("http://your-apollo.config-service.address", nil, nil)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	param := GetConfigParam{
		AppID:     "demo_apollo_agent",
		Namespace: "application",
		Secret:    "a93ab23b59044e10b9bce61c4629e666",
	}

	data, err := client.GetConfigCache(&param)
	//data, err := client.GetConfig(&param)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println(data.Configs)
}
watch
func TestWatch(t *testing.T) {
	var body io.Reader
	var request *http.Request
	var err error
	var client *Client

	ctx, cancel := context.WithCancel(context.Background())
	request, err = http.NewRequestWithContext(ctx, http.MethodGet, "", body)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	client, err = NewClient("http://your-apollo.config-service.address", nil, nil)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	client.Request = request

	notificationParam := &GetNotificationsParam{
		AppID:         "demo_apollo_agent",
		Secret:        "a93ab23b59044e10b9bce61c4629e666",
		Notifications: make([]Notification, 0),
	}
	notificationParam.Notifications = []Notification{
		Notification{
			Namespace:      "application",
			NotificationID: 0,
		},
	}
	param := GetConfigParam{
		AppID:     "demo_apollo_agent",
		Namespace: "application",
		Secret:    "a93ab23b59044e10b9bce61c4629e666",
	}

	go func() {
		for {
			select {
			case <-ctx.Done():
				fmt.Println("cancel request")
				return
			default:
				if update, notifications, err := client.GetNotifications(notificationParam); err != nil {
					fmt.Println("GetNotifications from Apollo Config Service error:" + err.Error())
				} else if update {
					notificationParam.Notifications[0].NotificationID = notifications[0].NotificationID
					if data, err := client.GetConfig(&param); err == nil {
						fmt.Println(data.Configs)
						param.ReleaseKey = data.ReleaseKey
					} else {
						fmt.Println("GetConfig from Apollo Config Service error:" + err.Error())
					}
				} else {
					fmt.Println("update = false")
				}
			}
		}
	}()

	time.Sleep(10 * time.Second)
	cancel()
	time.Sleep(1 * time.Second)
	fmt.Println("done")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client added in v1.1.0

type Client struct {
	HttpClient *http.Client
	Request    *http.Request
	BaseURL    *url.URL
}

func NewClient added in v1.1.0

func NewClient(serverAddr string, httpClient *http.Client, request *http.Request) (*Client, error)

func (*Client) GetConfig added in v1.1.0

func (c *Client) GetConfig(param *GetConfigParam) (ConfigData, error)

GetConfig 通过不带缓存的Http接口从Apollo读取配置

func (*Client) GetConfigCache added in v1.1.0

func (c *Client) GetConfigCache(param *GetConfigParam) (ConfigData, error)

GetConfigCache 通过带缓存的Http接口从Apollo读取配置

func (*Client) GetNotifications added in v1.1.0

func (c *Client) GetNotifications(param *GetNotificationsParam) (bool, []Notification, error)

GetNotifications 应用感知配置更新(默认最长阻塞60秒)

type ConfigData

type ConfigData struct {
	AppID      string            `json:"appId"`          // 应用ID
	Cluster    string            `json:"cluster"`        // 集群名称
	Namespace  string            `json:"namespaceName"`  // 命名空间
	Configs    map[string]string `json:"configurations"` // 配置项列表
	ReleaseKey string            `json:"releaseKey"`     // 当前的releaseKey
}

type GetConfigParam

type GetConfigParam struct {
	AppID      string // 应用ID,必填
	Cluster    string // 集群名称,选填
	Namespace  string // 命名空间,必填
	Secret     string // 授权密钥,选填
	ReleaseKey string // 上一次的releaseKey,选填
	ClientIP   string // 应用部署机器IP,选填
}

type GetNotificationsParam

type GetNotificationsParam struct {
	AppID         string         // 应用ID,必填
	Cluster       string         // 集群名称,必填
	Secret        string         // 授权密钥,必填
	Notifications []Notification // 通知列表,必填
}

type Notification

type Notification struct {
	Namespace      string `json:"namespaceName"`  // 命名空间,必填
	NotificationID int64  `json:"notificationId"` // 通知ID,必填
}

Jump to

Keyboard shortcuts

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