当前位置: 首页 > news >正文

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

#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

#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

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了。谢谢观看

相关文章:

  • vulkanscenegraph显示倾斜模型(5.2)-交换链
  • ViT-Small与Vit-Base区别
  • CI/CD—GitLab部署
  • 【redis】数据类型之geo
  • 【GPT入门】第12课 FunctionCall 生成数据库sql代码
  • 虚拟机的xml格式
  • 市场动态变化中的策略运用
  • Scala(Array,List,Set,Map,Tuple,字符串 使用的简单介绍)
  • 【redis】五种数据类型和编码方式
  • HTML 标签语义化指南:让网页更易读
  • 【认知学习篇】【深度拆解DeepSeek:从技术内核到江湖地位(万字暴力拆机报告)】
  • Linux系统之nethogs工具的基本使用
  • 技术领域,有许多优秀的博客和网站
  • Day04 模拟原生开发app过程 Androidstudio+逍遥模拟器
  • 【GPT入门】第11课 FunctionCall调用本地代码入门
  • k8s部署deepseek基于cpu的部署
  • 数字人系统源码---v10技术五大底层架构链路全局开发思路
  • Python中与字符串操作相关的30个常用函数及其示例
  • 每日一题——乘积最大子数组
  • 4.桥接模式
  • 如何推广网站完善火2星/cnzz站长统计工具
  • 可以做pos机的网站/百度导航和百度地图
  • 网站推广系统/页优化软件
  • 如何做网站产品图片/西安专业网络推广平台
  • 域名解析好了怎么做网站/关键词挖掘工具免费
  • 链接点开网页表白的网站怎么做的/网站域名怎么注册