You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/06/10 08:05:15 UTC
[incubator-shenyu] branch master updated: [type:feature] add nacos register for shenyu instance (#3499)
This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new c14526547 [type:feature] add nacos register for shenyu instance (#3499)
c14526547 is described below
commit c14526547778cc21b561efcf841af6525702b2f9
Author: Luke.Z <10...@users.noreply.github.com>
AuthorDate: Fri Jun 10 16:05:09 2022 +0800
[type:feature] add nacos register for shenyu instance (#3499)
* add nacos register for shenyu instance
* fix checkstyle
---
pom.xml | 2 +-
.../shenyu-register-instance/pom.xml | 1 +
.../shenyu-register-instance-core/pom.xml | 5 +
.../{ => shenyu-register-instance-nacos}/pom.xml | 29 +++---
.../nacos/NacosInstanceRegisterRepository.java | 106 +++++++++++++++++++++
...r.instance.api.ShenyuInstanceRegisterRepository | 17 ++++
.../nacos/NacosInstanceRegisterRepositoryTest.java | 100 +++++++++++++++++++
7 files changed, 246 insertions(+), 14 deletions(-)
diff --git a/pom.xml b/pom.xml
index 5f7ddbb17..29ff0a8a4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -86,7 +86,7 @@
<druid.version>1.0.29</druid.version>
<commons-lang3.version>3.3.2</commons-lang3.version>
<guava.version>30.1-jre</guava.version>
- <commons-collections4.version>4.1</commons-collections4.version>
+ <commons-collections4.version>4.4</commons-collections4.version>
<reactor-spring.version>1.0.1.RELEASE</reactor-spring.version>
<disruptor.version>3.4.0</disruptor.version>
<consul.api.version>1.4.5</consul.api.version>
diff --git a/shenyu-register-center/shenyu-register-instance/pom.xml b/shenyu-register-center/shenyu-register-instance/pom.xml
index b77f142b5..cf9fe5b49 100644
--- a/shenyu-register-center/shenyu-register-instance/pom.xml
+++ b/shenyu-register-center/shenyu-register-instance/pom.xml
@@ -32,6 +32,7 @@
<module>shenyu-register-instance-zookeeper</module>
<module>shenyu-register-instance-etcd</module>
<module>shenyu-register-instance-consul</module>
+ <module>shenyu-register-instance-nacos</module>
</modules>
</project>
\ No newline at end of file
diff --git a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-core/pom.xml b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-core/pom.xml
index 9864e71b7..9abd09e0a 100644
--- a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-core/pom.xml
+++ b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-core/pom.xml
@@ -46,6 +46,11 @@
<artifactId>shenyu-register-instance-consul</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-register-instance-nacos</artifactId>
+ <version>${project.version}</version>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git a/shenyu-register-center/shenyu-register-instance/pom.xml b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/pom.xml
similarity index 70%
copy from shenyu-register-center/shenyu-register-instance/pom.xml
copy to shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/pom.xml
index b77f142b5..cc9391a34 100644
--- a/shenyu-register-center/shenyu-register-instance/pom.xml
+++ b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/pom.xml
@@ -19,19 +19,22 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.shenyu</groupId>
- <artifactId>shenyu-register-center</artifactId>
+ <artifactId>shenyu-register-instance</artifactId>
<version>2.5.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <artifactId>shenyu-register-instance</artifactId>
- <packaging>pom</packaging>
-
- <modules>
- <module>shenyu-register-instance-api</module>
- <module>shenyu-register-instance-core</module>
- <module>shenyu-register-instance-zookeeper</module>
- <module>shenyu-register-instance-etcd</module>
- <module>shenyu-register-instance-consul</module>
- </modules>
-
-</project>
\ No newline at end of file
+ <artifactId>shenyu-register-instance-nacos</artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-register-instance-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.alibaba.nacos</groupId>
+ <artifactId>nacos-client</artifactId>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/src/main/java/org/apache/shenyu/register/instance/nacos/NacosInstanceRegisterRepository.java b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/src/main/java/org/apache/shenyu/register/instance/nacos/NacosInstanceRegisterRepository.java
new file mode 100644
index 000000000..4c4b34d90
--- /dev/null
+++ b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/src/main/java/org/apache/shenyu/register/instance/nacos/NacosInstanceRegisterRepository.java
@@ -0,0 +1,106 @@
+/*
+ * 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.shenyu.register.instance.nacos;
+
+import com.alibaba.nacos.api.PropertyKeyConst;
+import com.alibaba.nacos.api.exception.NacosException;
+import com.alibaba.nacos.api.naming.NamingFactory;
+import com.alibaba.nacos.api.naming.NamingService;
+import com.alibaba.nacos.api.naming.pojo.Instance;
+import org.apache.shenyu.common.config.ShenyuConfig.InstanceConfig;
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.exception.ShenyuException;
+import org.apache.shenyu.register.common.dto.InstanceRegisterDTO;
+import org.apache.shenyu.register.instance.api.ShenyuInstanceRegisterRepository;
+import org.apache.shenyu.spi.Join;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Properties;
+
+/**
+ * The type Nacos instance register repository.
+ */
+@Join
+public class NacosInstanceRegisterRepository implements ShenyuInstanceRegisterRepository {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(NacosInstanceRegisterRepository.class);
+
+ private static final String NAMESPACE = "nacosNameSpace";
+
+ private NamingService namingService;
+
+ private String groupName;
+
+ private String serviceName;
+
+ @Override
+ public void init(final InstanceConfig config) {
+ Properties properties = config.getProps();
+ Properties nacosProperties = new Properties();
+ this.groupName = properties.getProperty("groupName", "DEFAULT_GROUP");
+ this.serviceName = properties.getProperty("serviceName", "shenyu-instances");
+
+ String serverAddr = config.getServerLists();
+ nacosProperties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
+
+ nacosProperties.put(PropertyKeyConst.NAMESPACE, properties.getProperty(NAMESPACE, ""));
+ nacosProperties.put(PropertyKeyConst.USERNAME, properties.getProperty(PropertyKeyConst.USERNAME, ""));
+ nacosProperties.put(PropertyKeyConst.PASSWORD, properties.getProperty(PropertyKeyConst.PASSWORD, ""));
+ nacosProperties.put(PropertyKeyConst.ACCESS_KEY, properties.getProperty(PropertyKeyConst.ACCESS_KEY, ""));
+ nacosProperties.put(PropertyKeyConst.SECRET_KEY, properties.getProperty(PropertyKeyConst.SECRET_KEY, ""));
+ try {
+ this.namingService = NamingFactory.createNamingService(nacosProperties);
+ } catch (NacosException e) {
+ throw new ShenyuException(e);
+ }
+ }
+
+ @Override
+ public void persistInstance(final InstanceRegisterDTO instance) {
+ try {
+ Instance inst = new Instance();
+ inst.setWeight(1.0d);
+ inst.setEphemeral(true);
+ inst.setIp(instance.getHost());
+ inst.setPort(instance.getPort());
+ inst.setInstanceId(buildInstanceNodeName(instance));
+ inst.setServiceName(instance.getAppName());
+
+ namingService.registerInstance(serviceName, groupName, inst);
+ LOGGER.info("nacos client register success: {}", inst);
+ } catch (NacosException e) {
+ throw new ShenyuException(e);
+ }
+ }
+
+ private String buildInstanceNodeName(final InstanceRegisterDTO instance) {
+ String host = instance.getHost();
+ int port = instance.getPort();
+ return String.join(Constants.COLONS, host, Integer.toString(port));
+ }
+
+ @Override
+ public void close() {
+ try {
+ namingService.shutDown();
+ } catch (NacosException e) {
+ throw new ShenyuException(e);
+ }
+ }
+}
diff --git a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/src/main/resources/META-INF/shenyu/org.apache.shenyu.register.instance.api.ShenyuInstanceRegisterRepository b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/src/main/resources/META-INF/shenyu/org.apache.shenyu.register.instance.api.ShenyuInstanceRegisterRepository
new file mode 100644
index 000000000..3a403842a
--- /dev/null
+++ b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/src/main/resources/META-INF/shenyu/org.apache.shenyu.register.instance.api.ShenyuInstanceRegisterRepository
@@ -0,0 +1,17 @@
+# 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.
+
+nacos=org.apache.shenyu.register.instance.nacos.NacosInstanceRegisterRepository
diff --git a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/src/test/java/org/apache/shenyu/register/instance/nacos/NacosInstanceRegisterRepositoryTest.java b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/src/test/java/org/apache/shenyu/register/instance/nacos/NacosInstanceRegisterRepositoryTest.java
new file mode 100644
index 000000000..ea8c6b47d
--- /dev/null
+++ b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-nacos/src/test/java/org/apache/shenyu/register/instance/nacos/NacosInstanceRegisterRepositoryTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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.shenyu.register.instance.nacos;
+
+import com.alibaba.nacos.api.exception.NacosException;
+import com.alibaba.nacos.api.naming.NamingService;
+import com.alibaba.nacos.api.naming.pojo.Instance;
+import org.apache.shenyu.register.common.dto.InstanceRegisterDTO;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+
+public final class NacosInstanceRegisterRepositoryTest {
+
+ private NacosInstanceRegisterRepository repository;
+
+ private final Map<String, Instance> storage = new HashMap<>();
+
+ @BeforeEach
+ public void setUp() throws NoSuchFieldException, IllegalAccessException, NacosException {
+ this.repository = new NacosInstanceRegisterRepository();
+ Class<? extends NacosInstanceRegisterRepository> clazz = this.repository.getClass();
+
+ Field field = clazz.getDeclaredField("namingService");
+ field.setAccessible(true);
+ field.set(repository, mockNamingService());
+
+ field = clazz.getDeclaredField("groupName");
+ field.setAccessible(true);
+ field.set(repository, "group");
+
+ field = clazz.getDeclaredField("serviceName");
+ field.setAccessible(true);
+ field.set(repository, "shenyu-instances");
+
+ storage.clear();
+ }
+
+ private NamingService mockNamingService() throws NacosException {
+ NamingService namingService = mock(NamingService.class);
+
+ doAnswer(invocationOnMock -> {
+ String serviceName = invocationOnMock.getArgument(0);
+ String groupName = invocationOnMock.getArgument(1);
+ Instance value = invocationOnMock.getArgument(2);
+ storage.put(serviceName + "-" + groupName, value);
+ return null;
+ }).when(namingService).registerInstance(anyString(), anyString(), any());
+
+ doAnswer(invocationOnMock -> {
+ storage.clear();
+ return null;
+ }).when(namingService).shutDown();
+ return namingService;
+ }
+
+ @Test
+ public void testPersistInstance() {
+ InstanceRegisterDTO data = InstanceRegisterDTO.builder()
+ .appName("shenyu-test")
+ .host("shenyu-host")
+ .port(9195)
+ .build();
+
+ final String key = "shenyu-instances-group";
+ repository.persistInstance(data);
+ assertTrue(storage.containsKey(key));
+
+ final Instance instance = storage.get(key);
+ assertEquals(data.getHost(), instance.getIp());
+ assertEquals(data.getPort(), instance.getPort());
+ assertEquals(data.getAppName(), instance.getServiceName());
+ repository.close();
+ }
+}