160731liupf commented on a change in pull request #1405: 【SCB-1615】Extended 
dynamic configuration support for Nacos
URL: 
https://github.com/apache/servicecomb-java-chassis/pull/1405#discussion_r350472856
 
 

 ##########
 File path: 
dynamic-config/config-nacos/src/main/java/org/apache/servicecomb/config/client/NacosClient.java
 ##########
 @@ -0,0 +1,150 @@
+/*
+ * 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.servicecomb.config.client;
+
+import static org.apache.servicecomb.config.client.ConfigurationAction.CREATE;
+import static org.apache.servicecomb.config.client.ConfigurationAction.DELETE;
+import static org.apache.servicecomb.config.client.ConfigurationAction.SET;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.*;
+
+import com.alibaba.nacos.api.NacosFactory;
+import com.alibaba.nacos.api.config.ConfigService;
+import com.alibaba.nacos.api.config.listener.Listener;
+import 
org.apache.servicecomb.config.archaius.sources.NacosConfigurationSourceImpl.UpdateHandler;
+import org.apache.servicecomb.foundation.common.utils.JsonUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.client.RestTemplate;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+
+public class NacosClient {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(NacosClient.class);
+
+  private static final NacosConfig NACOS_CONFIG = NacosConfig.INSTANCE;
+
+  private static final Map<String, Object> originalConfigMap = new 
ConcurrentHashMap<>();
+
+  private final String serverAddr = NACOS_CONFIG.getServerAddr();
+
+  private final String dataId = NACOS_CONFIG.getDataId();
+
+  private final String group = NACOS_CONFIG.getGroup();
+
+  private final UpdateHandler updateHandler;
+
+  public NacosClient(UpdateHandler updateHandler) {
+    this.updateHandler = updateHandler;
+  }
+
+  public void refreshNacosConfig() {
+        new ConfigRefresh(serverAddr, dataId, group).refreshConfig();
+  }
+
+  class ConfigRefresh {
+      private final String serverAddr;
+      private final String dataId;
+      private final String group;
+      ConfigRefresh(String serverAddr,String dataId,String group) {
+          this.serverAddr = serverAddr;
+          this.dataId = dataId;
+          this.group = group;
+      }
+
+    @SuppressWarnings("unchecked")
+    void refreshConfig() {
+          Properties properties = new Properties();
+          properties.put("serverAddr",serverAddr);
+          properties.put("dataId",dataId);
+          properties.put("group",group);
+      try{
+        ConfigService configService = 
NacosFactory.createConfigService(properties);
+        String content = configService.getConfig(dataId, group, 5000);
+        Map<String, Object> body = JsonUtils.OBJ_MAPPER.readValue(content,
+              new TypeReference<Map<String, Object>>() {
+              });
+        refreshConfigItems(body);
+        configService.addListener(dataId, group, new Listener() {
+          @Override
+          public void receiveConfigInfo(String configInfo) {
+              LOGGER.info("receive from nacos:"+configInfo);
+              try {
+                  Map<String, Object> body = 
JsonUtils.OBJ_MAPPER.readValue(configInfo,
+                          new TypeReference<Map<String, Object>>() {
+                          });
+                  refreshConfigItems(body);
+              } catch (IOException e) {
+                  LOGGER.error("JsonObject parse config center response error: 
", e);
+              }
+          }
+          @Override
+          public Executor getExecutor() {
+            return null;
+          }
+        });
+      }catch (Exception e){
+          LOGGER.error("Receive nacos config error: ", e);
+      }
 
 Review comment:
   可以用中文解释一下吗?我不明白要怎么修改比较好

----------------------------------------------------------------
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