源码先锋

源码先锋

Go 每日一库之 java 转 go 遇到 Apollo?让 agollo 来平滑迁移

admin 98 174

Introduction

agollo是Apollo的Golang客户端

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

使用指南1.1.环境要求

+(最好使用)

1.2.依赖1.2.1.使用goget方式
/zouyx/agollo/v3@latest
1.2.2.使用gomod方式
/zouyx/agollo/v3latest

执行

gomodtidy

import

import("/zouyx/agollo/v3")

FAQ

为什么要加+incompatible

不能下载agollo的解决方法

1.3.必要设置

Apollo客户端依赖于AppId,Environment等环境信息来工作,所以请确保阅读下面的说明并且做正确的配置:

main:yourapplication

(必要):连接服务端必要配置

(非必要)

1.3.1.配置

加载优先级:

类配置

环境变量指定配置文件

默认()配置文件

1.3.1.1.类配置

会覆盖中配置,在调用Start方法之前调用

readyConfig:=AppConfig{AppId:"test1",Cluster:"dev1",NamespaceName:"application1",Ip:"localhost:8889",}InitCustomConfig(func()(*AppConfig,error){returnreadyConfig,nil})

类配置agollo的demo:

packagemainimport("fmt""/zouyx/agollo/v3""/zouyx/agollo/v3/env/config")funcInitAgolloConfig()error{readyConfig:={AppID:"test",Cluster:"default",NamespaceName:"application",IP:"localhost:8080",}(func()(*,error){returnreadyConfig,nil})err:=()iferr!=nil{("%v",err)returnerr}returnnil}funcmain(){//InitApolloerr:=()iferr!=nil{("初始化Apollo失败",err)panic(err)}("初始化Apollo配置成功")//Useyourapollokeytotestvalue:=("key","")(value)}
1.3.1.2.环境变量指定配置文件

Linux/Mac

exportAGOLLO_CONF=/a/

Windows

setAGOLLO_CONF=c:/a/

配置文件内容与内容一样

1.3.1.3.文件配置-

1.开发:请确保文件存在于workingdir目录下

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

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

appId:应用的身份信息,是从服务端获取配置的一个重要信息。

cluster:需要连接的集群,默认default

namespaceName:命名空间,默认:application(具体定义参考:namespace),多namespace使用英文逗号分割,非key/value配置(json,properties,yml等),则配置为:namespace.文件类型。如:

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/valuenamespace配置

{"appId":"test","cluster":"dev","namespaceName":",","ip":"localhost:8888"}
1.4日志组件

参考:

自定义日志组件

使用seelog日志组件

启动方式

异步启动agollo

场景:启动程序不依赖加载Apollo的配置。

funcmain(){()}

同步启动agollo(+)

场景:启动程序依赖加载Apollo的配置。例:初始化程序基础配置。

funcmain(){()}

启动agollo-自定义logger控件

funcmain(){(loggerInterface)}

启动agollo-自定义cache控件(+)

funcmain(){(cacheInterface)}

启动agollo-自定义各种控件(+)

funcmain(){(loggerInterface)(cacheInterface)()}

监听变更事件(阻塞)

funcmain(){event:=()changeEvent:=-eventbytes,_:=(changeEvent)("event:",string(bytes))}
基本方法

String

(Key,DefaultValue)

Int

(Key,DefaultValue)

Float

(Key,DefaultValue)

Bool

(Key,DefaultValue)
切换namespace获取配置

根据namespace获取配置

config:=(namespace)

String

(Key,DefaultValue)

Int

(Key,DefaultValue)

Float

(Key,DefaultValue)

Bool

(Key,DefaultValue)
自定义日志组件

复制以下代码至项目中,并在其中引用日志组件的方法进行打印log

typeDefaultLoggerstruct{}func(this*DefaultLogger)Debugf(formatstring,paramsinterface{}){}func(this*DefaultLogger)Infof(formatstring,paramsinterface{}){}func(this*DefaultLogger)Warnf(formatstring,paramsinterface{})error{returnnil}func(this*DefaultLogger)Errorf(formatstring,paramsinterface{})error{returnnil}func(this*DefaultLogger)Debug(vinterface{}){}func(this*DefaultLogger)Info(vinterface{}){}func(this*DefaultLogger)Warn(vinterface{})error{returnnil}func(this*DefaultLogger)Error(vinterface{})error{returnnil}

启动

----------------------------------------------------------------------------