golangconsultools的一个操作consul键值的工具,支持结构体整体打包查询及更新。使用安装goget-ugithub.com/xxjwxc/consult@master新建一个连接import("github.com/xxjwxc/consult/consulkv")conf:=consulkv.NewConfig()orWithOptionsconf:=consulkv.NewConfig(consulkv.WithPrefix(prefix),//consulkv前缀consulkv.WithAddress(address),//consul地址consulkv.WithAuth(username,password),//cosul用户密码consulkv.WithToken(token),//cousltokenconsulkv.WithLoger(loger),//loger)Initiferr:=conf.Init();err!=nil{returnerr}Putiferr:=conf.Put(key,value);err!=nil{returnerr}Deleteiferr:=conf.Delete(key);err!=nil{returnerr}Get//scaniferr:=conf.Get(key).Scan(x);err!=nil{returnerr}//getfloatfloat:=conf.Get(key).Float()//getfloatwithdefaultfloat:=conf.Get(key).Float(defaultFloat)//getinti:=conf.Get(key).Int()//getintwithdefaulti:=conf.Get(key).Int(defaultInt)监听conf.Watch(path,func(r*Result){r.Scan(x)})停止监听//stopsinglewatcherconf.StopWatch(path)//stopmultiplewatcherconf.StopWatch(path1,path2)//stopallwatcherconf.StopWatch()通过tag自动获取/自动更新定义变量时添加consul:""标签进行自动注册及获取import("github.com/xxjwxc/consult")typeInfostruct{Portstring`yaml:"port"consul:"port"`//端口号}varinfoInfoconsult.AutoLoadConfig(conf,&info)//自动加载consult.AutoSetConfig(conf,&info,false)//执行一次自动更新完整例子import("fmt""testing""github.com/xxjwxc/consult/consulkv""github.com/xxjwxc/consult")typeConfigstruct{MySQLInfoMysqlDbInfo`yaml:"mysql_info"consul:"mysql_info"`Portstring`yaml:"port"consul:"port"`//端口号}//MysqlDbInfomysqldatabaseinformation.mysql数据库信息typeMysqlDbInfostruct{Hoststring`validate:"required"consul:"host"`//Host.地址Portint`validate:"required"consul:"port"`//Port端口号Usernamestring`validate:"required"consul:"username"`//Username用户名Passwordstring`consul:"password"`//Password密码Databasestring`validate:"required"consul:"database"`//Database数据库名Typeint//数据库类型:0:mysql,1:sqlite,2:mssql}funcmain(){conf:=consulkv.NewConfig(consulkv.WithPrefix("service/servername"),//consulkvprefixconsulkv.WithAddress("192.155.1.150:8500"),//consuladdress)iferr:=conf.Init();err!=nil{mylog.Error(err)return}varconfigConfigconsult.AutoLoadConfig(conf,&config)//自动加载fmt.Println(config)consult.AutoSetConfig(conf,&config,false)//执行一次更新fmt.Println(config)}声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态
评论