This is an automated email from the ASF dual-hosted git repository.

xiatian pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/eventmesh-dashboard.git


The following commit(s) were added to refs/heads/dev by this push:
     new 67bc4f0  Add etcd client impl (#84)
67bc4f0 is described below

commit 67bc4f0e077d95d6c7eace09296a09f013b78e15
Author: Alonexc <[email protected]>
AuthorDate: Tue Apr 2 18:14:19 2024 +0800

    Add etcd client impl (#84)
    
    * add etcd sdk impl
    
    * check
---
 eventmesh-dashboard-core/pom.xml                   |  5 ++
 .../dashboard/core/function/SDK/SDKManager.java    |  3 +
 .../dashboard/core/function/SDK/SDKTypeEnum.java   |  2 +-
 .../CreateEtcdConfig.java}                         | 25 +++-----
 .../function/SDK/operation/EtcdSDKOperation.java   | 31 ++++++++--
 .../SDK/operation/EtcdSDKCreateOperationTest.java  | 68 ++++++++++++++++++++++
 6 files changed, 113 insertions(+), 21 deletions(-)

diff --git a/eventmesh-dashboard-core/pom.xml b/eventmesh-dashboard-core/pom.xml
index 22cdbd9..64d6dff 100644
--- a/eventmesh-dashboard-core/pom.xml
+++ b/eventmesh-dashboard-core/pom.xml
@@ -58,6 +58,11 @@
             <artifactId>nacos-client</artifactId>
             <version>2.2.4</version>
         </dependency>
+        <dependency>
+            <groupId>io.etcd</groupId>
+            <artifactId>jetcd-core</artifactId>
+            <version>0.3.0</version>
+        </dependency>
 
         <!-- Event Store -->
         <dependency>
diff --git 
a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKManager.java
 
b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKManager.java
index 43822e3..a203ff3 100644
--- 
a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKManager.java
+++ 
b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKManager.java
@@ -18,6 +18,7 @@
 package org.apache.eventmesh.dashboard.core.function.SDK;
 
 import org.apache.eventmesh.dashboard.core.function.SDK.config.CreateSDKConfig;
+import 
org.apache.eventmesh.dashboard.core.function.SDK.operation.EtcdSDKOperation;
 import 
org.apache.eventmesh.dashboard.core.function.SDK.operation.NacosConfigSDKOperation;
 import 
org.apache.eventmesh.dashboard.core.function.SDK.operation.NacosNamingSDKOperation;
 import 
org.apache.eventmesh.dashboard.core.function.SDK.operation.NacosSDKOperation;
@@ -70,6 +71,8 @@ public class SDKManager {
         clientCreateOperationMap.put(SDKTypeEnum.META_NACOS_CONFIG, new 
NacosConfigSDKOperation());
         clientCreateOperationMap.put(SDKTypeEnum.META_NACOS_NAMING, new 
NacosNamingSDKOperation());
 
+        clientCreateOperationMap.put(SDKTypeEnum.META_ETCD, new 
EtcdSDKOperation());
+
     }
 
     private SDKManager() {
diff --git 
a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKTypeEnum.java
 
b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKTypeEnum.java
index d01efa1..c6524c8 100644
--- 
a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKTypeEnum.java
+++ 
b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKTypeEnum.java
@@ -34,5 +34,5 @@ public enum SDKTypeEnum {
 
     META_NACOS_NAMING,
 
-
+    META_ETCD,
 }
diff --git 
a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKTypeEnum.java
 
b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateEtcdConfig.java
similarity index 74%
copy from 
eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKTypeEnum.java
copy to 
eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateEtcdConfig.java
index d01efa1..1168a65 100644
--- 
a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/SDKTypeEnum.java
+++ 
b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateEtcdConfig.java
@@ -15,24 +15,17 @@
  * limitations under the License.
  */
 
-package org.apache.eventmesh.dashboard.core.function.SDK;
+package org.apache.eventmesh.dashboard.core.function.SDK.config;
 
-public enum SDKTypeEnum {
+import lombok.Data;
 
-    RUNTIME,
-
-    STORAGE_ROCKETMQ_REMOTING,
-
-    STORAGE_ROCKETMQ_PRODUCER,
-
-    STORAGE_ROCKETMQ_CONSUMER,
-
-    STORAGE_REDIS,
-
-    META_NACOS,
-    META_NACOS_CONFIG,
-
-    META_NACOS_NAMING,
+@Data
+public class CreateEtcdConfig implements CreateSDKConfig {
 
+    private String etcdServerAddress;
 
+    @Override
+    public String getUniqueKey() {
+        return etcdServerAddress;
+    }
 }
diff --git 
a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKOperation.java
 
b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKOperation.java
index 3f483c1..b630a38 100644
--- 
a/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKOperation.java
+++ 
b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKOperation.java
@@ -18,18 +18,41 @@
 package org.apache.eventmesh.dashboard.core.function.SDK.operation;
 
 import org.apache.eventmesh.dashboard.core.function.SDK.AbstractSDKOperation;
+import 
org.apache.eventmesh.dashboard.core.function.SDK.config.CreateEtcdConfig;
 import org.apache.eventmesh.dashboard.core.function.SDK.config.CreateSDKConfig;
 
 import java.util.AbstractMap.SimpleEntry;
 
-public class EtcdSDKOperation extends AbstractSDKOperation {
+import io.etcd.jetcd.Client;
+import io.etcd.jetcd.KV;
+import io.etcd.jetcd.common.exception.EtcdException;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class EtcdSDKOperation extends AbstractSDKOperation<KV> {
+
     @Override
-    public SimpleEntry createClient(CreateSDKConfig clientConfig) {
-        return null;
+    public SimpleEntry<String, KV> createClient(CreateSDKConfig clientConfig) {
+        final CreateEtcdConfig etcdConfig = (CreateEtcdConfig) clientConfig;
+        KV kvClient = null;
+        try {
+            final Client client = Client.builder()
+                .endpoints(getSplitEndpoints(etcdConfig))
+                .build();
+            kvClient = client.getKVClient();
+        } catch (EtcdException e) {
+            log.error("create etcd client failed", e);
+        }
+        return new SimpleEntry<>(clientConfig.getUniqueKey(), kvClient);
+    }
+
+    private static String[] getSplitEndpoints(CreateEtcdConfig etcdConfig) {
+        return etcdConfig.getEtcdServerAddress().split(",");
     }
 
     @Override
     public void close(Object client) {
-
+        castClient(client).close();
     }
 }
diff --git 
a/eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKCreateOperationTest.java
 
b/eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKCreateOperationTest.java
new file mode 100644
index 0000000..a4d8f4b
--- /dev/null
+++ 
b/eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKCreateOperationTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.eventmesh.dashboard.core.function.SDK.operation;
+
+import 
org.apache.eventmesh.dashboard.core.function.SDK.config.CreateEtcdConfig;
+
+import java.nio.charset.StandardCharsets;
+import java.util.AbstractMap.SimpleEntry;
+import java.util.List;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import io.etcd.jetcd.ByteSequence;
+import io.etcd.jetcd.KV;
+import io.etcd.jetcd.KeyValue;
+import io.etcd.jetcd.kv.GetResponse;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class EtcdSDKCreateOperationTest {
+
+    private final EtcdSDKOperation etcdSDKOperation = new EtcdSDKOperation();
+
+    private static final String key = "/test/foo";
+
+    private static final String value = "test";
+
+    private static final String url = "http://127.0.0.1:2379";;
+
+    @Test
+    void testCreateClient() {
+        final CreateEtcdConfig etcdConfig = new CreateEtcdConfig();
+        etcdConfig.setEtcdServerAddress(url);
+        try {
+            final SimpleEntry<String, KV> simpleEntry = 
etcdSDKOperation.createClient(etcdConfig);
+            Assertions.assertEquals(url, simpleEntry.getKey());
+            simpleEntry.getValue().put(bytesOf(key), bytesOf(value));
+            final GetResponse response = 
simpleEntry.getValue().get(bytesOf(key)).get();
+            final List<KeyValue> keyValues = response.getKvs();
+            log.info("get key = {} , value = {} from etcd success",
+                keyValues.get(0).getKey().toString(StandardCharsets.UTF_8),
+                keyValues.get(0).getValue().toString(StandardCharsets.UTF_8));
+        } catch (Exception e) {
+            log.error("create etcd client failed", e);
+        }
+    }
+
+    private static ByteSequence bytesOf(String val) {
+        return ByteSequence.from(val, StandardCharsets.UTF_8);
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to