Golang如何优雅接入多个远程配置中心?_golang 读取多个apollo配置文件

本文基于viper实现了apollo多实例快速接入,授人以渔,带着大家读源码,详解实现思路,封装成自己的工具类并且开源。

前言

viper是适用于go应用程序的配置解决方案,这款配置管理神器,支持多种类型、开箱即用、极易上手。

本地配置文件的接入能很快速的完成,那么对于远程apollo配置中心的接入,是否也能很快速完成呢?如果有多个apollo实例都需要接入,是否能支持呢?以及apollo远程配置变更后,是否能支持热加载,实时更新呢?

拥抱开源

带着上面的这些问题,结合实际商业项目的实践,已经有较成熟的解决方案。本着分享的原则,现已将xconfig包脱敏开源:github地址,欢迎体验和star。

下面快速介绍下xconfig包的使用与能力,然后针对包的封装实践做个讲解

获取安装

go get -u github.com/jinzaigo/xconfig

Features
  • 支持viper包诸多同名方法
  • 支持本地配置文件和远程apollo配置热加载,实时更新
  • 使用sync.RWMutex读写锁,解决了viper并发读写不安全问题
  • 支持apollo配置中心多实例配置化快速接入
接入示例
本地配置文件

指定配置文件路径完成初始化,即可通过xconfig.GetLocalIns().xxx()链式操作,读取配置

package main

import (
“fmt”
“github.com/jinzaigo/xconfig”
)

func main() {
if xconfig.IsLocalLoaded() {
fmt.Println(“local config is loaded”)
return
}
//初始化
configIns := xconfig.New(xconfig.WithFile(“example/config.yml”))
xconfig.InitLocalIns(configIns)

//读取配置
fmt.Println(xconfig.GetLocalIns().GetString(“appId”))
fmt.Println(xconfig.GetLocalIns().GetString(“env”))
fmt.Println(xconfig.GetLocalIns().GetString(“apollo.one.endpoint”))
}

xxx支持的操作方法:

  • IsSet(key string) bool
  • Get(key string) interface{}
  • AllSettings() map[string]interface{}
  • GetStringMap(key string) map[string]interface{}
  • GetStringMapString(key string) map[string]string
  • GetStringSlice(key string) []string
  • GetIntSlice(key string) []int
  • GetString(key string) string
  • GetInt(key string) int
  • GetInt32(key string) int32
  • GetInt64(key string) int64
  • GetUint(key string) uint
  • GetUint32(key string) uint32
  • GetUint64(key string) uint64
  • GetFloat(key string) float64
  • GetFloat64(key string) float64
  • GetFloat32(key string) float32
  • GetBool(key string) bool
  • SubAndUnmarshal(key string, i interface{}) error
远程apollo配置中心

指定配置类型与apollo信息完成初始化,即可通过xconfig.GetRemoteIns(key).xxx()链式操作,读取配置

单实例场景

//初始化
configIns := xconfig.New(xconfig.WithConfigType(“properties”))
err := configIns.AddApolloRemoteConfig(endpoint, appId, namespace, backupFile)
if err != nil {
…handler
}
xconfig.AddRemoteIns(“ApplicationConfig”, configIns)

//读取配置
fmt.Println(xconfig.GetRemoteIns(“ApplicationConfig”).AllSettings())

多实例场景

在本地配置文件config.yaml维护apollo配置信息,然后批量完成多个实例的初始化,即可通过xconfig.GetRemoteIns(key).xxx()链式操作,读取配置

#apollo配置,支持多实例多namespace
apollo:
one:
endpoint: xxx
appId: xxx
namespaces:
one:
key: ApplicationConfig #用于读取配置,保证全局唯一,避免相互覆盖
name: application #注意:name不要带类型(例如application.properties),这里name和type分开配置
type: properties
two:
key: cipherConfig
name: cipher
type: properties
backupFile: /tmp/xconfig/apollo_bak/test.agollo #每个appId使用不同的备份文件名,避免相互覆盖

package main

import (
“fmt”
“github.com/jinzaigo/xconfig”
)

type ApolloConfig struct {
Endpoint string json:"endpoint"
AppId string json:"appId"
Namespaces map[string]ApolloNameSpace json:"namespaces"
BackupFile string json:"backupFile"
}

type ApolloNameSpace struct {
Key string json:"key"
Name string json:"name"
Type string json:"type"
}

