From c7e60461ec78cb17e1db1454f6482d8dfd5feeab Mon Sep 17 00:00:00 2001 From: Alonexc Date: Tue, 2 Apr 2024 15:57:49 +0800 Subject: [PATCH] add etcd sdk impl --- eventmesh-dashboard-core/pom.xml | 5 ++ .../core/function/SDK/SDKManager.java | 3 + .../core/function/SDK/SDKTypeEnum.java | 2 +- .../function/SDK/config/CreateEtcdConfig.java | 31 +++++++++ .../SDK/operation/EtcdSDKOperation.java | 31 +++++++-- .../operation/EtcdSDKCreateOperationTest.java | 68 +++++++++++++++++++ 6 files changed, 135 insertions(+), 5 deletions(-) create mode 100644 eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateEtcdConfig.java create mode 100644 eventmesh-dashboard-core/src/test/java/org/apache/eventmesh/dashboard/core/function/SDK/operation/EtcdSDKCreateOperationTest.java diff --git a/eventmesh-dashboard-core/pom.xml b/eventmesh-dashboard-core/pom.xml index 22cdbd99..64d6dff9 100644 --- a/eventmesh-dashboard-core/pom.xml +++ b/eventmesh-dashboard-core/pom.xml @@ -58,6 +58,11 @@ nacos-client 2.2.4 + + io.etcd + jetcd-core + 0.3.0 + 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 43822e30..a203ff30 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 static SDKManager getInstance() { 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 d01efa1c..c6524c8e 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/config/CreateEtcdConfig.java b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateEtcdConfig.java new file mode 100644 index 00000000..4c7329bd --- /dev/null +++ b/eventmesh-dashboard-core/src/main/java/org/apache/eventmesh/dashboard/core/function/SDK/config/CreateEtcdConfig.java @@ -0,0 +1,31 @@ +/* + * 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.config; + +import lombok.Data; + +@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 3f483c1f..b630a38b 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 { + @Override - public SimpleEntry createClient(CreateSDKConfig clientConfig) { - return null; + public SimpleEntry 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 00000000..a4d8f4be --- /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 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 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); + } +}