zookeeper 实现一个简单的服务注册与发现(C++) 三:服务发现
2021-01-02 10:29
标签:flags 执行 服务 sub count todo car conf onclick git:git@github.com:ccx19930930/services_register_and_discovery.git 参考链接:https://www.cnblogs.com/haippy/archive/2013/02/21/2920280.html down_service_mgr.h down_service_mgr.cpp discovery.h discovery.cpp discovery_test main.cpp Makefile 执行结果: 只执行discovery_test 使用register_test_1.conf 启动多个测试程序后 使用register_test_2.conf 启动多个测试程序后 依次停止register_test: 1 #ifndef _DOWN_SERVICE_MGR_H_
2 #define _DOWN_SERVICE_MGR_H_
3
4 #include "base_class.h"
5
6 class CDownServiceMgr : CUnCopyable
7 {
8 public:
9 CDownServiceMgr(int module_id) : m_module_id(module_id) {}
10 ~CDownServiceMgr() {}
11 private:
12 CDownServiceMgr() {}
13
14 public:
15 int Register(const string& zk_path, CNodeInfo* node_info);
16 int UnRegister(const string& zk_path);
17
18 private:
19 int m_module_id;
20 mapstring, CNodeInfo *> m_node_list;
21 pthread_mutex_t m_mutex;
22 };
23
24 #endif
1 #include "down_service_mgr.h"
2 #include "auto_lock.h"
3
4 int CDownServiceMgr::Register(const string& zk_path, CNodeInfo* node_info)
5 {
6 CAutoMutexLock auto_lock(m_mutex);
7 if (m_node_list.count(zk_path))
8 {
9 return -1;
10 }
11 m_node_list[zk_path] = node_info;
12 //TODO 长连接等
13
14 return 0;
15 }
16
17 int CDownServiceMgr::UnRegister(const string& zk_path)
18 {
19 CAutoMutexLock auto_lock(m_mutex);
20 if (m_node_list.count(zk_path) == 0)
21 {
22 return -1;
23 }
24 //TODO 长连接等
25
26 m_node_list.erase(zk_path);
27 return 0;
28 }
1 #ifndef _DISCOVERY_H_
2 #define _DISCOVERY_H_
3
4 #include "base_class.h"
5 #include "zk_handle.h"
6 #include "down_service_mgr.h"
7
8 #include
1 #include "discovery.h"
2 #include "auto_lock.h"
3
4 #include
1 #include "../zk_util/zk_handle.h"
2 #include "../zk_util/discovery.h"
3
4 #include
1 INC_DIR:= ./ ../zk_util/ /usr/local/include/zookeeper/ /usr/local/include/json/
2 SRCS:= $(wildcard ./*cpp ../zk_util/*cpp)
3 OBJS:= $(patsubst %.cpp, %.o, $(SRCS))
4 LIBS:= -lpthread -lzookeeper_mt -ljsoncpp
5
6 CXX:= g++
7
8 CXXFLAGS:= -w -g -std=c++11 $(addprefix -I, $(INC_DIR)) $(LIBS) -Wl,-rpath="/usr/local/lib" -D _DEBUG_
9
10 EXE:= ../../bin/discovery_test
11
12 $(EXE):$(OBJS)
13 $(CXX) -o $(EXE) $(OBJS) $(CXXFLAGS)
14
15 clean:
16 rm -rf $(EXE)
17 rm -rf $(OBJS)
ccx@ccx:~/self_test/zookeeper/services_register_and_discovery/bin$ ./discovery_test
CZkHandle::ZkInit create register check thread succ.
CDiscovery::StartCheck create discovery check thread succ.CZkHandle::ResetZkHandle: connect to zk succ.
DownPathCheck =======================================================
CZkHandle::ZkWgetChildren get children for path=/zk_test1
CZkHandle::ZkInitWatchar: [type=-1] [state=3] [path=] [watcher_ctx=(nil)]
CZkHandle::ZkWgetChildren get children succ. children_num=0
CZkHandle::ZkWgetChildren get children for path=/zk_test2
CZkHandle::ZkWgetChildren get children succ. children_num=0
InvalidNodeCheck =======================================================
DebugPrintAllNode =======================================================
DebugPrintAllNode down_path=/zk_test1 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode invalid_node_list:
DebugPrintAllNode down_path=/zk_test2 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode invalid_node_list:
CZkHandle::ZkExists: [ret=0]
CZkHandle::ZkExists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=291] [child_num=5]
DownPathCheck =======================================================
InvalidNodeCheck =======================================================
DebugPrintAllNode =======================================================
DebugPrintAllNode down_path=/zk_test1 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode invalid_node_list:
DebugPrintAllNode down_path=/zk_test2 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode invalid_node_list:
CZkHandle::ZkExists: [ret=0]
CZkHandle::ZkExists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=291] [child_num=5]
DownPathCheck =======================================================
InvalidNodeCheck =======================================================
DebugPrintAllNode =======================================================
DownPathCheck =======================================================
InvalidNodeCheck =======================================================
DebugPrintAllNode =======================================================
DebugPrintAllNode down_path=/zk_test1 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode invalid_node_list:
DebugPrintAllNode down_path=/zk_test2 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode invalid_node_list:
CZkHandle::ZkExists: [ret=0]
CZkHandle::ZkExists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=291] [child_num=5]
OnPathChange path=/zk_test1 =======================================================
DownPathCheck =======================================================
CZkHandle::ZkWgetChildren get children for path=/zk_test1
CZkHandle::ZkWgetChildren get children succ. children_num=1
CZkHandle::ZkWgetChildren children_idx=0, children_name=register_test_1
InvalidNodeCheck =======================================================
CZkHandle::ZkWGetNodeInfo get node info for path=/zk_test1/register_test_1
CZkHandle::ZkWGetNodeInfo get node info for path=/zk_test1/register_test_1 succ. buffer=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test1] MODULE_NAME[register_test] MODULE_ID[1] MODULE_IDX[1]
CZkHandle::ZkWGetNodeInfo: [path=/zk_test1/register_test_1] [czxid=17179873794] [mzxid=17179873794] [version=0] [cversion=0] [child_num=0]
DebugPrintAllNode =======================================================
DebugPrintAllNode down_path=/zk_test1 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test1/register_test_1
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test1] MODULE_NAME[register_test] MODULE_ID[1] MODULE_IDX[1]
DebugPrintAllNode invalid_node_list:
DebugPrintAllNode down_path=/zk_test2 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode invalid_node_list:
CZkHandle::ZkExists: [ret=0]
CZkHandle::ZkExists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=291] [child_num=5]
DownPathCheck =======================================================
InvalidNodeCheck =======================================================
DebugPrintAllNode =======================================================
DebugPrintAllNode down_path=/zk_test1 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test1/register_test_1
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test1] MODULE_NAME[register_test] MODULE_ID[1] MODULE_IDX[1]
DebugPrintAllNode invalid_node_list:
DebugPrintAllNode down_path=/zk_test2 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode invalid_node_list:
CZkHandle::ZkExists: [ret=0]
CZkHandle::ZkExists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=291] [child_num=5]
DownPathCheck =======================================================
InvalidNodeCheck =======================================================
DebugPrintAllNode =======================================================
DebugPrintAllNode down_path=/zk_test1 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test1/register_test_1
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test1] MODULE_NAME[register_test] MODULE_ID[1] MODULE_IDX[1]
DebugPrintAllNode invalid_node_list:
DebugPrintAllNode down_path=/zk_test2 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode invalid_node_list:
CZkHandle::ZkExists: [ret=0]
CZkHandle::ZkExists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=291] [child_num=5]
OnPathChange path=/zk_test2 =======================================================
DownPathCheck =======================================================
CZkHandle::ZkWgetChildren get children for path=/zk_test2
CZkHandle::ZkWgetChildren get children succ. children_num=1
CZkHandle::ZkWgetChildren children_idx=0, children_name=register_test_20000000010
InvalidNodeCheck =======================================================
CZkHandle::ZkWGetNodeInfo get node info for path=/zk_test2/register_test_20000000010
CZkHandle::ZkWGetNodeInfo get node info for path=/zk_test2/register_test_20000000010 succ. buffer=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test2] MODULE_NAME[register_test] MODULE_ID[2] MODULE_IDX[2]
CZkHandle::ZkWGetNodeInfo: [path=/zk_test2/register_test_20000000010] [czxid=17179873868] [mzxid=17179873868] [version=0] [cversion=0] [child_num=0]
DebugPrintAllNode =======================================================
DebugPrintAllNode down_path=/zk_test1 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test1/register_test_1
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test1] MODULE_NAME[register_test] MODULE_ID[1] MODULE_IDX[1]
DebugPrintAllNode invalid_node_list:
DebugPrintAllNode down_path=/zk_test2 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test2/register_test_20000000010
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test2] MODULE_NAME[register_test] MODULE_ID[2] MODULE_IDX[2]
DebugPrintAllNode invalid_node_list:
CZkHandle::ZkExists: [ret=0]
CZkHandle::ZkExists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=291] [child_num=5]
DownPathCheck =======================================================
InvalidNodeCheck =======================================================
DebugPrintAllNode =======================================================
DebugPrintAllNode down_path=/zk_test1 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test1/register_test_1
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test1] MODULE_NAME[register_test] MODULE_ID[1] MODULE_IDX[1]
DebugPrintAllNode invalid_node_list:
DebugPrintAllNode down_path=/zk_test2 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test2/register_test_20000000010
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test2] MODULE_NAME[register_test] MODULE_ID[2] MODULE_IDX[2]
DebugPrintAllNode invalid_node_list:
CZkHandle::ZkExists: [ret=0]
CZkHandle::ZkExists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=291] [child_num=5]
OnPathChange path=/zk_test2 =======================================================
DownPathCheck =======================================================
CZkHandle::ZkWgetChildren get children for path=/zk_test2
CZkHandle::ZkWgetChildren get children succ. children_num=2
CZkHandle::ZkWgetChildren children_idx=0, children_name=register_test_20000000010
CZkHandle::ZkWgetChildren children_idx=1, children_name=register_test_20000000011
InvalidNodeCheck =======================================================
CZkHandle::ZkWGetNodeInfo get node info for path=/zk_test2/register_test_20000000010
CZkHandle::ZkWGetNodeInfo get node info for path=/zk_test2/register_test_20000000010 succ. buffer=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test2] MODULE_NAME[register_test] MODULE_ID[2] MODULE_IDX[2]
CZkHandle::ZkWGetNodeInfo: [path=/zk_test2/register_test_20000000010] [czxid=17179873868] [mzxid=17179873868] [version=0] [cversion=0] [child_num=0]
CZkHandle::ZkWGetNodeInfo get node info for path=/zk_test2/register_test_20000000011
CZkHandle::ZkWGetNodeInfo get node info for path=/zk_test2/register_test_20000000011 succ. buffer=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test2] MODULE_NAME[register_test] MODULE_ID[2] MODULE_IDX[2]
CZkHandle::ZkWGetNodeInfo: [path=/zk_test2/register_test_20000000011] [czxid=17179873873] [mzxid=17179873873] [version=0] [cversion=0] [child_num=0]
DebugPrintAllNode =======================================================
DebugPrintAllNode down_path=/zk_test1 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test1/register_test_1
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test1] MODULE_NAME[register_test] MODULE_ID[1] MODULE_IDX[1]
DebugPrintAllNode invalid_node_list:
DebugPrintAllNode down_path=/zk_test2 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test2/register_test_20000000010
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test2] MODULE_NAME[register_test] MODULE_ID[2] MODULE_IDX[2]
DebugPrintAllNode node=/zk_test2/register_test_20000000011
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test2] MODULE_NAME[register_test] MODULE_ID[2] MODULE_IDX[2]
DebugPrintAllNode invalid_node_list:
CZkHandle::ZkExists: [ret=0]
CZkHandle::ZkExists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=291] [child_num=5]
DownPathCheck =======================================================
InvalidNodeCheck =======================================================
DebugPrintAllNode =======================================================
DebugPrintAllNode down_path=/zk_test1 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test1/register_test_1
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test1] MODULE_NAME[register_test] MODULE_ID[1] MODULE_IDX[1]
DebugPrintAllNode invalid_node_list:
DebugPrintAllNode down_path=/zk_test2 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test2/register_test_20000000014
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test2] MODULE_NAME[register_test] MODULE_ID[2] MODULE_IDX[2]
DebugPrintAllNode node=/zk_test2/register_test_20000000015
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345] ZK_PATH[/zk_test2] MODULE_NAME[register_test] MODULE_ID[2] MODULE_IDX[2]
DebugPrintAllNode invalid_node_list:
CZkHandle::ZkExists: [ret=0]
CZkHandle::ZkExists: [path=/] [czxid=0] [mzxid=0] [version=0] [cversion=291] [child_num=5]
OnNodeChange node=/zk_test1/register_test_1 =======================================================
OnPathChange path=/zk_test1 =======================================================
DownPathCheck =======================================================
CZkHandle::ZkWgetChildren get children for path=/zk_test1
CZkHandle::ZkWgetChildren get children succ. children_num=0
InvalidNodeCheck =======================================================
CZkHandle::ZkWGetNodeInfo get node info for path=/zk_test1/register_test_1
CZkHandle::ZkWGetNodeInfo get node info for path=/zk_test1/register_test_1 fail. ret=-101
DebugPrintAllNode =======================================================
DebugPrintAllNode down_path=/zk_test1 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode invalid_node_list:
DebugPrintAllNode down_path=/zk_test2 is_full_node=1 --------------------------------------------
DebugPrintAllNode node_list:
DebugPrintAllNode node=/zk_test2/register_test_20000000014
DebugPrintAllNode info=IP[127.0.0.1] PORT[12345]
文章标题:zookeeper 实现一个简单的服务注册与发现(C++) 三:服务发现
文章链接:http://soscw.com/index.php/essay/39562.html