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/05/01 10:53:28 UTC

[incubator-shenyu] branch master updated: Support regsiter instance to consul (#3366)

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 24b299b17 Support regsiter instance to consul (#3366)
24b299b17 is described below

commit 24b299b1773c40bcf488a057536a138f99e618c6
Author: renzhuyan <40...@qq.com>
AuthorDate: Sun May 1 18:53:19 2022 +0800

    Support regsiter instance to consul (#3366)
---
 .../shenyu-register-instance/pom.xml               |  1 +
 .../pom.xml                                        | 22 +++---
 .../consul/ConsulInstanceRegisterRepository.java   | 82 ++++++++++++++++++++++
 ...r.instance.api.ShenyuInstanceRegisterRepository | 17 +++++
 .../shenyu-register-instance-core/pom.xml          |  5 ++
 5 files changed, 114 insertions(+), 13 deletions(-)

diff --git a/shenyu-register-center/shenyu-register-instance/pom.xml b/shenyu-register-center/shenyu-register-instance/pom.xml
index bab6dc259..b77f142b5 100644
--- a/shenyu-register-center/shenyu-register-instance/pom.xml
+++ b/shenyu-register-center/shenyu-register-instance/pom.xml
@@ -31,6 +31,7 @@
         <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
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-consul/pom.xml
similarity index 64%
copy from shenyu-register-center/shenyu-register-instance/shenyu-register-instance-core/pom.xml
copy to shenyu-register-center/shenyu-register-instance/shenyu-register-instance-consul/pom.xml
index d2bd4e0eb..6bd42f553 100644
--- a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-core/pom.xml
+++ b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-consul/pom.xml
@@ -16,31 +16,27 @@
   ~ limitations under the License.
   -->
 
-<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">
+<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-instance</artifactId>
+        <groupId>org.apache.shenyu</groupId>
         <version>2.5.0-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
-    <artifactId>shenyu-register-instance-core</artifactId>
+    <artifactId>shenyu-register-instance-consul</artifactId>
 
     <dependencies>
         <dependency>
             <groupId>org.apache.shenyu</groupId>
-            <artifactId>shenyu-spi</artifactId>
+            <artifactId>shenyu-register-instance-api</artifactId>
             <version>${project.version}</version>
         </dependency>
         <dependency>
-            <groupId>org.apache.shenyu</groupId>
-            <artifactId>shenyu-register-instance-zookeeper</artifactId>
-            <version>${project.version}</version>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.shenyu</groupId>
-            <artifactId>shenyu-register-instance-etcd</artifactId>
-            <version>${project.version}</version>
+            <groupId>com.ecwid.consul</groupId>
+            <artifactId>consul-api</artifactId>
+            <version>${consul.api.version}</version>
         </dependency>
     </dependencies>
-
 </project>
\ No newline at end of file
diff --git a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-consul/src/main/java/org/apache/shenyu/register/instance/consul/ConsulInstanceRegisterRepository.java b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-consul/src/main/java/org/apache/shenyu/register/instance/consul/ConsulInstanceRegisterRepository.java
new file mode 100644
index 000000000..dbed6a019
--- /dev/null
+++ b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-consul/src/main/java/org/apache/shenyu/register/instance/consul/ConsulInstanceRegisterRepository.java
@@ -0,0 +1,82 @@
+/*
+ * 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.consul;
+
+import com.ecwid.consul.v1.ConsulClient;
+import com.ecwid.consul.v1.agent.model.NewCheck;
+import org.apache.shenyu.common.config.ShenyuConfig.InstanceConfig;
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.register.common.dto.InstanceRegisterDTO;
+import org.apache.shenyu.register.common.path.RegisterPathConstants;
+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 Consul instance register repository.
+ */
+@Join
+public class ConsulInstanceRegisterRepository implements ShenyuInstanceRegisterRepository {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ConsulInstanceRegisterRepository.class);
+
+    private ConsulClient consulClient;
+
+    private NewCheck check;
+
+    @Override
+    public void init(final InstanceConfig config) {
+        final Properties props = config.getProps();
+        final String timeout = props.getProperty("consulTimeout", "3000");
+        final String ttl = props.getProperty("consulTTL", "3000");
+        final String name = props.getProperty("consulName", "shenyu-gateway");
+        check = new NewCheck();
+        check.setName(name);
+        check.setId(name);
+        check.setTtl(ttl.concat("ms"));
+        check.setTimeout(timeout.concat("ms"));
+        consulClient = new ConsulClient(config.getServerLists());
+        consulClient.agentCheckRegister(check);
+    }
+
+    @Override
+    public void persistInstance(final InstanceRegisterDTO instance) {
+        String instanceNodeName = buildInstanceNodeName(instance);
+        String instancePath = RegisterPathConstants.buildInstanceParentPath();
+        String realNode = RegisterPathConstants.buildRealNode(instancePath, instanceNodeName);
+        String nodeData = GsonUtils.getInstance().toJson(instance);
+        consulClient.setKVValue(realNode, nodeData);
+        LOGGER.info("consul client register success: {}", nodeData);
+    }
+
+    @Override
+    public void close() {
+        consulClient.agentCheckDeregister(check.getId());
+    }
+
+    private String buildInstanceNodeName(final InstanceRegisterDTO instance) {
+        String host = instance.getHost();
+        int port = instance.getPort();
+        return String.join(Constants.COLONS, host, Integer.toString(port));
+    }
+
+}
diff --git a/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-consul/src/main/resources/META-INF/shenyu/org.apache.shenyu.register.instance.api.ShenyuInstanceRegisterRepository b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-consul/src/main/resources/META-INF/shenyu/org.apache.shenyu.register.instance.api.ShenyuInstanceRegisterRepository
new file mode 100644
index 000000000..8e3e3d6f3
--- /dev/null
+++ b/shenyu-register-center/shenyu-register-instance/shenyu-register-instance-consul/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.
+
+consul=org.apache.shenyu.register.instance.consul.ConsulInstanceRegisterRepository
\ 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 d2bd4e0eb..9864e71b7 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
@@ -41,6 +41,11 @@
             <artifactId>shenyu-register-instance-etcd</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shenyu</groupId>
+            <artifactId>shenyu-register-instance-consul</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
 
 </project>
\ No newline at end of file