## 3) Report Client設計與實現
? report client主要是實現thread_queue的回調業務,udp server會定期的上傳上報數據到reporter,那么請求對于report client就是透傳給reporter serivce即可。
> lars_loadbalance_agent/src/reporter_client.cpp
```c
#include "lars_reactor.h"
#include "main_server.h"
#include <string>
#include <pthread.h>
//typedef void io_callback(event_loop *loop, int fd, void *args);
//只要thread_queue有數據,loop就會觸發此回調函數來處理業務
void new_report_request(event_loop *loop, int fd, void *args)
{
tcp_client *client = (tcp_client*)args;
//1. 將請求數據從thread_queue中取出,
std::queue<lars::ReportStatusRequest> msgs;
//2. 將數據放在queue隊列中
report_queue->recv(msgs);
//3. 遍歷隊列,通過client依次將每個msg發送給reporter service
while (!msgs.empty()) {
lars::ReportStatusRequest req = msgs.front();
msgs.pop();
std::string requestString;
req.SerializeToString(&requestString);
//client 發送數據
client->send_message(requestString.c_str(), requestString.size(), lars::ID_ReportStatusRequest);
}
}
void *report_client_thread(void* args)
{
printf("report client thread start\n");
event_loop loop;
//1 加載配置文件得到repoter ip + port
std::string ip = config_file::instance()->GetString("reporter", "ip", "");
short port = config_file::instance()->GetNumber("reporter", "port", 0);
//2 創建客戶端
tcp_client client(&loop, ip.c_str(), port, "reporter client");
//3 將 thread_queue消息回調事件,綁定到loop中
report_queue->set_loop(&loop);
report_queue->set_callback(new_report_request, &client);
//4 啟動事件監聽
loop.event_process();
return NULL;
}
void start_report_client()
{
//開辟一個線程
pthread_t tid;
//啟動線程業務函數
int ret = pthread_create(&tid, NULL, report_client_thread, NULL);
if (ret == -1) {
perror("pthread_create");
exit(1);
}
//設置分離模式
pthread_detach(tid);
}
```
## 4) Dns Client設計與實現
? dns client 和report client的業務十分相似,只是針對的協議不同了。dns client的thread_queue 回調業務主要是透傳`lars::GetRouteRequest`數據包。
> lars_loadbalance_agent/src/dns_client.cpp
```c
#include "lars_reactor.h"
#include "main_server.h"
#include <pthread.h>
//typedef void io_callback(event_loop *loop, int fd, void *args);
//只要thread_queue有數據,loop就會觸發此回調函數來處理業務
void new_dns_request(event_loop *loop, int fd, void *args)
{
tcp_client *client = (tcp_client*)args;
//1. 將請求數據從thread_queue中取出,
std::queue<lars::GetRouteRequest> msgs;
//2. 將數據放在queue隊列中
dns_queue->recv(msgs);
//3. 遍歷隊列,通過client依次將每個msg發送給reporter service
while (!msgs.empty()) {
lars::GetRouteRequest req = msgs.front();
msgs.pop();
std::string requestString;
req.SerializeToString(&requestString);
//client 發送數據
client->send_message(requestString.c_str(), requestString.size(), lars::ID_GetRouteRequest);
}
}
void *dns_client_thread(void* args)
{
printf("dns client thread start\n");
event_loop loop;
//1 加載配置文件得到dns service ip + port
std::string ip = config_file::instance()->GetString("dnsserver", "ip", "");
short port = config_file::instance()->GetNumber("dnsserver", "port", 0);
//2 創建客戶端
tcp_client client(&loop, ip.c_str(), port, "dns client");
//3 將thread_queue消息回調事件,綁定到loop中
dns_queue->set_loop(&loop);
dns_queue->set_callback(new_dns_request, &client);
//4 啟動事件監聽
loop.event_process();
return NULL;
}
void start_dns_client()
{
//開辟一個線程
pthread_t tid;
//啟動線程業務函數
int ret = pthread_create(&tid, NULL, dns_client_thread, NULL);
if (ret == -1) {
perror("pthread_create");
exit(1);
}
//設置分離模式
pthread_detach(tid);
}
```
---
### 關于作者:
作者:`Aceld(劉丹冰)`
mail: [danbing.at@gmail.com](mailto:danbing.at@gmail.com)
github: [https://github.com/aceld](https://github.com/aceld)
原創書籍: [http://www.hmoore.net/@aceld](http://www.hmoore.net/@aceld)

>**原創聲明:未經作者允許請勿轉載, 如果轉載請注明出處**
- 一、Lars系統概述
- 第1章-概述
- 第2章-項目目錄構建
- 二、Reactor模型服務器框架
- 第1章-項目結構與V0.1雛形
- 第2章-內存管理與Buffer封裝
- 第3章-事件觸發EventLoop
- 第4章-鏈接與消息封裝
- 第5章-Client客戶端模型
- 第6章-連接管理及限制
- 第7章-消息業務路由分發機制
- 第8章-鏈接創建/銷毀Hook機制
- 第9章-消息任務隊列與線程池
- 第10章-配置文件讀寫功能
- 第11章-udp服務與客戶端
- 第12章-數據傳輸協議protocol buffer
- 第13章-QPS性能測試
- 第14章-異步消息任務機制
- 第15章-鏈接屬性設置功能
- 三、Lars系統之DNSService
- 第1章-Lars-dns簡介
- 第2章-數據庫創建
- 第3章-項目目錄結構及環境構建
- 第4章-Route結構的定義
- 第5章-獲取Route信息
- 第6章-Route訂閱模式
- 第7章-Backend Thread實時監控
- 四、Lars系統之Report Service
- 第1章-項目概述-數據表及proto3協議定義
- 第2章-獲取report上報數據
- 第3章-存儲線程池及消息隊列
- 五、Lars系統之LoadBalance Agent
- 第1章-項目概述及構建
- 第2章-主模塊業務結構搭建
- 第3章-Report與Dns Client設計與實現
- 第4章-負載均衡模塊基礎設計
- 第5章-負載均衡獲取Host主機信息API
- 第6章-負載均衡上報Host主機信息API
- 第7章-過期窗口清理與過載超時(V0.5)
- 第8章-定期拉取最新路由信息(V0.6)
- 第9章-負載均衡獲取Route信息API(0.7)
- 第10章-API初始化接口(V0.8)
- 第11章-Lars Agent性能測試工具
- 第12章- Lars啟動工具腳本