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

企业网站建设管理及推广手机微信网页版登录

企业网站建设管理及推广,手机微信网页版登录,海南网站搭建价格,专门做网站网站犯法吗文章目录 前言etcd安装Ubuntu 上通过包管理器安装通过源码安装配置 客户端开发包开发包的安装接口介绍添加一个键值对获取一个键值对租约保活机制监听 封装服务注册与发现服务注册服务发现 前言 Etcd 是一个 golang 编写的分布式、高可用的一致性键值存储系统#xff0c;用于配… 文章目录 前言etcd安装Ubuntu 上通过包管理器安装通过源码安装配置 客户端开发包开发包的安装接口介绍添加一个键值对获取一个键值对租约保活机制监听 封装服务注册与发现服务注册服务发现 前言 Etcd 是一个 golang 编写的分布式、高可用的一致性键值存储系统用于配置共享和服务发现等 etcd安装 Ubuntu 上通过包管理器安装 # 直接安装 sudo apt-get install etcd # 启动 sudo systemctl start etcd # 设置开机自启 sudo systemctl enable etcd验证安装 $ etcd --version etcd Version: 3.2.26 Git SHA: Not provided (use ./build instead of go build) Go Version: go1.13.8 Go OS/Arch: linux/amd64可见已经安装成功 通过源码安装 etcd是使用Go语言编写的, 根据官网描述的教程, 需要Go1.2的版本 # 克隆仓库 $ git clone -b v3.5.16 https://github.com/etcd-io/etcd.git $ cd etcd # 运行构建脚本 $ ./build.sh # 二进制文件位于 directory/bin # 设置环境变量, 假如你的二进制文件位于/bin $ export PATH$PATH:pwd/bin # 验证安装 $ etcd --version配置 默认 etcd 的集群节点通信端口为 2380, 客户端访问端口为 2379, 如果需要更改可以修改/etc/default/etcd文件 客户端开发包 开发包的安装 官方只维护了 go 语言版本的 client 库, 因此需要使用 C/C 非官方的 client 开发库, etcd-cpp-apiv3是一个不错的选择, etcd-cpp-apiv3 是一个 etcd 的 C版本客户端 API。它依赖于 mipsasm, boost, protobuf, gRPC, cpprestsdk 等库 所有首先需要安装依赖 # 建议先看看是否已经用其他方式安装这些, 否则出现版本冲突问题很难受 # 比如up就在项目中途把protobuf版本更新了, 就导致所有依赖protobuf编译安装的第三方库都用不了 # 比如这个, brpc等等都用不了,最后重新编译安装了它们才解决, 还出现了莫名其妙的库找不到的问题 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使用源码安装即可 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接口介绍 项目中主要使用etcd进行服务注册和发现, 我们重点介绍这些接口, 其他介绍可参考官方介绍 添加一个键值对 #includeetcd/Client.hpp//构造一个etcd::Client 对象, 用etcd服务端的url构造 etcd::Client etcd(http://127.0.0.1:2379); //使用put方法添加一个键值对, etcd.put() 方法是非阻塞的返回一个异步任务pplx::task pplx::tasketcd::Response response_task etcd.put(/test/1, 111); // 等待异步任务完成并获取结果 try {etcd::Response response response_task.get();if (response.is_ok()) {std::cout Put: response.key() : response.value() std::endl;} else {std::cerr Error: response.error_message() std::endl;} } catch (const std::exception e) {std::cerr e.what() std::endl; }获取一个键值对 etcd::Client etcd(http://127.0.0.1:2379); //使用get方法获取一个键值对, 这个也是非阻塞的, 返回一个异步任务 pplx::tasketcd::Response response_task etcd.get(/test/1); try {etcd::Response response response_task.get();if (response.is_ok()){std::cout 获取成功 response.value().as_string();}else{std::cout 获取失败: response.error_message();} } catch (std::exception const ex) {std::cerr ex.what() std::endl; }租约保活机制 在项目中我们需要监控一个服务的在线情况, 如果异常退出, 有时候就不能很好的检测到, etcd提供了租约保活机制, 服务如果没有续约, 就可以认为该服务已下线 etcd::Client etcd(http://127.0.0.1:2379);//获取一个三秒的租约保活对象 std::shared_ptretcd::KeepAlive keep(etcd.leasekeepalive(3).get());//获取租约IDint64 lease_idkeep-Lease();etcd.put(/test/2, 222,lease_id);监听 项目中需要对服务的上线与下线进行监控, 再结合其他组件, 比如bprc, 就可通过这个服务获取相对应的通信对象 //构造一个监控对象, 第二个参数可以是一个目录, 比如/test, 那么就会监控/test下比如/test/1 /test/2, 第三个是一个回调函数, 当被监控的对象发送改变时就会触发回调etcd::Watcher watcher(http://127.0.0.1:2379, /test, CallBack);//etcd::Response是一个容器, 可以通过遍历的方式获取, 比如void CallBack(const etcd::Response re){if(!re.is_ok()){LOG_ROOT_ERRORre.error_message();return;}for(const auto ev: re.events()){//通过etcd::EVent::EventType 可得知具体情况if(ev.event_type()etcd::Event::EventType::PUT){LOG_ROOT_INFO新增服务: ev.kv().key() : ev.kv().as_string();}if(ev.event_type()etcd::Event::EventType::DELETE_){LOG_ROOT_INFO服务下线: ev.prev_kv().key() : ev.prev_kv().as_string();}}}封装服务注册与发现 有了上面了了解, 我们就能完成一个简单的服务注册与发现模块 服务注册 #includeetcd/Client.hpp #includeetcd/KeepAlive.hpp #includeetcd/Response.hpp #includeetcd/Value.hpp #includeetcd/Watcher.hpp #includelog.hpp namespace MindbniM {class Registry{public:using ptrstd::shared_ptrRegistry;//这里默认使用3秒保活了, 也可以手动传入Registry(const std::string host):_client(std::make_uniqueetcd::Client(host)),_ka(_client-leasekeepalive(3).get()),_lease_id(_ka-Lease()){}bool registry(const std::string key,const std::string value){etcd::Response re_client-put(key,value,_lease_id).get();if(!re.is_ok()){LOG_ROOT_ERROR服务注册失败:re.error_message();return false;}LOG_ROOT_DEBUG服务注册:key : value;return true;}private:std::unique_ptretcd::Client _client;std::shared_ptretcd::KeepAlive _ka;int64_t _lease_id;}; }服务发现 为了和其他组件相互结合, 我们设置两个回调, 分别对应服务上线与下线的操作 #includeetcd/Client.hpp #includeetcd/Response.hpp #includeetcd/Value.hpp #includeetcd/Watcher.hpp #includelog.hpp namespace MindbniM {class Discovery{public:using ptrstd::shared_ptrDiscovery;//对应key和valueusing CallBackstd::functionvoid(const std::string,const std::string);Discovery(const std::string host,const CallBack putnullptr,const CallBack delnullptr):_client(std::make_uniqueetcd::Client(host)),_put(put),_del(del){}bool discover(const std::string dir){etcd::Response re_client-ls(dir).get();if(!re.is_ok()){LOG_ROOT_ERROR服务发现错误re.error_message();return false;}int nre.keys().size();if(_put!nullptr){for(int i0;in;i){_put(re.key(i),re.value(i).as_string());}}//开始监听_watchstd::make_uniqueetcd::Watcher(*_client,dir,std::bind(Discovery::_CallBack,this,std::placeholders::_1),true);return true;}bool wait(){return _watch-Wait();}private:void _CallBack(const etcd::Response re){if(!re.is_ok()){LOG_ROOT_ERRORre.error_message();return;}for(const auto ev: re.events()){if(ev.event_type()etcd::Event::EventType::PUT){if(_put) _put(ev.kv().key(),ev.kv().as_string());LOG_ROOT_INFO新增服务: ev.kv().key() : ev.kv().as_string();}if(ev.event_type()etcd::Event::EventType::DELETE_){if(_del) _del(ev.prev_kv().key(),ev.prev_ky().as_string());LOG_ROOT_INFO服务下线: ev.prev_kv().key() : ev.prev_kv().as_string();}}}CallBack _put;CallBack _del;std::unique_ptretcd::Client _client;std::unique_ptretcd::Watcher _watch;}; }
http://www.pierceye.com/news/990617/

