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

dataID 是指定的 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)