Go 每日一库之 java 转 go 遇到 Apollo?让 agollo 来平滑迁移
ztj100 2025-01-10 18:40 16 浏览 0 评论
以下文章来源于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
----------------------------------------------------------------------------
相关推荐
- 使用Python编写Ping监测程序(python 测验)
-
Ping是一种常用的网络诊断工具,它可以测试两台计算机之间的连通性;如果您需要监测某个IP地址的连通情况,可以使用Python编写一个Ping监测程序;本文将介绍如何使用Python编写Ping监测程...
- 批量ping!有了这个小工具,python再也香不了一点
-
号主:老杨丨11年资深网络工程师,更多网工提升干货,请关注公众号:网络工程师俱乐部下午好,我的网工朋友。在咱们网工的日常工作中,经常需要检测多个IP地址的连通性。不知道你是否也有这样的经历:对着电脑屏...
- python之ping主机(python获取ping结果)
-
#coding=utf-8frompythonpingimportpingforiinrange(100,255):ip='192.168.1.'+...
- 网站安全提速秘籍!Nginx配置HTTPS+反向代理实战指南
-
太好了,你直接问到重点场景了:Nginx+HTTPS+反向代理,这个组合是现代Web架构中最常见的一种部署方式。咱们就从理论原理→实操配置→常见问题排查→高级玩法一层层剖开说,...
- Vue开发中使用iframe(vue 使用iframe)
-
内容:iframe全屏显示...
- Vue3项目实践-第五篇(改造登录页-Axios模拟请求数据)
-
本文将介绍以下内容:项目中的public目录和访问静态资源文件的方法使用json文件代替http模拟请求使用Axios直接访问json文件改造登录页,配合Axios进行登录请求,并...
- Vue基础四——Vue-router配置子路由
-
我们上节课初步了解Vue-router的初步知识,也学会了基本的跳转,那我们这节课学习一下子菜单的路由方式,也叫子路由。子路由的情况一般用在一个页面有他的基础模版,然后它下面的页面都隶属于这个模版,只...
- Vue3.0权限管理实现流程【实践】(vue权限管理系统教程)
-
作者:lxcan转发链接:https://segmentfault.com/a/1190000022431839一、整体思路...
- swiper在vue中正确的使用方法(vue中如何使用swiper)
-
swiper是网页中非常强大的一款轮播插件,说是轮播插件都不恰当,因为它能做的事情太多了,swiper在vue下也是能用的,需要依赖专门的vue-swiper插件,因为vue是没有操作dom的逻辑的,...
- Vue怎么实现权限管理?控制到按钮级别的权限怎么做?
-
在Vue项目中实现权限管理,尤其是控制到按钮级别的权限控制,通常包括以下几个方面:一、权限管理的层级划分...
- 【Vue3】保姆级毫无废话的进阶到实战教程 - 01
-
作为一个React、Vue双修选手,在Vue3逐渐稳定下来之后,是时候摸摸Vue3了。Vue3的变化不可谓不大,所以,本系列主要通过对Vue3中的一些BigChanges做...
- Vue3开发极简入门(13):编程式导航路由
-
前面几节文章,写的都是配置路由。但是在实际项目中,下面这种路由导航的写法才是最常用的:比如登录页面,服务端校验成功后,跳转至系统功能页面;通过浏览器输入URL直接进入系统功能页面后,读取本地存储的To...
- vue路由同页面重定向(vue路由重定向到外部url)
-
在Vue中,可以使用路由的重定向功能来实现同页面的重定向。首先,在路由配置文件(通常是`router/index.js`)中,定义一个新的路由,用于重定向到同一个页面。例如,我们可以定义一个名为`Re...
- 那个 Vue 的路由,路由是干什么用的?
-
在Vue里,路由就像“页面导航的指挥官”,专门负责管理页面(组件)的切换和显示逻辑。简单来说,它能让单页应用(SPA)像多页应用一样实现“不同URL对应不同页面”的效果,但整个过程不会刷新网页。一、路...
- Vue3项目投屏功能开发!(vue投票功能)
-
最近接了个大屏项目,产品想在不同的显示器上展示大屏项目不同的页面,做出来的效果图大概长这样...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)