apollo集群部署,apollo集群

  

  以下文章来自Go官方博客,由Go官方博客撰写   

  

     

  

  agollo是Apollo的Golang客户。   

  

  Apollo(阿波罗)是携程框架部开发的分布式配置中心。可以集中管理不同环境和集群的配置,配置修改后可以实时推送到应用端。具有标准化权限和流程管理的特点,适用于微服务配置管理场景。   

  

  如果在用golang进行java重构的过程中使用了Apollo这个分布式配置中心,那么最快的方法就是使用原来的配置,并保持平滑迁移。这时候你需要一个Apollo的golang客户端,Agolo可以是你的选择。   

  

  使用指南1.1。环境要求Go 1.11(最好是Go 1.12)1.2。依赖性1.2.1。github.com/zouyx/agollo/v3@latest1.2.2.使用go.mod   

  

  要求github.com/zouyx/agollo/v3最新执行   

  

  go mod tidyimport   

  

  导入(' github.com/zouyx/agollo/v3 ')常见问题   

  

  为什么要加不兼容的lego.mod?不能下载Agolo的解决方案1.3。需要根据AppId、环境和其他环境信息来设置Apollo客户端,因此请确保阅读以下说明并进行正确配置:   

  

  main:yoursapplicationapp . properties(必选):连接服务器必须配置seelog.xml(非必选)1.3.1。配置加载优先级:   

  

  配置环境变量指定配置文件默认(app.properties)配置文件1.3.1.1。类配置重写app.properties中的配置,并在调用Start方法之前调用。   

  

  ready config :=AppConfig { appid : ' test1 ',Cluster:'dev1 ',namespace name : ' application 1 ',Ip: ' localhost:8889 ',} class initcustomconfig(func()(* AppConfig,error) {return ready config,nil})配置agollo的demo:   

  

  package main import(' fmt ' ' github . com/zouyx/agollo/v3 ' ' github . com/zouyx/agollo/v3/env/config ')func initagoloconfig()error { ready config :=config。AppConfig{ AppID: 'test ',Cluster: 'default ',NamespaceName: 'application ',IP: 'localhost:8080 ',} agollo。InitCustomConfig(func()(* config。AppConfig,error) { return readyConfig,nil })err :=a Gallo。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配置初始化成功')//用你的Apollo键测试value 3360=agollo . getstringvalue(' key ',' ')fmt . println(value)} 1.3.1.2。环境变量指定配置文件Linux/Mac   

  

  导出阿格洛_CONF=/a/conf.prop   

ertiesWindows

  

set AGOLLO_CONF=c:/a/conf.properties配置文件内容与app.properties内容一样

  

1.3.1.3.文件配置 - app.properties1.开发:请确保 app.properties 文件存在于workingdir目录下

  

2.打包后:请确保 app.properties 文件存在于与打包程序同级目录下,参考1.3.必要配置。

  

目前只支持json形式,其中字段包括:

  

appId :应用的身份信息,是从服务端获取配置的一个重要信息。cluster :需要连接的集群,默认defaultnamespaceName :命名空间,默认:application(具体定义参考:namespace),多namespace使用英文逗号分割, 非key/value配置(json,properties,yml等),则配置为:namespace.文件类型。如:namespace.jsonip :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))}基本方法Stringagollo.GetStringValue(Key,DefaultValue)Intagollo.GetIntValue(Key,DefaultValue)Floatagollo.GetFloatValue(Key,DefaultValue)Boolagollo.GetBoolValue(Key,DefaultValue)切换namespace获取配置根据namespace获取配置config := agollo.GetConfig(namespace)Stringconfig.GetStringValue(Key,DefaultValue)Intconfig.GetIntValue(Key,DefaultValue)Floatconfig.GetFloatValue(Key,DefaultValue)Boolconfig.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----------------------------------------------------------------------------

相关文章