Etcd的安装与使用

1.Etcd介绍

Etcd 是一个 golang 编写的分布式、高可用的一致性键值存储系统,用于配置共享和服
务发现等。它使用 Raft 一致性算法来保持集群数据的一致性,且客户端通过长连接
watch 功能,能够及时收到数据变化通知。 以下
是关于 etcd 的安装与使用方法的详细介绍。
安装 Etcd
首先,需要在你的系统中安装 Etcd 。 Etcd 是一个分布式键值存储,通常用于服务发现
和配置管理。以下是在 Linux 系统上安装 Etcd 的基本步骤

2.安装 Etcd:

apt-get install etcd

启动 Etcd 服务:

systemctl start etcd

设置 Etcd 开机自启:

systemctl enable etcd

3.节点配置

如果是单节点集群其实就可以不用进行配置,默认 etcd 的集群节点通信端口为 2380 ,
客户端访问端口为 2379.
若需要修改,则可以配置: /etc/default/etcd

4.运行验证

etcdctl put mykey "etcd test"

如果出现报错:

No help topic for 'put'

则sudo vim /etc/profile 在末尾声明环境变量 ETCDCTL_API=3 以确定 etcd 版本。

完毕后,加载配置文件,并重新执行测试指令

5.安装etcd-cpp-apiv3

etcd-cpp-apiv3 是一个 etcd 的 C++ 版本客户端 API 。它依赖于 mipsasm, boost,
protobuf, gRPC, cpprestsdk 等库。
etcd-cpp-apiv3 的 GitHub 地址是: https://github.com/etcd-cpp-apiv3/etcd-cpp-apiv3

依赖安装:

sudo apt-get install libboost-all-dev libssl-dev
sudo apt-get install libprotobuf-dev protobuf-compiler-grpc
sudo apt-get install libgrpc-dev libgrpc++-dev 
sudo apt-get install libcpprest-dev

api 框架安装

git clone https://github.com/etcd-cpp-apiv3/etcd-cpp-apiv3.git
cd etcd-cpp-apiv3
mkdir build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr
make -j$(nproc) && sudo make install

6.测试样例

put.cpp

css 复制代码
#include<etcd/Client.hpp>
#include<etcd/Response.hpp>
#include<etcd/KeepAlive.hpp>
#include<etcd/Watcher.hpp>
#include<chrono>
#include<thread>
#include<string>
#include<iostream>
int main(int argc,char* argv[])
{
    std::string etcd_host="http://127.0.0.1:2379";
    //实例化客户端对象
    etcd::Client client(etcd_host);
    //获取租约
    auto keep_alive=client.leasekeepalive(3).get();
    auto lease_id = keep_alive->Lease();
    //向etcd新增数据
    auto resp=client.put("/service/test1","127.0.0.1:8080",lease_id).get();
    if(resp.is_ok()==false)
    {
        std::cerr<<"put error:"<<resp.error_message()<<endl;
        return -1;
    }
    auto resp2=client.put("/service/test2","127.0.0.1:9090",lease_id).get();
    if(resp2.is_ok()==false)
    {
        std::cerr<<"put error:"<<resp2.error_message()<<endl;
        return -1;
    }
    std::this_thread::sleep_for(std::chrono::seconds(100));
    return 0;
}

get.cpp

css 复制代码
#include<etcd/Client.hpp>
#include<etcd/Response.hpp>
#include<etcd/KeepAlive.hpp>
#include<etcd/Watcher.hpp>
#include<chrono>
#include<thread>
#include<string>
#include<iostream>

void callback(const etcd::Response& response)
{
    if (response.is_ok() == false)
    {
        std::cout << "callback error:" << response.error_message() << std::endl;
        return ;
    }

    for(auto const & ev: response.events())
    {
        if(ev.event_type() == etcd::Event::EventType::PUT)
        {
            std::cout<<"原本的k-v:"<<ev.kv().key()<<"-"<<ev.kv().as_string()<<std::endl;
            std::cout<<"现在的k-v:"<<ev.prev_kv().key()<<"-"<<ev.prev_kv().as_string()<<std::endl;
        }
        else if(ev.event_type() == etcd::Event::EventType::DELETE_)
        {
            std::cout<<"原本的k-v:"<<ev.prev_kv().key()<<"-"<<ev.prev_kv().as_string()<<std::endl;
            std::cout<<"现在的k-v:NULL"<<std::endl;
        }
    }
}


int main(int argc,char* argv[])
{
    std::string etcd_host="http://127.0.0.1:2379";
    // 实例化客户端对象
    //实例化客户端对象
    etcd::Client client(etcd_host);

    // 获取指定路径下的所有键值对
    auto response = client.ls("/service").get();

    // 检查响应是否成功
    if (response.is_ok() == false)
    {
        std::cout << "error:" << response.error_message() << std::endl;
        return -1;
    }

    // 获取键值对的数量
    int sz = response.keys().size();
    // 遍历所有键值对并输出
    for (int i = 0; i < sz; ++i)
    {
        // 输出键值对
        std::cout << response.value(i) << ":" << response.key(i) << std::endl;
    }

    auto watcher = etcd::Watcher(client,"/service",callback,true);
    watcher.Wait();

}

makefile

css 复制代码
all:get put
get:get.cpp
	g++ get.cpp -o get -std=c++17 -letcd-cpp-api -lcpprest
put:put.cpp
	g++ put.cpp -o put -std=c++17 -letcd-cpp-api -lcpprest

@PHONY:clean
clean:
	rm -f put get

运行测试

至此大家已经可以基本了解安装和简单使用etcd了。谢谢观看

相关推荐
程序员总部26 分钟前
如何应对Maven中的依赖导入挑战?
数据库·sql·maven
赵渝强老师2 小时前
【赵渝强老师】管理MongoDB的运行
数据库·mongodb
A仔不会笑2 小时前
MySQL面试篇——性能优化
java·数据库·mysql·面试·性能优化
梦幻编织者2 小时前
python使用django搭建图书管理系统
数据库·python·django
考虑考虑3 小时前
MySQL中的DATE_FORMAT时间函数
数据库·后端·mysql
SelectDB技术团队3 小时前
云原生时代的架构革新,Apache Doris 存算分离如何实现弹性与性能双重提升
大数据·数据库·云原生·doris·存算分离
星迹日3 小时前
MySQL : 数据库和表操作
数据库·mysql·数据类型··
2302_799525744 小时前
【Hadoop】如何理解MapReduce?
数据库·hadoop·mapreduce
已是上好佳4 小时前
介绍一下Qt中的事件过滤
java·服务器·数据库