etcd: 从带密码的etcd中读取数据

文章介绍了如何在Java项目中添加并使用io.etcd和IBM提供的EtcdJava客户端进行依赖管理,以及演示了基本的KV操作,如设置/获取键值对和范围查询。
摘要由CSDN通过智能技术生成

一、添加依赖

        <dependency>
            <groupId>io.etcd</groupId>
            <artifactId>jetcd-core</artifactId>
            <version>0.5.0</version>
        </dependency>

二、demo程序

package cn.edu.tju;

import com.google.protobuf.ByteString;
import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.KV;
import io.etcd.jetcd.kv.GetResponse;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.CompletableFuture;

public class EtcdTest3 {
    private static String SERVER_IP = "http://xx.xx.xx.xx:2379";

    public static void main(String[] args) throws Exception {
        ByteString username = ByteString.copyFrom("root".getBytes());
        ByteString password = ByteString.copyFrom("root".getBytes());
        Client client = Client.
                builder()
                //设置用户名
                .user(ByteSequence.from(username))
                //设置密码
                .password(ByteSequence.from(password))
                .endpoints(SERVER_IP).build();
        KV kvClient = client.getKVClient();
        ByteSequence key = ByteSequence.from(new String("foo").getBytes("utf-8"));
        CompletableFuture<GetResponse> getFuture = kvClient.get(key);
        GetResponse response = getFuture.get();
        response.getKvs().forEach(item -> {
            String yourKey = new String(item.getKey().getBytes());
            String yourValue = new String(item.getValue().getBytes());
            System.out.println(yourKey);
            System.out.println(yourValue);
        });
    }
}

也可以使用IBM提供的客户端

        <dependency>
            <groupId>com.ibm.etcd</groupId>
            <artifactId>etcd-java</artifactId>
            <version>0.0.24</version>
        </dependency>
package cn.edu.tju;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.protobuf.ByteString;
import com.ibm.etcd.api.KeyValue;
import com.ibm.etcd.api.RangeResponse;
import com.ibm.etcd.client.EtcdClient;
import com.ibm.etcd.client.KvStoreClient;
import com.ibm.etcd.client.kv.KvClient;
import org.springframework.stereotype.Service;
import org.w3c.dom.ranges.Range;

import java.util.List;

public class EtcdTest2 {

    private static String CONNECTION_STRING = "xx.xx.xx.xx";
    private static String USERNAME="root";
    private static String PASSWORD="root";

    public static void main(String[] args) {
        KvClient etcdClient = getEtcdClient();
        ByteString bs = ByteString.copyFrom("foo".getBytes());
        ByteString bs2 = ByteString.copyFrom("world2024".getBytes());
        etcdClient.put(bs, bs2).timeout(30000).async();
        KvClient.FluentRangeRequest fluentRangeRequest = etcdClient.get(bs).timeout(30000);
        System.out.println("I will sleep 5 seconds...");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ListenableFuture<RangeResponse> result = etcdClient.get(bs).timeout(30000).async();
        RangeResponse rangeResponse = null;
        try {
            rangeResponse = result.get();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex.getMessage());

        }
        List<KeyValue> lst = rangeResponse.getKvsList();
        for (KeyValue kv : lst) {
            ByteString me = kv.getValue();
            String hello = me.toStringUtf8();
            System.out.println(hello);
        }

    }

    public static KvClient getEtcdClient() {

        KvStoreClient client = EtcdClient.forEndpoint(CONNECTION_STRING, 2379)
                .withPlainText()
                .withCredentials(USERNAME, PASSWORD)
                .build();
        KvClient kvClient = client.getKvClient();
        return kvClient;
    }

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值