网站首页 > 技术文章 正文
以下文章来源于Go Official Blog ,作者Go Official Blog
Introduction
agollo 是Apollo的 Golang 客户端
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
如果在使用 golang 重构 java 的过程中,使用到了分布式配置中心 Apollo,那么最快的方式就是使用原来的配置,保持最平滑的迁移,这个时候你就需要一个 Apollo 的 golang 客户端,agollo 可以是你的一个选择。
使用指南
1.1.环境要求
- Go 1.11+ (最好使用Go 1.12)
1.2.依赖
1.2.1.使用 go get 方式
go get -u github.com/zouyx/agollo/v3@latest
1.2.2.使用 go mod 方式
go.mod
require github.com/zouyx/agollo/v3 latest
执行
go mod tidy
import
import ( "github.com/zouyx/agollo/v3")
FAQ
- 为什么要加+incompatible
- go.mod不能下载agollo的解决方法
1.3.必要设置
Apollo 客户端依赖于 AppId,Environment 等环境信息来工作,所以请确保阅读下面的说明并且做正确的配置:
- main : your application
- app.properties (必要) : 连接服务端必要配置
- seelog.xml(非必要)
1.3.1.配置
加载优先级:
- 类配置
- 环境变量指定配置文件
- 默认(app.properties)配置文件
1.3.1.1.类配置
会覆盖 app.properties中配置,在调用Start方法之前调用
readyConfig:=&AppConfig{ AppId:"test1", Cluster:"dev1", NamespaceName:"application1", Ip:"localhost:8889", } InitCustomConfig(func() (*AppConfig, error) { return readyConfig,nil })
类配置 agollo 的 demo:
package mainimport ( "fmt" "github.com/zouyx/agollo/v3" "github.com/zouyx/agollo/v3/env/config")func InitAgolloConfig() error { readyConfig := &config.AppConfig{ AppID: "test", Cluster: "default", NamespaceName: "application", IP: "localhost:8080", } agollo.InitCustomConfig(func() (*config.AppConfig, error) { return readyConfig, nil }) err := agollo.Start() if err != nil { fmt.Errorf("%v", err) return err } return nil}func main(){ //Init Apollo err := config.InitAgolloConfig() if err != nil { fmt.Errorf("初始化Apollo失败", err) panic(err) } fmt.Println("初始化Apollo配置成功") //Use your apollo key to test value := agollo.GetStringValue("key", "") fmt.Println(value)}
1.3.1.2.环境变量指定配置文件
Linux/Mac
export AGOLLO_CONF=/a/conf.properties
Windows
set AGOLLO_CONF=c:/a/conf.properties
配置文件内容与app.properties内容一样
1.3.1.3.文件配置 - app.properties
1.开发:请确保 app.properties 文件存在于workingdir目录下
2.打包后:请确保 app.properties 文件存在于与打包程序同级目录下,参考1.3.必要配置。
目前只支持json形式,其中字段包括:
- appId :应用的身份信息,是从服务端获取配置的一个重要信息。
- cluster :需要连接的集群,默认default
- namespaceName :命名空间,默认:application(具体定义参考:namespace),多namespace使用英文逗号分割, 非key/value配置(json,properties,yml等),则配置为:namespace.文件类型。如:namespace.json
- ip :Apollo的CONFIGSERVICE的ip,非METASERVICE地址
配置例子如下:
一般配置
{ "appId": "test", "cluster": "dev", "namespaceName": "application", "ip": "localhost:8888"}
多namespace配置
{ "appId": "test", "cluster": "dev", "namespaceName": "application, applications1", "ip": "localhost:8888"}
非key/value namespace配置
{ "appId": "test", "cluster": "dev", "namespaceName": "application.json,a.yml", "ip": "localhost:8888"}
1.4日志组件
参考:
- 自定义日志组件
- 使用seelog日志组件
启动方式
- 异步启动 agollo
场景:启动程序不依赖加载Apollo的配置。
func main() { go agollo.Start()}
- 同步启动 agollo(v1.2.0+)
场景:启动程序依赖加载 Apollo 的配置。例:初始化程序基础配置。
func main() { agollo.Start()}
- 启动 agollo - 自定义 logger 控件
func main() { agollo.StartWithLogger(loggerInterface)}
- 启动 agollo - 自定义 cache 控件 (v1.7.0+)
func main() { agollo.StartWithCache(cacheInterface)}
- 启动 agollo - 自定义各种控件 (v1.8.0+)
func main() { agollo.SetLogger(loggerInterface) agollo.SetCache(cacheInterface) agollo.Start()}
- 监听变更事件(阻塞)
func main() { event := agollo.ListenChangeEvent() changeEvent := <-event bytes, _ := json.Marshal(changeEvent) fmt.Println("event:", string(bytes))}
基本方法
- String
agollo.GetStringValue(Key,DefaultValue)
- Int
agollo.GetIntValue(Key,DefaultValue)
- Float
agollo.GetFloatValue(Key,DefaultValue)
- Bool
agollo.GetBoolValue(Key,DefaultValue)
切换namespace获取配置
- 根据namespace获取配置
config := agollo.GetConfig(namespace)
- String
config.GetStringValue(Key,DefaultValue)
- Int
config.GetIntValue(Key,DefaultValue)
- Float
config.GetFloatValue(Key,DefaultValue)
- Bool
config.GetBoolValue(Key,DefaultValue)
自定义日志组件
复制以下代码至项目中,并在其中引用日志组件的方法进行打印 log
type DefaultLogger struct {}func (this *DefaultLogger)Debugf(format string, params ...interface{}) {}func (this *DefaultLogger)Infof(format string, params ...interface{}) {}func (this *DefaultLogger)Warnf(format string, params ...interface{}) error { return nil}func (this *DefaultLogger)Errorf(format string, params ...interface{}) error { return nil}func (this *DefaultLogger)Debug(v ...interface{}) {}func (this *DefaultLogger)Info(v ...interface{}){}func (this *DefaultLogger)Warn(v ...interface{}) error{ return nil}func (this *DefaultLogger)Error(v ...interface{}) error{ return nil}
启动
func main() { agollo.SetLogger(&DefaultLogger{}) agollo.Start()}
自定义缓存组件
声明自定义缓存组件
//DefaultCache 默认缓存type DefaultCache struct { defaultCache sync.Map}//Set 获取缓存func (d *DefaultCache)Set(key string, value []byte, expireSeconds int) (err error) { d.defaultCache.Store(key,value) return nil}//EntryCount 获取实体数量func (d *DefaultCache)EntryCount() (entryCount int64){ count:=int64(0) d.defaultCache.Range(func(key, value interface{}) bool { count++ return true }) return count}//Get 获取缓存func (d *DefaultCache)Get(key string) (value []byte, err error){ v, ok := d.defaultCache.Load(key) if !ok{ return nil,errors.New("load default cache fail") } return v.([]byte),nil}//Range 遍历缓存func (d *DefaultCache)Range(f func(key, value interface{}) bool){ d.defaultCache.Range(f)}//Del 删除缓存func (d *DefaultCache)Del(key string) (affected bool) { d.defaultCache.Delete(key) return true}//Clear 清除所有缓存func (d *DefaultCache)Clear() { d.defaultCache=sync.Map{}}//DefaultCacheFactory 构造默认缓存组件工厂类type DefaultCacheFactory struct {}//Create 创建默认缓存组件func (d *DefaultCacheFactory) Create()CacheInterface { return &DefaultCache{}}
使用自定义缓存
agollo.SetCache(&DefaultCacheFactory{})agollo.Start()
- 项目地址: https://github.com/zouyx/agollo
----------------------------------------------------------------------------
猜你喜欢
- 2024-09-18 如何用Python抓抖音上的小姐姐(如何用python抓抖音上的小姐姐呢)
- 2024-09-18 建立深度学习服务器最简单的方法!快来get新技能吧
- 2024-09-18 Go 每日一库之 jobrunner(github每日一题)
- 2024-09-18 一个非常不错的项目,没有启动步骤文档「伪开源」
- 2024-09-18 渐进式Web应用(PWA)入门教程(上)(渐进式web app)
- 2024-09-18 教程|手把手教你在本地构建Nervos AppChain全家桶
- 2024-09-18 ssh 代理详细解释(ssh全局代理)
- 2024-09-18 数据持久化技术——MP(数据持久化层的主要作用)
- 2024-09-18 前端容易理解错的跨域原理(跨域是前端解决还是后端解决的)
- 2024-09-18 代理,生活中随处可见,Go中如何进行 HTTPS 代理呢?
- 1513℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 558℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 505℃MySQL service启动脚本浅析(r12笔记第59天)
- 484℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 482℃启用MySQL查询缓存(mysql8.0查询缓存)
- 462℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 442℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 439℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- c语言min函数头文件 (68)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)