0

Apollo配置中心

 2 years ago
source link: https://www.liwenzhou.com/posts/Go/apollo/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Apollo配置中心

发布于2022/05/28 ,更新于2022/05/28 22:26:41

| Golang
|总阅读量:次

Apollo(阿波罗)是携程开源的一款可靠的分布式配置管理中心,它能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。

Apollo(阿波罗)是携程开源的一款可靠的分布式配置管理中心,它能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。

搭建开发环境

Apollo 官方提供了一个方便学习使用的docker-quick-start 环境。我们只需要git clone 官方仓库,就可以在项目中的scripts目录下找到这个docker-quick-start文件夹。

git clone https://github.com/apolloconfig/apollo.git

cd apollo/scripts/docker-quick-start/

docker-quick-start目录下执行下面的命令启动容器。

docker-compose up

等镜像下载完,容器启动之后,就可以访问本地的http://localhost:8070查看Apollo管理后台了。

consul ui

输入用户名apollo,密码admin后登录管理后台。

consul ui

Apollo支持4个维度管理Key-Value格式的配置:

  • application (应用)
  • environment (环境)
  • cluster (集群)
  • namespace (命名空间)

十分推荐大家看一下官方的Apollo使用指南,文档读到位,必定事半功倍。

go接入Apollo

社区中有很多 Go语言Apollo客户端可供选择,本文以https://github.com/philchia/agollo为例讲解如何使用Go语言接入Apollo获取配置。此外,也可以使用https://github.com/shima-park/agollo支持搭配 Viper 来使用。

go get -u github.com/philchia/agollo/v4
package main

import (
	"fmt"
	"log"

	"github.com/philchia/agollo/v4"
)

func main() {
	agollo.Start(&agollo.Conf{
		AppID:           "SampleApp",
		Cluster:         "dev",
		NameSpaceNames:  []string{"application.properties", "shopping_cart.yaml"},
		MetaAddr:        "http://localhost:8080",
		AccesskeySecret: "b8ceb3ec62f34030b1b1fd9a431e420b",
	})

	agollo.OnUpdate(func(event *agollo.ChangeEvent) {
		// 监听配置变更
		log.Printf("event:%#v\n", event)
	})
	log.Println("初始化Apollo配置成功")

	// 从默认的application.properties命名空间获取key的值
	val := agollo.GetString("timeout")
	log.Println(val)
	// 获取命名空间下所有key
	keys := agollo.GetAllKeys(agollo.WithNamespace("shopping_cart.yaml"))
	fmt.Println(keys)
	// 获取指定一个命令空间下key的值
	other := agollo.GetString("content", agollo.WithNamespace("shopping_cart.yaml"))
	log.Println(other)
	// 获取指定命名空间下的所有内容
	namespaceContent := agollo.GetContent(agollo.WithNamespace("shopping_cart.yaml"))
	log.Println(namespaceContent)
}

扫码关注微信公众号

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK