💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 四、Lars-Reporter Service开发 ## 1) 简介 负责接收各agent对某modid、cmdid下节点的调用状态的上报。 > 目前`agent`我们还没有实现,就可以把agent当成一个普通客户端就可以。 agent会把代理的host节点的状态上报给Reporter,Reporter负责存储在Mysql数据库中。 **业务**: Reporter服务模型采用了single thread TCP服务器 + 线程池处理请求 * 主线程Reporter负责接收agent请求,并根据请求中携带的`modid`和`cmdid`,拼接后进行`murmurHash`(一致性hash),分配到某个线程的MQ上 * Thread 1~N们负责处理请求:把MQ上的请求中的数据同步更新到MySQL数据表 由于agent上报给Reporter的信息是携带时间的,且仅作为前台展示方便查看服务的过载情况,故通信仅有请求没有响应 于是Reporter服务只要可以高效读取请求即可,后端写数据库的实时性能要求不高。 架构图如下: ![](https://img.kancloud.cn/a6/1b/a61b823c490255dc61c5d290fa8cd39f_982x661.png) ## 2) 数据库创建 - 表`ServerCallStatus`: | 字段 | 数据类型 | 是否可以为空 | 主键 | 默认 | 附加 | 说明 | | -------- | ---------- | ------------ | ---- | ---- | ---- | ------------ | | modid | int(11) | No | 是 | NULL | | 模块ID | | modid | int(11) | No | 是 | NULL | | 指令ID | | ip | int(11) | No | 是 | NULL | | 服务器IP地址 | | port | int(11) | No | 是 | NULL | | 服务器端口 | | caller | int(11) | No | 是 | NULL | | 调用者 | | succ_cnt | int(11) | No | | NULL | | 成功次数 | | err_cnt | int(11) | No | | NULL | | 失败次数 | | ts | bigint(20) | No | | NULL | | 记录时间 | | overload | char(1) | No | | NULL | | 是否过载 | > Lars/base/sql/lars_dns.sql ```sql DROP TABLE IF EXISTS `ServerCallStatus`; CREATE TABLE `ServerCallStatus` ( `modid` int(11) NOT NULL, `cmdid` int(11) NOT NULL, `ip` int(11) NOT NULL, `port` int(11) NOT NULL, `caller` int(11) NOT NULL, `succ_cnt` int(11) NOT NULL, `err_cnt` int(11) NOT NULL, `ts` bigint(20) NOT NULL, `overload` char(1) NOT NULL, PRIMARY KEY (`modid`,`cmdid`,`ip`,`port`,`caller`), KEY `mlb_index` (`modid`,`cmdid`,`ip`,`port`,`caller`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` ## 3) 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 ID_ReportStatusRequest = 3; //上报host调用状态信息请求消息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; } message HostCallResult { int32 ip = 1; //主机ip int32 port = 2; //主机端口 uint32 succ = 3; //调度成功 uint32 err = 4; //调度失败 bool overload = 5; //是否过载 } //上报 负载均衡 调度数据 给 lars_reporter 的消息内容 message ReportStatusReq { int32 modid = 1; int32 cmdid = 2; int32 caller = 3; repeated HostCallResult results = 4; uint32 ts = 5; } ``` --- ### 关于作者: 作者:`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) >**原创声明:未经作者允许请勿转载, 如果转载请注明出处**