config center

Go-zero will soon support the configuration center function in the new version (v1.7.1). This article introduces the simple use of the configuration center in advance, and introduces the use precautions and characters. code https://github.com/zeromicro/go-zero/pull/3035.

Demo

  1. package main
  2. import (
  3. "github.com/zeromicro/go-zero/core/configcenter"
  4. "github.com/zeromicro/go-zero/core/configcenter/subscriber"
  5. "github.com/zeromicro/go-zero/core/discov"
  6. )
  7. // configuration structure definition
  8. type TestSt struct {
  9. Name string `json:"name"`
  10. }
  11. func main() {
  12. // 创建 etcd subscriber
  13. ss := subscriber.MustNewEtcdSubscriber(subscriber.EtcdConf{
  14. Hosts: []string{"localhost:2379"}, // ETCD address
  15. Key: "test1", // Configuration key
  16. })
  17. // Create configurator
  18. cc := configurator.MustNewConfigCenter[TestSt](configurator.Config{
  19. Type: "json", // Configuration value type: json, yaml, toml
  20. }, ss)
  21. // Get configuration
  22. // Note: If the configuration changes, the result of the call will always get the latest configuration
  23. v, err := cc.GetConfig()
  24. if err != nil {
  25. panic(err)
  26. }
  27. println(v.Name)
  28. // If you want to listen for configuration changes, you can add listeners
  29. cc.AddListener(func() {
  30. v, err := cc.GetConfig()
  31. if err != nil {
  32. panic(err)
  33. }
  34. println(v.Name)
  35. })
  36. select {}
  37. }

Note

  1. configurator Supported template types: struct, string。

  2. If you want to use more configuration types([]struct,map,[]map…),You can use the string type to get the data and parse it yourself.

    1. func main() {
    2. // Create etcd subscriber
    3. ss := subscriber.MustNewEtcdSubscriber(subscriber.EtcdConf{
    4. Hosts: []string{"localhost:2379"}, // ETCD address
    5. Key: "test1", // Configuration key
    6. })
    7. // Create configurator
    8. cc := configurator.MustNewConfigCenter[string](configurator.Config{
    9. Type: "json", // Configuration value type: json, yaml, toml
    10. }, ss)
    11. // Get configuration
    12. // Note: If the configuration changes, the result of the call will always get the latest configuration
    13. v, err := cc.GetConfig()
    14. if err != nil {
    15. panic(err)
    16. }
    17. // For cc. GetConfig () results, customize the parsing method
    18. }
  3. If it is a struct template type, the parser of the configurator is the same as the static configuration method, and the config will be checked. For detailed rules, please refer to: 参数规则

  1. // configuration structure definition
  2. type TestSt struct {
  3. Name string `json:",optional"`
  4. age int `json:",default=20"`
  5. }

Features

  1. Configurator built-in snapshot data, can provide high performance capabilities.

  2. Configurator supports custom subscribers, you can customize extensions according to your own technology stack, go-zero supports etcd by default.

  1. package main
  2. import (
  3. "sync"
  4. "github.com/zeromicro/go-zero/core/configcenter"
  5. )
  6. type TestSt struct {
  7. Name string `json:"name"`
  8. }
  9. // Custom Subscriber
  10. type MySubscriber struct {
  11. listeners []func()
  12. lock sync.Mutex
  13. }
  14. // Implement Custom AddListeners
  15. func (m *MySubscriber) AddListener(listener func()) error {
  16. m.lock.Lock()
  17. m.listeners = append(m.listeners, listener)
  18. m.lock.Unlock()
  19. return nil
  20. }
  21. // Implement Custom Values
  22. func (m *MySubscriber) Value() (string, error) {
  23. return "", nil
  24. }
  25. func main() {
  26. mySubscriber := &MySubscriber{}
  27. cc := configurator.MustNewConfigCenter[TestSt](configurator.Config{
  28. Type: "json",
  29. }, mySubscriber) // Inject Custom Subscribers
  30. v, err := cc.GetConfig()
  31. if err != nil {
  32. panic(err)
  33. }
  34. println(len(v.Name))
  35. }