main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package main
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "github.com/go-kit/kit/sd/etcdv3"
  7. "github.com/spf13/viper"
  8. "time"
  9. )
  10. type (
  11. Config struct {
  12. App App `mapstructure:"app"`
  13. Mysql Mysql `mapstructure:"mysql"`
  14. Redis Redis `mapstructure:"redis"`
  15. Etcd Etcd `mapstructure:"etcd"`
  16. }
  17. App struct {
  18. ServerName string `mapstructure:"name"`
  19. Version string `mapstructure:"version"`
  20. LogLevel string `mapstructure:"log_level"`
  21. LogFormat string `mapstructure:"log_format"`
  22. Addr string `mapstructure:"addr"`
  23. Env string `mapstructure:"env"`
  24. RouterPrefix string `mapstructure:"router_prefix"`
  25. SignatureAuth bool `mapstructure:"signature_auth"`
  26. DefaultKeyPassword string `mapstructure:"default_key_password"`
  27. PrometheusAddr string `mapstructure:"prometheus_addr"`
  28. }
  29. Mysql struct {
  30. Host string `mapstructure:"host"`
  31. Port int `mapstructure:"port"`
  32. DB string `mapstructure:"db"`
  33. Username string `mapstructure:"username"`
  34. Password string `mapstructure:"password"`
  35. }
  36. Redis struct {
  37. Host string `mapstructure:"host"`
  38. Password string `mapstructure:"password"`
  39. DB int64 `mapstructure:"db"`
  40. }
  41. Etcd struct {
  42. Host string `mapstructure:"host"`
  43. Prefix string `mapstructure:"prefix"`
  44. Username string `mapstructure:"username"`
  45. Password string `mapstructure:"password"`
  46. }
  47. )
  48. func InitEtcdClient(a string,b string,c string) etcdv3.Client{
  49. //etcd连接参数
  50. option := etcdv3.ClientOptions{
  51. DialTimeout: time.Second * 3,
  52. DialKeepAlive: time.Second * 3,
  53. Username: b,
  54. Password: c,
  55. }
  56. //创建连接
  57. client, err := etcdv3.NewClient(context.Background(), []string{a}, option)
  58. if err != nil {
  59. panic(err)
  60. }
  61. return client
  62. }
  63. func readConfig(data []byte) (*Config, error) {
  64. v := viper.New()
  65. v.SetConfigType("toml")
  66. reader := bytes.NewReader(data)
  67. err := v.ReadConfig(reader)
  68. if err != nil {
  69. return nil, err
  70. }
  71. cfg := &Config{}
  72. if err := v.Unmarshal(cfg); err != nil {
  73. return nil, err
  74. }
  75. return cfg, nil
  76. }
  77. func main(){
  78. conn := InitEtcdClient("192.168.150.41:2379", "root", "JmZMbqQ1nc")
  79. resp, err := conn.GetEntries("/openapi/config")
  80. if err != nil {
  81. fmt.Println("Error: Get Config Data Error: ", err)
  82. return
  83. }
  84. fmt.Println([]byte(resp[0]))
  85. fmt.Println(resp[0])
  86. resp2, err := conn.GetEntries("/test/config")
  87. if err != nil {
  88. fmt.Println("Error: Get Config Data Error: ", err)
  89. return
  90. }
  91. fmt.Println([]byte(resp2[0]))
  92. fmt.Println(resp2[0])
  93. config, err := readConfig([]byte(resp2[0]))
  94. if err != nil {
  95. fmt.Println("Error: Get Config Data Error: ", err)
  96. return
  97. }
  98. fmt.Println(config)
  99. }