kezhenxu94 commented on a change in pull request #2817: Provide Nacos dynamic 
configuration center implementation. fixes #2816
URL: https://github.com/apache/skywalking/pull/2817#discussion_r291199869
 
 

 ##########
 File path: 
oap-server/server-configuration/configuration-nacos/src/main/java/org/apache/skywalking/oap/server/configuration/nacos/NacosConfigWatcherRegister.java
 ##########
 @@ -0,0 +1,95 @@
+/*
+ * 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 org.apache.skywalking.oap.server.configuration.nacos;
+
+import com.alibaba.nacos.api.NacosFactory;
+import com.alibaba.nacos.api.config.ConfigService;
+import com.alibaba.nacos.api.config.listener.Listener;
+import com.alibaba.nacos.api.exception.NacosException;
+import org.apache.skywalking.oap.server.configuration.api.ConfigTable;
+import 
org.apache.skywalking.oap.server.configuration.api.ConfigWatcherRegister;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executor;
+
+/**
+ * @author kezhenxu94
+ */
+public class NacosConfigWatcherRegister extends ConfigWatcherRegister {
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(NacosConfigWatcherRegister.class);
+
+    private final NacosServerSettings settings;
+    private final ConfigService configService;
+    private final Map<String, ConfigTable.ConfigItem> configItemKeyedByName;
+
+    public NacosConfigWatcherRegister(NacosServerSettings settings) throws 
NacosException {
+        super(settings.getPeriod());
+
+        this.settings = settings;
+        this.configItemKeyedByName = new ConcurrentHashMap<>();
+
+        final int port = this.settings.getPort();
+        final String serverAddr = this.settings.getServerAddr();
+
+        final Properties properties = new Properties();
+        properties.put("serverAddr", serverAddr + ":" + port);
+        this.configService = NacosFactory.createConfigService(properties);
+
+        final String group = settings.getGroup();
+        for (final String dataId : settings.getDataIds()) {
+            this.configService.addListener(dataId, group, new Listener() {
+                @Override
+                public Executor getExecutor() {
+                    return null;
+                }
+
+                @Override
+                public void receiveConfigInfo(String configInfo) {
+                    onDataIdValueChanged(dataId, configInfo);
+                }
+            });
+        }
+    }
+
+    void onDataIdValueChanged(String dataId, String configInfo) {
+        if (LOGGER.isInfoEnabled()) {
+            LOGGER.info("Nacos config changed: {}: {}", dataId, configInfo);
+        }
+
+        final ConfigTable.ConfigItem configItem =
+            configItemKeyedByName.computeIfAbsent(dataId, name -> new 
ConfigTable.ConfigItem(name, null));
+
+        configItem.setValue(configInfo);
+    }
+
+    @Override
+    public ConfigTable readConfig() {
 
 Review comment:
   > I think you need to change this interface method, let's add config keys as 
parameter? Then we could avoid config keys in providet.
   > 
   > Could you send a new pull request about this first?
   
   Sure, will send another PR soon

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to