3

适用于golang 的 配置中心

 1 year ago
source link: https://studygolang.com/articles/29535?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

configurator

地址:https://github.com/xincao9/configurator

配置器的特点:

  1. 完整的配置管理功能
  2. 多环境,多业务组,多服务,多版本 的配置分类
architectures

安装中间件

安装 dkv

docker pull xincao9/dkv
docker run -d -p 9090:9090 -p 6380:6380 dkv:latest

创建服务配置

curl -X PUT -H 'content-type:application/json' 'http://localhost:9090/kv' -d '{"k":"configurator|test|cbs|user-service|1.0", "v":"{\"redis\":{\"host\":\"localhost\",\"port\":\"6379\"}}"}'

推荐使用 configurator-ui 系统管理配置

如何使用SDK

获取SDK

go get github.com/xincao9/configurator

设置系统环境变量

export env="test" // 环境
export group="cbs" // 业务组
export project="user-service" // 项目
export version="1.0" // 版本
export master="localhost:9090" // dkv 的master地址
export slaves="" // dkv 的slaves地址,host1:port1,host2:port2

读取属性

configurator.C.Get("redis.host")
configurator.C.Get("redis.port")

设置管理端口

http.HandleFunc("/config", configurator.AllSettings)
http.ListenAndServe(":8080", nil)
r := gin.Default()
r.GET("/config", func(c *gin.Context) {
    c.JSON(http.StatusOK, configurator.C.AllSettings())
})

查看运行时的配置

curl -X GET 'http://localhost:8080/config'

知识


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK