24.1 简介

负责接收各agent对某modid、cmdid下节点的调用状态的上报。

目前agent我们还没有实现,就可以把agent当成一个普通客户端就可以。

agent会把代理的host节点的状态上报给Reporter,Reporter负责存储在Mysql数据库中。

业务:

Reporter服务模型采用了single thread TCP服务器 + 线程池处理请求

  • 主线程Reporter负责接收agent请求,并根据请求中携带的modidcmdid,拼接后进行murmurHash(一致性hash),分配到某个线程的MQ上
  • Thread 1~N们负责处理请求:把MQ上的请求中的数据同步更新到MySQL数据表
    由于agent上报给Reporter的信息是携带时间的,且仅作为前台展示方便查看服务的过载情况,故通信仅有请求没有响应

于是Reporter服务只要可以高效读取请求即可,后端写数据库的实时性能要求不高。

架构图如下:

6-Lars-reporter.png

24.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

  1. DROP TABLE IF EXISTS `ServerCallStatus`;
  2. CREATE TABLE `ServerCallStatus` (
  3. `modid` int(11) NOT NULL,
  4. `cmdid` int(11) NOT NULL,
  5. `ip` int(11) NOT NULL,
  6. `port` int(11) NOT NULL,
  7. `caller` int(11) NOT NULL,
  8. `succ_cnt` int(11) NOT NULL,
  9. `err_cnt` int(11) NOT NULL,
  10. `ts` bigint(20) NOT NULL,
  11. `overload` char(1) NOT NULL,
  12. PRIMARY KEY (`modid`,`cmdid`,`ip`,`port`,`caller`),
  13. KEY `mlb_index` (`modid`,`cmdid`,`ip`,`port`,`caller`)
  14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

24.3 Proto协议定义

Lars/base/proto/lars.proto

  1. syntax = "proto3";
  2. package lars;
  3. /* Lars系统的消息ID */
  4. enum MessageId {
  5. ID_UNKNOW = 0; //proto3 enum第一个属性必须是0,用来占位
  6. ID_GetRouteRequest = 1; //向DNS请求Route对应的关系的消息ID
  7. ID_GetRouteResponse = 2; //DNS回复的Route信息的消息ID
  8. ID_ReportStatusRequest = 3; //上报host调用状态信息请求消息ID
  9. }
  10. //一个管理的主机的信息
  11. message HostInfo {
  12. int32 ip = 1;
  13. int32 port = 2;
  14. }
  15. //请求lars-dns route信息的消息内容
  16. message GetRouteRequest {
  17. int32 modid = 1;
  18. int32 cmdid = 2;
  19. }
  20. //lars-dns 回复的route信息消息内容
  21. message GetRouteResponse {
  22. int32 modid = 1;
  23. int32 cmdid = 2;
  24. repeated HostInfo host = 3;
  25. }
  26. message HostCallResult {
  27. int32 ip = 1; //主机ip
  28. int32 port = 2; //主机端口
  29. uint32 succ = 3; //调度成功
  30. uint32 err = 4; //调度失败
  31. bool overload = 5; //是否过载
  32. }
  33. //上报 负载均衡 调度数据 给 lars_reporter 的消息内容
  34. message ReportStatusReq {
  35. int32 modid = 1;
  36. int32 cmdid = 2;
  37. int32 caller = 3;
  38. repeated HostCallResult results = 4;
  39. uint32 ts = 5;
  40. }