You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/01/08 02:29:18 UTC

[GitHub] [incubator-shardingsphere] sunbufu commented on a change in pull request #3887: Feature 3185 add nacos center

sunbufu commented on a change in pull request #3887: Feature 3185 add nacos center
URL: https://github.com/apache/incubator-shardingsphere/pull/3887#discussion_r364040884
 
 

 ##########
 File path: sharding-orchestration/sharding-orchestration-center/sharding-orchestration-center-nacos/src/main/java/org/apache/shardingsphere/orchestration/center/instance/NacosInstance.java
 ##########
 @@ -0,0 +1,165 @@
+/*
+ * 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.shardingsphere.orchestration.center.instance;
+
+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 java.util.List;
+import java.util.Properties;
+import java.util.concurrent.Executor;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.orchestration.center.api.ConfigCenter;
+import org.apache.shardingsphere.orchestration.center.configuration.InstanceConfiguration;
+import org.apache.shardingsphere.orchestration.center.listener.DataChangedEvent;
+import org.apache.shardingsphere.orchestration.center.listener.DataChangedEventListener;
+
+/**
+ * The nacos instance for ConfigCenter.
+ *
+ * @author huangjian
+ * @author sunbufu
+ */
+@Slf4j
+public class NacosInstance implements ConfigCenter {
+    
+    private final String defaultGroup = "SHARDING_SPHERE_DEFAULT_GROUP";
+    
+    private ConfigService configService;
+    
+    @Getter
+    @Setter
+    private Properties properties = new Properties();
+    
+    /**
+     * Initialize nacos instance.
+     *
+     * @param config config center configuration
+     */
+    @Override
+    public void init(final InstanceConfiguration config) {
+        try {
+            Properties properties = new Properties();
+            properties.put("serverAddr", config.getServerLists());
+            properties.put("namespace", null == config.getNamespace() ? "" : config.getNamespace());
+            configService = NacosFactory.createConfigService(properties);
+        } catch (final NacosException ex) {
+            log.debug("exception for: {}", ex.toString());
+        }
+    }
+    
+    /**
+     * Get data from nacos instance.
+     *
+     * @param key key of data
+     * @return value of data
+     */
+    @Override
+    public String get(final String key) {
+        return getDirectly(key);
+    }
+    
+    private String getDirectly(final String key) {
+        try {
+            String dataId = key.replace("/", ".");
+            String group = properties.getProperty("group", defaultGroup);
+            long timeoutMs = Long.parseLong(properties.getProperty("timeout", "3000"));
+            return configService.getConfig(dataId, group, timeoutMs);
+        } catch (final NacosException ex) {
+            log.debug("exception for: {}", ex.toString());
+            return null;
+        }
+    }
+    
+    /**
+     * Get node's sub-nodes list.
+     *
+     * @param key key of data
+     * @return sub-nodes name list
+     */
+    @Override
+    public List<String> getChildrenKeys(final String key) {
 
 Review comment:
   Yes, I think here need @dongzl 's new solution.  😊 But now, I just move the old nacos module in here.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services