1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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)
}
|