Dubbogo 3.0 配置中心與配置監控

1. 配置中心概念

配置中心是指在分散式場景下,最新的框架配置檔和應用程式不必綁定在一起。您可以指定配置中心的資訊,例如配置中心的類型和地址,框架啟動時會從配置中心拉取對應的配置。

2. 配置中心的配置

參考儲存庫:dubbo-go-samples/configcenter

dubbogo.yml

dubbo:
  config-center:
    protocol: nacos
    address: 127.0.0.1:8848
    data-id: dubbo-go-samples-configcenter-nacos-server
    namespace: myNamespaceID # optional configuration nacos namespace ID, the default is public
    group: mygroup # Optional configuration nacos group, default is DEFAULT_GROUP

在配置中心 Nacos 中

群組預設為 dubbo

資料 ID 為指定的 ID:dubbo-go-samples-configcenter-nacos-server

撰寫如下框架配置即可正常啟動。

dubbo:
  registries:
    demoZK:
      protocol: zookeeper
      timeout: 3s
      address: 127.0.0.1:2181
  protocols:
    triple:
      name: tri
      port: 20000
  provider:
    services:
      GreeterProvider:
        interface: com.apache.dubbo.sample.basic.IGreeter

3. Dubbogo 動態配置 API

Config API 是 dubbogo 3.0 用於操作配置結構的 API。您可以使用框架提供的 Config API 初始化配置結構、取得元件實例並使用它們。範例如下,包含動態配置實例的初始化、發布配置、讀取配置和訂閱配置操作。

const configCenterNacosServerConfig = `# set in config center, group is 'dubbo', dataid is 'dubbo-go-samples-configcenter-nacos-server', namespace is default 'public'
dubbo:
  registries:
    demoZK:
      protocol: zookeeper
      address: 127.0.0.1:2181
  protocols:
    triple:
      name: tri
      port: 20000
  provider:
    services:
      GreeterProvider:
        interface: com.apache.dubbo.sample.basic.IGreeter # must be compatible with grpc or dubbo-java`

type GreeterProvider struct {
  api. GreeterProviderBase
}

func (s *GreeterProvider) SayHello(ctx context.Context, in *api.HelloRequest) (*api.User, error) {
  logger.Infof("Dubbo3 GreeterProvider get user name = %s\n", in.Name)
  return &api.User{Name: "Hello " + in.Name, Id: "12345", Age: 21}, nil
}

// There is no need to export DUBBO_GO_CONFIG_PATH, as you are using config api to set config
func main() {
// Get dynamic configuration instance dynamicConfig
  dynamicConfig, err := config.NewConfigCenterConfigBuilder().
    SetProtocol("nacos").
    SetAddress("127.0.0.1:8848").
    SetGroup("dubbo").
    Build(). GetDynamicConfiguration()
  if err != nil {
    panic(err)
  }
  
  // Use the dynamicConfig structure to publish the configuration
  if err := dynamicConfig.PublishConfig("dubbo-go-samples-configcenter-nacos-server", "dubbo", configCenterNacosServerConfig); err != nil {
    panic(err)
  }
  
   // use dynamicConfig structure to read configuration
  data, err := dynamicConfig.GetRule("dubbo-go-samples-configcenter-nacos-server", config_center.WithGroup("dubbo"))
  if err != nil{
    panic(err)
  }
  logger.Infof("get config = %s", data)
  
  
  // Use the dynamicConfig structure to subscribe to configuration update events through a custom listener
  l := &listener{}
  dynamicConfig.AddListener("dubbo-go-samples-configcenter-nacos-server", l)
  
  time. Sleep(time. Second * 10)
  
  config. SetProviderService(&GreeterProvider{})

  // Start the framework in the form of API
  rootConfig := config. NewRootConfigBuilder().
    SetConfigCenter(config. NewConfigCenterConfigBuilder().
      SetProtocol("nacos").SetAddress("127.0.0.1:8848"). // Set the configuration center according to the configuration structure
      SetDataID("dubbo-go-samples-configcenter-nacos-server"). // Set configuration ID
      SetGroup("dubbo").
      Build()).
  build()

  if err := rootConfig.Init(); err != nil { // framework starts
    panic(err)
  }
  select {}
}

type listener struct {

}

func (l listener) Process(event *config_center. ConfigChangeEvent) {
  logger.Infof("listener get config = %s", event.Value)
}

當然,以 API 形式啟動框架時,可以直接以 API 形式啟動框架。

4. Dubbogo 配置熱更新

//todo

開發中


最後修改日期:2024 年 1 月 17 日:修復損壞的連結 (6651e217e73)