consul kv 自动配置工具

本文介绍了一个名为consult的库,它封装了Consul API,提供了KV操作、Watcher功能,还支持自动注册与配置更新。通过实例展示了如何安装、配置并利用consult进行key-value存储、监听和自动获取/更新配置的过程。
摘要由CSDN通过智能技术生成

consult

前言

  • consult 是对consul的二次封装,方便用户使用,
  • consult 提供一系列函数操作consul
  • consult 支持watcher
  • consult 支持tag标签自动获取/自动更新
  • key/value kv 操作

使用

安装

go get -u github.com/xxjwxc/consult@master

新建一个连接

import (
	"github.com/xxjwxc/consult/consulkv"
)

conf := consulkv.NewConfig()

or

With Options

conf := consulkv.NewConfig(
    consulkv.WithPrefix(prefix),             // consul kv 前缀
    consulkv.WithAddress(address),           // consul 地址
    consulkv.WithAuth(username, password),   // cosul 用户密码
    consulkv.WithToken(token),               // cousl token
    consulkv.WithLoger(loger),               // loger
)

Init

if err := conf.Init();err !=nil {
    return err
}

Put

if err := conf.Put(key, value);err !=nil {
    return err
}

Delete

if err := conf.Delete(key);err !=nil {
    return err
}

Get

// scan
if err := conf.Get(key).Scan(x);err !=nil {
    return err
}

// get float
float := conf.Get(key).Float()

// get float with default
float := conf.Get(key).Float(defaultFloat)

// get int
i := conf.Get(key).Int()

// get int with default
i := conf.Get(key).Int(defaultInt)

// get uint
uInt := conf.Get(key).Uint()

// get uint with default
uInt := conf.Get(key).Uint(defaultUint)

// get bool
b := conf.Get(key).Bool()

// get bool with default
b := conf.Get(key).Bool(defaultBool)

// get []byte
bytes := conf.Get(key).Bytes()

// get uint with default
bytes := conf.Get(key).bytes(defaultBytes)

// get string
str := conf.Get(key).String()

// get string with default
str := conf.Get(key).String(defaultStr)

// get time
t := conf.Get(key).Time()

// get time with default
t := conf.Get(key).Time(defaultTime)

// get nested key values
conf.Get(key).Get(nextKey1).Get(nextKey2).String()

监听

conf.Watch(path, func(r *Result){
    r.Scan(x)
})

停止监听

// stop single watcher
conf.StopWatch(path)

// stop multiple watcher
conf.StopWatch(path1, path2)

// stop all watcher
conf.StopWatch()

通过tag自动获取/自动更新

  • 定义变量时添加consul:""标签进行自动注册及获取
import (
	"github.com/xxjwxc/consult"
)

type Info struct {
    Port  string  `yaml:"port" consul:"port"` // 端口号
}

var info Info
consult.AutoLoadConfig(conf, &info) //  自动加载

consult.AutoSetConfig(conf, &info, false) // 执行一次自动更新

完整例子


import (
	"fmt"
	"testing"

	"github.com/xxjwxc/consult/consulkv"
    "github.com/xxjwxc/consult"
)

type Config struct {
	MySQLInfo    MysqlDbInfo `yaml:"mysql_info" consul:"mysql_info"`
	Port         string      `yaml:"port" consul:"port"`                   // 端口号
}

// MysqlDbInfo mysql database information. mysql 数据库信息
type MysqlDbInfo struct {
	Host     string `validate:"required" consul:"host"`     // Host. 地址
	Port     int    `validate:"required" consul:"port"`     // Port 端口号
	Username string `validate:"required" consul:"username"` // Username 用户名
	Password string `consul:"password"`                     // Password 密码
	Database string `validate:"required" consul:"database"` // Database 数据库名
	Type     int    // 数据库类型: 0:mysql , 1:sqlite , 2:mssql
}

func main() {
	conf := consulkv.NewConfig(
		consulkv.WithPrefix("service/servername"),      // consul kv prefix
		consulkv.WithAddress("192.155.1.150:8500"), // consul address
	)
	if err := conf.Init(); err != nil {
		mylog.Error(err)
		return
	}

	var config Config
	consult.AutoLoadConfig(conf, &config) //  自动加载
	fmt.Println(config)

	consult.AutoSetConfig(conf, &config, false) // 执行一次更新
	fmt.Println(config)
}

更多:

xxjwxc
consult
consul

Consul是一个开源的分布式系统工具,主要用于服务发现、健康检查和 kv 存储,常用于构建高可用性和容错的微服务架构。在生产环境中,配置Consul需要考虑以下几个关键步骤: 1. **安装与部署**: - 根据你的基础设施(如单机、容器或Kubernetes)选择合适的安装方法。 - 分配角色:设置一个或多个Consul服务器节点(通常作为领导者),以及客户端节点。 2. **配置文件**: - 在`/etc/consul/consul-template.d/`目录下管理动态配置模板,例如使用`consul-template`进行配置文件的自动刷新。 - 定义服务发现和服务注册的策略。 3. **数据中心和节点**: - 创建数据中心,如`datacenter`,并为每个节点设置正确的数据中心标签。 - 启用跨数据中心复制(如有需要)。 4. **健康检查**: - 配置服务健康检查,包括HTTP/HTTPS、TCP连接等,并设置超时和重试规则。 5. **KV存储**: - 使用kv store来存储应用配置、元数据和其他临时数据。 - 可以设置ACL(访问控制列表)以保护敏感信息。 6. **安全**: - 开启TLS加密通信,启用证书验证。 - 如果需要,还可以设置防火墙规则和访问控制。 7. **监控与日志**: - 安装监控工具(如Prometheus、Grafana)来收集Consul的指标。 - 配置日志级别和输出目的地。 8. **故障转移**: - 如果集群规模较大,配置多主复制以增强高可用性。 相关问题: 1. 如何在Consul中创建和管理服务? 2. 如何设置Consul的健康检查模板? 3. Consul如何实现服务发现与负载均衡? 4. 如何在Consul中处理节点故障和恢复?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值