Skip to content

Commit

Permalink
Add etcd client impl (#84)
Browse files Browse the repository at this point in the history
* add etcd sdk impl

* check
  • Loading branch information
Alonexc authored Apr 2, 2024
1 parent c00a0f1 commit 67bc4f0
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 5 deletions.
5 changes: 5 additions & 0 deletions eventmesh-dashboard-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ public enum SDKTypeEnum {

META_NACOS_NAMING,


META_ETCD,
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 67bc4f0

Please sign in to comment.