This is an automated email from the ASF dual-hosted git repository.

zhaoyunxing pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 915292c  Config update issue (#1592)
915292c is described below

commit 915292c53e5d98d0f2ec0c8d42f075eee43522fa
Author: sunrui1225 <[email protected]>
AuthorDate: Sun Dec 19 21:47:07 2021 +0800

    Config update issue (#1592)
    
    * config center auto update issue
    
    * split import block
    
    * Remove quotation mark
    
    * log use Infof instead of info
    
    * remove UpdateProperties to register lay
    
    * set UpdateProperties param registryConfig
    
    * use GetConfigResolver method to generate koan
    
    * imports format root_config.go and test
    
    * imports format use imports-formatter -path .
    
    Co-authored-by: ruishansun <[email protected]>
---
 config/config_center_config.go       |  2 ++
 config/registry_config.go            |  9 +++++++
 config/root_config.go                | 23 +++++++++++++++++
 config/root_config_test.go           | 50 ++++++++++++++++++++++++++++++++++++
 config/testdata/root_config_test.yml | 17 ++++++++++++
 5 files changed, 101 insertions(+)

diff --git a/config/config_center_config.go b/config/config_center_config.go
index 9ad399e..10dbce3 100644
--- a/config/config_center_config.go
+++ b/config/config_center_config.go
@@ -154,6 +154,8 @@ func startConfigCenter(rc *RootConfig) error {
        if err = koan.UnmarshalWithConf(rc.Prefix(), rc, 
koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
                return err
        }
+
+       dynamicConfig.AddListener(cc.DataId, rc, 
config_center.WithGroup(cc.Group))
        return nil
 }
 
diff --git a/config/registry_config.go b/config/registry_config.go
index af4eadd..506dc79 100644
--- a/config/registry_config.go
+++ b/config/registry_config.go
@@ -397,3 +397,12 @@ func (rcb *RegistryConfigBuilder) Build() *RegistryConfig {
        }
        return rcb.registryConfig
 }
+
+// UpdateProperties update registry
+func (c *RegistryConfig) UpdateProperties(updateRegistryConfig 
*RegistryConfig) {
+       // if nacos's registry timeout not equal local root config's registry 
timeout , update.
+       if updateRegistryConfig != nil && updateRegistryConfig.Timeout != 
c.Timeout {
+               c.Timeout = updateRegistryConfig.Timeout
+               logger.Infof("CenterConfig process update timeout, new 
value:%v", c.Timeout)
+       }
+}
diff --git a/config/root_config.go b/config/root_config.go
index ad3c33a..7450a32 100644
--- a/config/root_config.go
+++ b/config/root_config.go
@@ -25,6 +25,8 @@ import (
 import (
        hessian "github.com/apache/dubbo-go-hessian2"
 
+       "github.com/knadh/koanf"
+
        perrors "github.com/pkg/errors"
 
        "go.uber.org/atomic"
@@ -35,6 +37,7 @@ import (
        "dubbo.apache.org/dubbo-go/v3/common/constant"
        "dubbo.apache.org/dubbo-go/v3/common/extension"
        "dubbo.apache.org/dubbo-go/v3/common/logger"
+       "dubbo.apache.org/dubbo-go/v3/config_center"
        "dubbo.apache.org/dubbo-go/v3/metadata/service/exporter"
 )
 
@@ -379,3 +382,23 @@ func publishMapping(sc exporter.MetadataServiceExporter) 
error {
        }
        return nil
 }
+
+// Process receive changing listener's event, dynamic update config
+func (rc *RootConfig) Process(event *config_center.ConfigChangeEvent) {
+       logger.Infof("CenterConfig process event:\n%+v", event)
+       config := NewLoaderConf(WithBytes([]byte(event.Value.(string))))
+       koan := GetConfigResolver(config)
+
+       updateRootConfig := &RootConfig{}
+       if err := koan.UnmarshalWithConf(rc.Prefix(),
+               updateRootConfig, koanf.UnmarshalConf{Tag: "yaml"}); err != nil 
{
+               logger.Errorf("CenterConfig process unmarshalConf failed, got 
error %#v", err)
+               return
+       }
+
+       // update register
+       for registerId, updateRegister := range updateRootConfig.Registries {
+               register := rc.Registries[registerId]
+               register.UpdateProperties(updateRegister)
+       }
+}
diff --git a/config/root_config_test.go b/config/root_config_test.go
new file mode 100644
index 0000000..bf38868
--- /dev/null
+++ b/config/root_config_test.go
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package config
+
+import (
+       "testing"
+)
+
+import (
+       "github.com/stretchr/testify/assert"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/yaml"
+       "dubbo.apache.org/dubbo-go/v3/config_center"
+)
+
+func TestGoConfigProcess(t *testing.T) {
+       rc := &RootConfigBuilder{rootConfig: newEmptyRootConfig()}
+       r := &RegistryConfig{Protocol: "zookeeper", Timeout: "10s", Address: 
"127.0.0.1:2181"}
+       rc.AddRegistry("demoZK", r)
+
+       // test koan.UnmarshalWithConf error
+       b := "dubbo:\n  registries:\n    demoZK:\n      protocol: zookeeper\n   
   timeout: 11s\n      address: 127.0.0.1:2181\n      simplified: abc123"
+       c2 := &config_center.ConfigChangeEvent{Key: "test", Value: b}
+       rc.rootConfig.Process(c2)
+       assert.Equal(t, rc.rootConfig.Registries["demoZK"].Timeout, "10s")
+
+       // test update registry time out
+       bs, _ := yaml.LoadYMLConfig("./testdata/root_config_test.yml")
+       c := &config_center.ConfigChangeEvent{Key: "test", Value: string(bs)}
+       rc.rootConfig.Process(c)
+       assert.Equal(t, rc.rootConfig.Registries["demoZK"].Timeout, "11s")
+
+}
diff --git a/config/testdata/root_config_test.yml 
b/config/testdata/root_config_test.yml
new file mode 100644
index 0000000..285b79c
--- /dev/null
+++ b/config/testdata/root_config_test.yml
@@ -0,0 +1,17 @@
+dubbo:
+  registries:
+    demoZK:
+      protocol: zookeeper
+      timeout: 11s
+      address: 127.0.0.1:2181
+  protocols:
+    triple:
+      name: tri
+      port: 20000
+  provider:
+    registry-ids:
+      - demoZK
+    services:
+      GreeterProvider:
+        protocol-ids: triple
+        interface: com.apache.dubbo.sample.basic.IGreeter # must be compatible 
with grpc or dubbo-java
\ No newline at end of file

Reply via email to