Consul 是用于分布式系统服务发现与配置,内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value 存储、多数据中心方案。Consul是使用Go语言编写的,使用很方便。有个需求,配置文件里面的参数经常会调整,变动后需要实时获取,今天来实现监听consul中key/value 数据的变化。
在ubuntu安装consul并配置启动服务,
$ wget https://releases.hashicorp.com/consul/1.2.0/consul_1.2.0_linux_amd64.zip
$ yum install unzip
$ unzip consul_0.7.2_linux_amd64.zip
$ mv consul /usr/local/bin/consul
配置好后,控制台可以输出consul,可以看到有输出。
因为是监控配置文件,所以搭配的是一台服务器。拉取consul 镜像后,通过docker来启动:
sudo docker run -d -p 8500:8500 --restart=always --name=consul consul:latest agent -server -bootstrap -ui -node=1 -client=‘0.0.0.0’
命令说明
-p8500:8500 端口映射:前表示主机部分,:后表示容器部分。
-restart=always 重启方式:表示docker会自动重启该容器
–name=consulconsul:latest 指定该容器名称,查看和进行操作都比较方便。
agent 启动 Agent 进程
-server启动 Consul Server 模式
-bootstrap表示这个节点是 Server-Leader 。
-ui启动 Web UI 管理器 可访问8500查看
-node 节点名称集群中必须是唯一的,默认是该节点的主机名。
-client启动 Consul Cilent 模式consul服务侦听地址 提供HTTP、DNS、RPC等服务默认是127.0.0.1对外提供服务改成0.0.0.0
成功启动后,浏览器输入 http://192.168.x.xxx:8500/ui查看
可以在Key/Value中添加所需要的配置,这里创建格式为yaml
接下来需要代码去监听consul配置的数据变化,在做出更改时,能够及时返回最新的数据。
package main
import (
"bytes"
"fmt"
"log"
"time"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/api/watch"
"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
)
var (
defaultConfig *viper.Viper
consulAddress string
consulPath string
)
func initConfig() *viper.Viper {
consulAddress = "http://192.168.2.xxx:8500"
consulPath = "config/local"
defaultConfig = viper.New()
defaultConfig.SetConfigType("yaml")
consulClient, err := consulapi.NewClient(&consulapi.Config{Address: consulAddress})
if err != nil {
log.Fatalln("consul连接失败:", err)
}
kv, _, err := consulClient.KV().Get(consulPath, nil)
if err != nil {
log.Fatalln("consul获取配置失败:", err)
}
err = defaultConfig.ReadConfig(bytes.NewBuffer(kv.Value))
if err != nil {
log.Fatalln("Viper解析配置失败:", err)
}
go watchConfig()
return defaultConfig
}
func watchConfig() {
time.Sleep(time.Second * 10)
params := make(map[string]interface{})
params["type"] = "key"
params["key"] = consulPath
w, err := watch.Parse(params)
if err != nil {
log.Fatalln(err)
}
w.Handler = func(u uint64, i interface{}) {
kv := i.(*consulapi.KVPair)
hotconfig := viper.New()
hotconfig.SetConfigType("yaml")
err = hotconfig.ReadConfig(bytes.NewBuffer(kv.Value))
if err != nil {
log.Fatalln("Viper解析配置失败:", err)
}
defaultConfig = hotconfig
}
err = w.Run(consulAddress)
if err != nil {
log.Fatalln("监听consul错误:", err)
}
}
func GetConfig() *viper.Viper {
if defaultConfig == nil {
defaultConfig = initConfig()
}
return defaultConfig
}
func main() {
ReadOne()
go func() {
for {
color := GetConfig().GetString("config.color")
fmt.Println("color===", color)
time.Sleep(time.Second * 10)
}
}()
select {}
}
func ReadOne() {
runtimeConfig := viper.New()
runtimeConfig.AddRemoteProvider("consul", "http://192.168.2.xxx:8500", "config/local")
runtimeConfig.SetConfigType("yaml")
err := runtimeConfig.ReadRemoteConfig()
if err != nil {
log.Fatalln("viper read:", err)
}
err = runtimeConfig.WatchRemoteConfigOnChannel()
if err != nil {
log.Fatalln("viper watch err:", err)
}
go func() {
for {
price := runtimeConfig.GetString("config.price")
fmt.Println("price:", price)
time.Sleep(time.Second * 10)
}
}()
}
执行打印出来可以看到,成功获取到consul中配置的数据。