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/07 04:33:44 UTC

[GitHub] [incubator-shardingsphere] sunbufu opened a new pull request #3887: Feature 3185 add nacos center

sunbufu opened a new pull request #3887: Feature 3185 add nacos center
URL: https://github.com/apache/incubator-shardingsphere/pull/3887
 
 
   Fixes #3185.
   
   Changes proposed in this pull request:
   - add nacos center for new orchestration.
   

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

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

Posted by GitBox <gi...@apache.org>.
sunbufu commented on a change in pull request #3887: Feature 3185 add nacos center
URL: https://github.com/apache/incubator-shardingsphere/pull/3887#discussion_r364041736
 
 

 ##########
 File path: sharding-orchestration/sharding-orchestration-center/sharding-orchestration-center-nacos/src/main/resources/META-INF/services/org.apache.shardingsphere.orchestration.center.api.ConfigCenter
 ##########
 @@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.orchestration.center.instance.NacosInstance
 
 Review comment:
   This module is a sub module of `sharding-orchestration-center`, and different instance module only have itself instance 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

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

Posted by GitBox <gi...@apache.org>.
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

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

Posted by GitBox <gi...@apache.org>.
wgy8283335 commented on a change in pull request #3887: Feature 3185 add nacos center
URL: https://github.com/apache/incubator-shardingsphere/pull/3887#discussion_r363711927
 
 

 ##########
 File path: sharding-orchestration/sharding-orchestration-center/sharding-orchestration-center-nacos/src/test/java/org/apache/shardingsphere/orchestration/center/instance/NacosInstanceTest.java
 ##########
 @@ -0,0 +1,112 @@
+/*
+ * 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.config.ConfigService;
+import com.alibaba.nacos.api.config.listener.Listener;
+import java.lang.reflect.Field;
+import java.util.Properties;
+import lombok.SneakyThrows;
+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;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class NacosInstanceTest {
+    
+    private static ConfigCenter nacosConfigCenter = new NacosInstance();
+    
+    private ConfigService configService = mock(ConfigService.class);
+    
+    private String group = "SHARDING_SPHERE_DEFAULT_GROUP";
+    
+    @Before
+    public void init() {
+        Properties properties = new Properties();
+        properties.setProperty("group", group);
+        properties.setProperty("timeout", "3000");
+        InstanceConfiguration configuration = new InstanceConfiguration(nacosConfigCenter.getType(), properties);
+        configuration.setServerLists("x.x.x.:8848");
+        nacosConfigCenter.init(configuration);
+        setConfigService(configService);
+    }
+    
+    @SneakyThrows
+    private void setConfigService(final ConfigService configService) {
+        Field configServiceField = NacosInstance.class.getDeclaredField("configService");
+        configServiceField.setAccessible(true);
+        configServiceField.set(nacosConfigCenter, configService);
+    }
+    
+    @Test
+    @SneakyThrows
+    public void assertPersist() {
+        String value = "value";
+        nacosConfigCenter.persist("sharding/test", value);
+        verify(configService).publishConfig("sharding.test", group, value);
+    }
+    
+    @Test
+    @SneakyThrows
+    public void assertGet() {
+        String value = "value";
+        when(configService.getConfig(eq("sharding.test"), eq(group), anyLong())).thenReturn(value);
+        Assert.assertEquals(value, nacosConfigCenter.get("sharding/test"));
+    }
+    
+    @Test
+    @SneakyThrows
+    public void assertWatch() {
+        final String expectValue = "expectValue";
+        final String[] actualValue = {null};
+        doAnswer(getListenerAnswer(expectValue)).when(configService).addListener(anyString(), anyString(), any(Listener.class));
+        DataChangedEventListener listener = new DataChangedEventListener() {
+            @Override
+            public void onChange(final DataChangedEvent dataChangedEvent) {
+                actualValue[0] = dataChangedEvent.getValue();
+            }
+        };
+        nacosConfigCenter.watch("sharding/test", listener);
+        Assert.assertEquals(expectValue, actualValue[0]);
+    }
+    
+    private Answer getListenerAnswer(final String expectValue) {
+        return new Answer() {
+            @Override
+            public Object answer(final InvocationOnMock invocation) {
+                Listener listener = invocation.getArgument(2);
+                listener.receiveConfigInfo(expectValue);
+                return null;
 
 Review comment:
   Why null?

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

[GitHub] [incubator-shardingsphere] dongzl merged pull request #3887: Feature 3185 add nacos center

Posted by GitBox <gi...@apache.org>.
dongzl merged pull request #3887: Feature 3185 add nacos center
URL: https://github.com/apache/incubator-shardingsphere/pull/3887
 
 
   

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

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

Posted by GitBox <gi...@apache.org>.
wgy8283335 commented on a change in pull request #3887: Feature 3185 add nacos center
URL: https://github.com/apache/incubator-shardingsphere/pull/3887#discussion_r363711641
 
 

 ##########
 File path: sharding-orchestration/sharding-orchestration-center/sharding-orchestration-center-nacos/src/test/java/org/apache/shardingsphere/orchestration/center/instance/NacosInstanceTest.java
 ##########
 @@ -0,0 +1,112 @@
+/*
+ * 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.config.ConfigService;
+import com.alibaba.nacos.api.config.listener.Listener;
+import java.lang.reflect.Field;
+import java.util.Properties;
+import lombok.SneakyThrows;
+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;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class NacosInstanceTest {
+    
+    private static ConfigCenter nacosConfigCenter = new NacosInstance();
+    
+    private ConfigService configService = mock(ConfigService.class);
+    
+    private String group = "SHARDING_SPHERE_DEFAULT_GROUP";
+    
+    @Before
+    public void init() {
+        Properties properties = new Properties();
+        properties.setProperty("group", group);
+        properties.setProperty("timeout", "3000");
+        InstanceConfiguration configuration = new InstanceConfiguration(nacosConfigCenter.getType(), properties);
+        configuration.setServerLists("x.x.x.:8848");
 
 Review comment:
   “x.x.x.” or "192.168.1.2"?

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

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

Posted by GitBox <gi...@apache.org>.
wgy8283335 commented on a change in pull request #3887: Feature 3185 add nacos center
URL: https://github.com/apache/incubator-shardingsphere/pull/3887#discussion_r363706760
 
 

 ##########
 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:
   If return "null", whether there will be problems.

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

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

Posted by GitBox <gi...@apache.org>.
wgy8283335 commented on a change in pull request #3887: Feature 3185 add nacos center
URL: https://github.com/apache/incubator-shardingsphere/pull/3887#discussion_r363707170
 
 

 ##########
 File path: sharding-orchestration/sharding-orchestration-center/sharding-orchestration-center-nacos/src/main/resources/META-INF/services/org.apache.shardingsphere.orchestration.center.api.ConfigCenter
 ##########
 @@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.orchestration.center.instance.NacosInstance
 
 Review comment:
   Where is follow items:
   org.apache.shardingsphere.orchestration.center.instance.CuratorZookeeperInstance
   org.apache.shardingsphere.orchestration.center.instance.ApolloInstance

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