🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 5) 获取Route信息 ### 5.1 proto协议定义 ​ 获取Route信息,根据之前的架构图,可以看出来应该是Agent来获取,这里我们并没有实现Agent,所以用一个其他简单的客户端来完成单元测试。但是无论用什么,总需要一个传递数据,需要一定的消息协议,lars-dns也需要设置不同纤细的分发消息路由机制,所以我们需要先定义一些proto协议。 ​ 在`Lars/base`下创建`proto/`文件夹. > Lars/base/proto/lars.proto ```protobuf syntax = "proto3"; package lars; /* Lars系统的消息ID */ enum MessageId { ID_UNKNOW = 0; //proto3 enum第一个属性必须是0,用来占位 ID_GetRouteRequest = 1; //向DNS请求Route对应的关系的消息ID ID_GetRouteResponse = 2; //DNS回复的Route信息的消息ID } //一个管理的主机的信息 message HostInfo { int32 ip = 1; int32 port = 2; } //请求lars-dns route信息的消息内容 message GetRouteRequest { int32 modid = 1; int32 cmdid = 2; } //lars-dns 回复的route信息消息内容 message GetRouteResponse { int32 modid = 1; int32 cmdid = 2; repeated HostInfo host = 3; } ``` ​ 然后我们将proto文件编译成对应的C++文件, 我们还是提供一个脚本 > Lars/base/proto/build.sh ```bash #!/bin/bash # proto编译 protoc --cpp_out=. ./*.proto # 将全部的cc 文件 变成 cpp文件 oldsuffix="cc" newsuffix="cpp" dir=$(eval pwd) for file in $(ls $dir | grep .${oldsuffix}) do name=$(ls ${file} | cut -d. -f1,2) mv $file ${name}.${newsuffix} done echo "build proto file successd!" ``` ​ 因为protoc会自动生成cc后缀的文件,为了方便我们Makefile的编译,所以将cc文件改成cpp的。 ### 5.2 proto编译环境集成 ​ 现在我们将`lars-dns`的Makefile加入针对proto文件的编译 > Lars/lars_dns/Makefile ```makefile TARGET= bin/lars_dns CXX=g++ CFLAGS=-g -O2 -Wall -Wno-deprecated BASE=../base BASE_H=$(BASE)/include PROTO = $(BASE)/proto PROTO_H = $(BASE)/proto LARS_REACTOR=../lars_reactor LARS_REACTOR_H =$(LARS_REACTOR)/include LARS_REACTOR_LIB=$(LARS_REACTOR)/lib -llreactor MYSQL=$(BASE)/mysql-connector-c MYSQL_H=$(MYSQL)/include MYSQL_LIB=$(MYSQL)/lib/libmysqlclient.a OTHER_LIB = -lpthread -ldl -lprotobuf SRC= ./src INC= -I./include -I$(BASE_H) -I$(LARS_REACTOR_H) -I$(MYSQL_H) -I$(PROTO_H) LIB= $(MYSQL_LIB) -L$(LARS_REACTOR_LIB) $(OTHER_LIB) OBJS = $(addsuffix .o, $(basename $(wildcard $(SRC)/*.cpp))) OBJS += $(PROTO)/lars.pb.o $(TARGET): $(OBJS) mkdir -p bin $(CXX) $(CFLAGS) -o $(TARGET) $(OBJS) $(INC) $(LIB) %.o: %.cpp $(CXX) $(CFLAGS) -c -o $@ $< $(INC) .PHONY: clean clean: -rm -f src/*.o $(TARGET) ``` ​ 添加了两个部分一个`OBJS`增添一个`lars.pb.o`的依赖,然后再`OTHER_LIB`增加`-lprotobuf`动态库的连接。 ### 5.3 实现Route获取 ​ 接下来我们来实现针对`ID_GetRouteRequest`消息指令的业务处理. > lars_dns/src/dns_service.cpp ```c #include "lars_reactor.h" #include "dns_route.h" #include "lars.pb.h" void get_route(const char *data, uint32_t len, int msgid, net_connection *net_conn, void *user_data) { //1. 解析proto文件 lars::GetRouteRequest req; req.ParseFromArray(data, len); //2. 得到modid 和 cmdid int modid, cmdid; modid = req.modid(); cmdid = req.cmdid(); //3. 根据modid/cmdid 获取 host信息 host_set hosts = Route::instance()->get_hosts(modid, cmdid); //4. 将数据打包成protobuf lars::GetRouteResponse rsp; rsp.set_modid(modid); rsp.set_cmdid(cmdid); for (host_set_it it = hosts.begin(); it != hosts.end(); it ++) { uint64_t ip_port = *it; lars::HostInfo host; host.set_ip((uint32_t)(ip_port >> 32)); host.set_port((int)(ip_port)); rsp.add_host()->CopyFrom(host); } //5. 发送给客户端 std::string responseString; rsp.SerializeToString(&responseString); net_conn->send_message(responseString.c_str(), responseString.size(), lars::ID_GetRouteResponse) ; } int main(int argc, char **argv) { event_loop loop; //加载配置文件 config_file::setPath("conf/lars_dns.conf"); std::string ip = config_file::instance()->GetString("reactor", "ip", "0.0.0.0"); short port = config_file::instance()->GetNumber("reactor", "port", 7778); //创建tcp服务器 tcp_server *server = new tcp_server(&loop, ip.c_str(), port); //注册路由业务 server->add_msg_router(lars::ID_GetRouteRequest, get_route); //开始事件监听 printf("lars dns service ....\n"); loop.event_process(); return 0; } ``` ​ 需要给`Route`类,实现一个get_host()方法,来针对modid/cmdid取出对应的value > lars_dns/src/dns_route.cpp ```c //获取modid/cmdid对应的host信息 host_set Route::get_hosts(int modid, int cmdid) { host_set hosts; //组装key uint64_t key = ((uint64_t)modid << 32) + cmdid; pthread_rwlock_rdlock(&_map_lock); route_map_it it = _data_pointer->find(key); if (it != _data_pointer->end()) { //找到对应的ip + port对 hosts = it->second; } pthread_rwlock_unlock(&_map_lock); return hosts; } ``` ### 5.3 lars_dns获取Route信息测试-V0.2 ​ 下面我们写一个客户端检查测试一下该功能. > lars_dns/test/lars_dns_test1.cpp ```c #include <string.h> #include <unistd.h> #include <string> #include "lars_reactor.h" #include "lars.pb.h" //命令行参数 struct Option { Option():ip(NULL),port(0) {} char *ip; short port; }; Option option; void Usage() { printf("Usage: ./lars_dns_test -h ip -p port\n"); } //解析命令行 void parse_option(int argc, char **argv) { for (int i = 0; i < argc; i++) { if (strcmp(argv[i], "-h") == 0) { option.ip = argv[i + 1]; } else if (strcmp(argv[i], "-p") == 0) { option.port = atoi(argv[i + 1]); } } if ( !option.ip || !option.port ) { Usage(); exit(1); } } //typedef void (*conn_callback)(net_connection *conn, void *args); void on_connection(net_connection *conn, void *args) { //发送Route信息请求 lars::GetRouteRequest req; req.set_modid(1); req.set_cmdid(2); std::string requestString; req.SerializeToString(&requestString); conn->send_message(requestString.c_str(), requestString.size(), lars::ID_GetRouteRequest); } void deal_get_route(const char *data, uint32_t len, int msgid, net_connection *net_conn, void *user_data) { //解包得到数据 lars::GetRouteResponse rsp; rsp.ParseFromArray(data, len); //打印数据 printf("modid = %d\n", rsp.modid()); printf("cmdid = %d\n", rsp.cmdid()); printf("host_size = %d\n", rsp.host_size()); for (int i = 0; i < rsp.host_size(); i++) { printf("-->ip = %u\n", rsp.host(i).ip()); printf("-->port = %d\n", rsp.host(i).port()); } //再请求 lars::GetRouteRequest req; req.set_modid(rsp.modid()); req.set_cmdid(rsp.cmdid()); std::string requestString; req.SerializeToString(&requestString); net_conn->send_message(requestString.c_str(), requestString.size(), lars::ID_GetRouteRequest); } int main(int argc, char **argv) { parse_option(argc, argv); event_loop loop; tcp_client *client; //创建客户端 client = new tcp_client(&loop, option.ip, option.port, "lars_dns_test"); if (client == NULL) { fprintf(stderr, "client == NULL\n"); exit(1); } //客户端成功建立连接,首先发送请求包 client->set_conn_start(on_connection); //设置服务端回应包处理业务 client->add_msg_router(lars::ID_GetRouteResponse, deal_get_route); loop.event_process(); return 0; } ``` ​ ​ 同时提供一个`Makefile`对客户端进行编译。 > lars_dns/test/Makefile ```makefile TARGET= lars_dns_test1 CXX=g++ CFLAGS=-g -O2 -Wall -Wno-deprecated BASE=../../base BASE_H=$(BASE)/include PROTO = $(BASE)/proto PROTO_H = $(BASE)/proto LARS_REACTOR=../../lars_reactor LARS_REACTOR_H =$(LARS_REACTOR)/include LARS_REACTOR_LIB=$(LARS_REACTOR)/lib -llreactor OTHER_LIB = -lpthread -ldl -lprotobuf SRC= ./src INC= -I./include -I$(BASE_H) -I$(LARS_REACTOR_H) -I$(PROTO_H) LIB= $(MYSQL_LIB) -L$(LARS_REACTOR_LIB) $(OTHER_LIB) OBJS = lars_dns_test1.o OBJS += $(PROTO)/lars.pb.o $(TARGET): $(OBJS) $(CXX) $(CFLAGS) -o $(TARGET) $(OBJS) $(INC) $(LIB) %.o: %.cpp $(CXX) $(CFLAGS) -c -o $@ $< $(INC) .PHONY: clean clean: -rm -f ./*.o $(TARGET) ``` 我们分别启动`bin/lars_dns`进程,和`test/lars_dns_test1`进程 服务端 ```bash $ ./bin/lars_dns msg_router init... create 0 thread create 1 thread create 2 thread create 3 thread create 4 thread add msg cb msgid = 1 lars dns service .... begin accept begin accept [thread]: get new connection succ! modID = 1, cmdID = 1, ip = 3232235953, port = 7777 modID = 1, cmdID = 2, ip = 3232235954, port = 7776 modID = 2, cmdID = 1, ip = 3232235955, port = 7778 modID = 2, cmdID = 2, ip = 3232235956, port = 7779 modID = 1, cmdID = 1, ip = 3232235953, port = 7777 modID = 1, cmdID = 2, ip = 3232235954, port = 7776 modID = 2, cmdID = 1, ip = 3232235955, port = 7778 modID = 2, cmdID = 2, ip = 3232235956, port = 7779 read data from socket ``` 客户端 ```bash $ ./lars_dns_test1 -h 127.0.0.1 -p 7778 msg_router init... do_connect EINPROGRESS add msg cb msgid = 2 connect 127.0.0.1:7778 succ! modid = 1 cmdid = 2 host_size = 1 -->ip = 3232235954 -->port = 7776 ``` --- ### 关于作者: 作者:`Aceld(刘丹冰)` mail: [danbing.at@gmail.com](mailto:danbing.at@gmail.com) github: [https://github.com/aceld](https://github.com/aceld) 原创书籍: [https://www.kancloud.cn/@aceld](https://www.kancloud.cn/@aceld) ![](https://img.kancloud.cn/b0/d1/b0d11a21ba62e96aef1c11d5bfff2cf8_227x227.jpg) >**原创声明:未经作者允许请勿转载, 如果转载请注明出处**