美文网首页
(29)主模块业务结构搭建(LoadBalanceAgent部分

(29)主模块业务结构搭建(LoadBalanceAgent部分

作者: 刘丹冰Aceld | 来源:发表于2019-10-23 09:44 被阅读0次

【Lars教程目录】

Lars源代码
https://github.com/aceld/Lars


【Lars系统概述】
第1章-概述
第2章-项目目录构建


【Lars系统之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启动工具脚本


19-lars-agent_1.png

首先我们要在主线程中,启动3个UDP Server线程,这个是提供业务层/API层的服务。然后分别启动report_client线程,用来和reporter Service进行通信,将请求上报信息发送给Reporter Service。 然后再启动dns_client线程,用来和dns service通信。

lars_loadbalance_agent/include/main_server.h

#pragma once
#include "lars_reactor.h"
#include "lars.pb.h"


//与report_client通信的thread_queue消息队列
extern thread_queue<lars::ReportStatusRequest>* report_queue;
//与dns_client通信的thread_queue消息队列
extern thread_queue<lars::GetRouteRequest>* dns_queue;


// 启动udp server服务,用来接收业务层(调用者/使用者)的消息
void start_UDP_servers(void);

// 启动lars_reporter client 线程
void start_report_client(void);

// 启动lars_dns client 线程
void start_dns_client(void);

lars_loadbalance_agent/src/main_server.cpp

#include "main_server.h"
#include "lars.pb.h"

//与report_client通信的thread_queue消息队列
thread_queue<lars::ReportStatusRequest>* report_queue = NULL;
//与dns_client通信的thread_queue消息队列
thread_queue<lars::GetRouteRequest>* dns_queue = NULL;

int main(int argc, char **argv)
{
    //1 加载配置文件
    
    //2 启动udp server服务,用来接收业务层(调用者/使用者)的消息
    start_UDP_servers();
    
    //3 启动lars_reporter client 线程
    report_queue = new thread_queue<lars::ReportStatusRequest>();
    if (report_queue == NULL) {
        fprintf(stderr, "create report queue error!\n");
        exit(1);
    }
    start_report_client();
    
    //4 启动lars_dns client 线程
    dns_queue = new thread_queue<lars::GetRouteRequest>();
    if (dns_queue == NULL) {
        fprintf(stderr, "create dns queue error!\n");
        exit(1);
    }
    start_dns_client();
    

    std::cout <<"done!" <<std::endl;
    while (1) {
        sleep(10);
    }

    return 0;
}

这里我们分别在main()中 ,开启以上线程。

​ 其中report_client线程需要携带thread_queue<lars::ReportStatusRequest>消息队列通道。agent负责将上报请求消息lars::ReportStatusRequest通过thread_queue发送给reporter service。

​ 其中dns_client线程需要携带thread_queue<lars::GetRouteRequest>agent负责将请求modid/cmdid的route消息lars::GetRouteRequest通过thread_queue发送给dns service。

3个udp server的线程开辟实现如下:

lars_loadbalance_agent/src/agent_udp_server.cpp

#include "lars_reactor.h"
#include "main_server.h"


void * agent_server_main(void * args)
{
    int *index = (int*)args;
    short port = *index + 8888;
    event_loop loop;

    udp_server server(&loop, "0.0.0.0", port);

    //TODO 给server注册消息分发路由业务

    printf("agent UDP server :port %d is started...\n", port);
    loop.event_process();

    return NULL;
}

void start_UDP_servers(void)
{
    for (int i = 0; i < 3; i ++) {
        pthread_t tid;
        
        int ret = pthread_create(&tid, NULL, agent_server_main, &i);
        if (ret == -1) {
            perror("pthread_create");
            exit(1);
        }

        pthread_detach(tid);

    }
}

reporter thread创建实现如下:

lars_loadbalance_agent/src/reporter_client.cpp

#include "lars_reactor.h"
#include "main_server.h"
#include <pthread.h>

void *report_client_thread(void* args)
{
    printf("report client thread start\n");
#if 0
    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()

    //4 启动事件监听
    loop.event_process();
#endif

    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);
}

dns thread创建实现如下:

lars_loadbalance_agent/src/dns_client.cpp

#include "lars_reactor.h"
#include "main_server.h"
#include <pthread.h>

void *dns_client_thread(void* args)
{
    printf("dns client thread start\n");

    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);
}

2.3 测试lb_agentV0.1开发

编译,然后我们简单启动一下./bin/lars_lb_agent

$ ./bin/lars_lb_agent 
dns client thread start
report client thread start
done!
msg_router init...
server on 0.0.0.0:8888 is running...
agent UDP server :port 8888 is started...
msg_router init...
server on 0.0.0.0:8888 is running...
agent UDP server :port 8888 is started...
msg_router init...
server on 0.0.0.0:8888 is running...
agent UDP server :port 8888 is started...
...

关于作者:

作者:Aceld(刘丹冰)

mail: danbing.at@gmail.com
github: https://github.com/aceld
原创书籍gitbook: http://legacy.gitbook.com/@aceld

原创声明:未经作者允许请勿转载, 如果转载请注明出处

相关文章

  • (29)主模块业务结构搭建(LoadBalanceAgent部分

    【Lars教程目录】 Lars源代码[https://github.com/aceld/Lars]https://...

  • Spring Security项目构建(一)

    源码地址 Github 项目构建 依赖关系 代码结构 security:主模块security-core:核心业务...

  • 已有项目模块化流程

    1 根据业务拆分模块,找出所有的业务模块。复杂的业务模块拆分为小的业务模块。2 根据模块的层级结构已文件夹...

  • 2019-04-02

    导航模块架构 TraceNavigate发还给业务模块GTraceList数据结构中,GTraceList/GTr...

  • Flask----图书管理系统拆解

    一 分析项目结构 1.图书管理框架搭建 所示图是图书管理系统整体功能结构图,每个部分也就是一个模块。系统由图上所示...

  • 严选 Android 组件化实践

    0 背景 早前严选 Android 工程,业务模块和功能模块不多,工程较为简单,全部的业务代码均在主 app 工程...

  • SAP-MM模块业务

    业务操作详解与详细流程。您就是业务之神? 物料主数据_销售视图 MM模块相关透明表收集 SAP MM模块相关透明表...

  • 财务应付结算系统设计-总览

    1. 领域说明 应付结算系统承担了公司供应链结算、对账、付款、发票管理以及部分收款业务,是财务领域核心模块之一,主...

  • 大纲

    目录结构 1.框架基础搭建2.框架目录结构3.模块整合4 登陆认证模块配置5.Vue开发问题整理 自述 最近时间相...

  • 六天完成一个简单iOS App - 第二天

    第二天任务: 项目主框架搭建完毕后,就可以从各个模块入手完成项目,这里从最简单的关注模块开始。 关注页面的搭建 登...

网友评论

      本文标题:(29)主模块业务结构搭建(LoadBalanceAgent部分

      本文链接:https://www.haomeiwen.com/subject/bjyhvctx.html