记一次升级 Viper、ETCD V3操作Toml

前一阵子碰到Go写的一项目,使用viper和ETCD进行Toml文件的存储与写入。在当我安装新版本的ETCD和升级Go依赖包之后出现了不兼容的问题。旧版viper为1.10版本,使用github.com/coreos/go-etcd v2.0.0+incompatible 作为请求包。看了源码之后发现新的版本中废弃掉了很多内容,所以既然升级了ETCD,那不如将逻辑重写了。
目前使用的包版本
github.com/spf13/viper v1.15.0
go.etcd.io/etcd/client/v3 v3.5.9

代码如下:

go 复制代码
package main

import (
	"bytes"
	"context"
	"crypto/tls"
	"crypto/x509"
	"fmt"
	"github.com/spf13/viper"
	"io/ioutil"
	"log"
	"os"
	"time"

	_ "github.com/spf13/viper/remote"
	clientv3 "go.etcd.io/etcd/client/v3"
)

type etcdClient struct {
	cli *clientv3.Client
}

func initClient(endpoint string) *etcdClient {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
    
    // 这里的证书只是释意,我没有配置证书下面跳过验证了
	cert, err := tls.LoadX509KeyPair("healthcheck-client.crt", "healthcheck-client.key")
	if err != nil {
		log.Fatalf("Failed to load client cert and key: %v", err)
	}

	caCert, err := ioutil.ReadFile("ca.crt")
	if err != nil {
		log.Fatalf("Failed to read ca certificate: %v", err)
	}
	caCertPool := x509.NewCertPool()
	caCertPool.AppendCertsFromPEM(caCert)

	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{endpoint},
		DialTimeout: 10 * time.Second,
		TLS: &tls.Config{
			Certificates:       []tls.Certificate{cert},
			RootCAs:            caCertPool,
			InsecureSkipVerify: false,
		},
	})
	if err != nil {
		log.Fatalf("Failed to connect to etcd: %v", err)
	}
	fmt.Println("init client...")
	return &etcdClient{
		cli: cli,
	}
}

func (cli *etcdClient) Put(key string, fileName string) {

	fmt.Println("write config to etcd...")
	data, err := os.ReadFile(fileName)
	if err != nil {
		log.Fatalf("Failed to read config file: %v", err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	_, err = cli.cli.Put(ctx, key, string(data))
	cancel()
	if err != nil {
		log.Fatalf("Failed to write config to etcd: %v", err)
	}
}

func (cli *etcdClient) Get(key string) {
	fmt.Println("get config from etcd...")

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	resp, err := cli.cli.Get(ctx, key)
	cancel()
	if err != nil {
		log.Fatalf("Failed to get config from etcd: %v", err)
	}

	viper.SetConfigType("toml")
	if len(resp.Kvs) > 0 {
		err = viper.ReadConfig(bytes.NewBuffer(resp.Kvs[0].Value))
		if err != nil {
			log.Fatalf("Failed to read configuration: %v", err)
		}
	} else {
		log.Fatalf("No config found in ETCD")
	}

	fmt.Println("读取的配置信息:", viper.Get("god_job.repo.k8s"))
}

func viperDemo(endpoint, key string) {
	v := viper.New()

	v.SetConfigType("toml")
	err := v.AddRemoteProvider("etcd3", endpoint, key)
	if err != nil {
		log.Fatalf("Failed to add remote provider: %v", err)
	}

	err = v.ReadRemoteConfig()
	fmt.Println("读取的配置信息:", v.Get("god_job.repo.k8s"))
}

func main() {
	fileName := "conf.dev.toml"
	keyString := "/dev/conf.dev.toml"
	endpoint := "http://127.0.0.1:23791"
	cli := initClient(endpoint)
	cli.Put(keyString, fileName)
	cli.Get(keyString)
	cli.cli.Close()

	viperDemo(endpoint, keyString)
}
注意点!

1. Viper包中是不支持配置证书的,旧版remote可以绕过HTTPS,(目前我没找到,有大佬可以在评论区告诉我),所以如果ETCD有证书的话建议先读出来然后使用Viper解析。
2. ETCD V2 版本的API存入的数据使用v3是读不出来的,也就是说v2存v2读,v3存v3读。

相关推荐
TDengine (老段)1 小时前
TDengine IDMP 功能点—数据接入
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
索西引擎1 小时前
MySQL 进阶:视图、存储过程、存储函数与触发器
数据库·mysql
bksczm3 小时前
InnoDB 通用表空间与临时表空间详解
大数据·数据库·mysql
靠沿3 小时前
MySQL数据库进阶——存储过程和触发器
java·数据库·mysql
码少女4 小时前
MySQL--库与表的增删改查基础
java·数据库·mysql
Eyfcom4 小时前
〖物流 SaaS 实战 02〗SaaS 租户的计费与订阅模型:从“一锤子买卖”到“按需订阅”
linux·服务器·数据库·快递系统·物流saas实战
打工的小王5 小时前
postgresql和mysql对比
数据库·mysql·postgresql
瑞克-桑切斯5 小时前
Kubernetes 1.28 保姆级新手学习教程
运维·学习·docker·云原生·容器·kubernetes
数据库急诊室_阿哲6 小时前
一次割接后的性能复核记录:基线、样本、回滚点
运维·数据库·dba
保利威视频SaaS小张7 小时前
企业直播接入笔记 12|企业内训直播怎么做签到和学时统计,防挂机这件事比想象中难
java·前端·数据库·笔记·视频云·polvy·polyv