func main() {
//本地配置初始化
xconfig.InitLocalIns(xconfig.New(xconfig.WithFile(“example/config.yml”)))
if !xconfig.GetLocalIns().IsSet(“apollo”) {
fmt.Println(“without apollo key”)
return
}

apolloConfigs := make(map[string]ApolloConfig, 0)
err := xconfig.GetLocalIns().SubAndUnmarshal(“apollo”, &apolloConfigs)
if err != nil {
fmt.Println(apolloConfigs)
fmt.Println(“SubAndUnmarshal error:”, err.Error())
return
}

//多实例初始化
for _, apolloConfig := range apolloConfigs {
for _, namespaceConf := range apolloConfig.Namespaces {
configIns := xconfig.New(xconfig.WithConfigType(namespaceConf.Type))
err = configIns.AddApolloRemoteConfig(apolloConfig.Endpoint, apolloConfig.AppId, namespaceConf.Name, apolloConfig.BackupFile)
if err != nil {
fmt.Println(“AddApolloRemoteConfig error:” + err.Error())
}
xconfig.AddRemoteIns(namespaceConf.Key, configIns)
}
}

//读取
fmt.Println(xconfig.GetRemoteIns(“ApplicationConfig”).AllSettings())
}

封装实践

欢迎大家关注我们,拥抱开源:加入我和劲仔的交流群

学会使用xconfig包后,能快速的实现本地配置文件和远程apollo配置中心多实例的接入。再进一步了解这个包在封装过程都中遇到过哪些问题,以及对应的解决方案,能更深入的理解与使用这个包,同时也有助于增加读者自己在封装新包时的实践理论基础。

1.viper远程连接不支持apollo

查看viper的使用文档,会发现viper是支持远程K/V存储连接的,所以一开始我尝试着连接apollo

v := viper.New()
v.SetConfigType(“properties”)
err := v.AddRemoteProvider(“apollo”, “http://endpoint”, “application”)
if err != nil {
panic(fmt.Errorf(“AddRemoteProvider error: %s”, err))
}
fmt.Println(“AddRemoteProvider success”)
//执行结果:
//panic: AddRemoteProvider error: Unsupported Remote Provider Type “apollo”

执行后发现,并不支持apollo,随即查看viper源码,发现只支持以下3个provider

// SupportedRemoteProviders are universally supported remote providers.
var SupportedRemoteProviders = []string{“etcd”, “consul”, “firestore”}

解决方案:

安装shima-park/agollo包: go get -u github.com/shima-park/agollo

安装成功后,只需要在上面代码基础上,最前面加上 remote.SetAppID("appId") 即可连接成功

import (
“fmt”
remote “github.com/shima-park/agollo/viper-remote”
“github.com/spf13/viper”
)

remote.SetAppID(“appId”)
v := viper.New()
v.SetConfigType(“properties”)
err := v.AddRemoteProvider(“apollo”, “http://endpoint”, “application”)
if err != nil {
panic(fmt.Errorf(“AddRemoteProvider error: %s”, err))
}
fmt.Println(“AddRemoteProvider success”)
//执行结果:
//AddRemoteProvider success

2.agollo是怎么让viper支持apollo连接的呢

不难发现,在执行 remote.SetAppID("appId") 之前,remote.go 中init方法,会往viper.SupportedRemoteProviders中append一个"apollo",其实就是让viper认识一下这个provider,随后将viper.RemoteConfig 做重新赋值,并重新实现了viper中的Get Watch WatchChannel这3个方法,里边就会做apollo连接的适配。

//github.com/shima-park/agollo/viper-remote/remote.go 278-284行
func init() {
viper.SupportedRemoteProviders = append(
viper.SupportedRemoteProviders,
“apollo”,
)
viper.RemoteConfig = &configProvider{}
}

//github.com/spf13/viper/viper.go 113-120行
type remoteConfigFactory interface {
Get(rp RemoteProvider) (io.Reader, error)
Watch(rp RemoteProvider) (io.Reader, error)
WatchChannel(rp RemoteProvider) (<-chan *RemoteResponse, chan bool)
}

// RemoteConfig is optional, see the remote package
var RemoteConfig remoteConfigFactory

3.agollo只支持apollo单实例,怎么扩展为多实例呢

执行remote.SetAppID("appId")之后,这个appId是往全局变量appID里写入的,并且在初始化时也是读取的这个全局变量。带来的问题就是不支持apollo多实例,那么解决呢

//github.com/shima-park/agollo/viper-remote/remote.go 26行
var (
// apollod的appid
appID string

)
func SetAppID(appid string) {
appID = appid
}

//github.com/shima-park/agollo/viper-remote/remote.go 252行
switch rp.Provider() {

case “apollo”:
return newApolloConfigManager(appID, rp.Endpoint(), defaultAgolloOptions)
}

解决方案:

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Go语言工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Go语言全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Golang知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Go)
img

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

67137015)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Golang知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Go)
[外链图片转存中…(img-RVeUhcdY-1712967137015)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值