相关文章:

  • 网站无法连接服务器哪些国家网站无须备案
  • 重庆做网站设计培训机构排名全国十大教育机构排名
  • 做网站建设销售网络营销推广技巧
  • 南宁网站制作定制北京网站seo服务
  • 门户网站网页设计规范willin kan 让你的wordpress飞起来
  • 建设银行广州招聘网站wordpress dz
  • 如何介绍自己做的网站东莞回收网站设计
  • 北京驾校网站建设厦门网页设计培训班
  • 网络公司给我做网站我有没有源代码版权吗我怎么做个人网站
  • 免费建站网站一站式做网站需要懂那些软件
  • 做新网站怎样提交360寻找销售团队外包
  • 重庆市建设网站wordpress 新闻模版
  • 国内网站建设推荐手工做的网站
  • 深圳罗湖做网站的公司网站建设与管理案例教程第三版课后答案
  • 有关网站招标商务标书怎么做做终端客户网站
  • c 网站做微信收款功能青岛网站建设定制
  • 贵州安顺建设主管部门网站网站全程设计技术
  • 公司宣传网站建设企业网站建设与实现的论文
  • 连云港网站建设推广网站的推广优化
  • 手机商城网站制作公司网站版面设计方案
  • 网站开发制作熊掌号网站推广方法主要有什么
  • 怎么查看网站的建设时间提高企业网站的访问率
  • 宁德做网站的公司长沙网站建设 芙蓉区
  • 兴平市住房和城乡建设局门户网站会员管理网站建设
  • 做seo的网站是怎么样的上饶小程序开发公司
  • 网站硬件需求建网站网站
  • 网站主机域名合肥室内装修设计培训
  • 上海网站优化网站对企业的作用
  • 哪些园林网站可以做外链网址怎么注册
  • 做网站域名服务器wordpress 互动性