(29)主模塊業(yè)務(wù)結(jié)構(gòu)搭建(LoadBalanceAgent部分)-【Lars-基于C++負(fù)載均衡遠(yuǎn)程服務(wù)器調(diào)度系統(tǒng)教程】

【Lars教程目錄】

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


【Lars系統(tǒng)概述】
第1章-概述
第2章-項目目錄構(gòu)建


【Lars系統(tǒng)之Reactor模型服務(wù)器框架模塊】
第1章-項目結(jié)構(gòu)與V0.1雛形
第2章-內(nèi)存管理與Buffer封裝
第3章-事件觸發(fā)EventLoop
第4章-鏈接與消息封裝
第5章-Client客戶端模型
第6章-連接管理及限制
第7章-消息業(yè)務(wù)路由分發(fā)機制
第8章-鏈接創(chuàng)建/銷毀Hook機制
第9章-消息任務(wù)隊列與線程池
第10章-配置文件讀寫功能
第11章-udp服務(wù)與客戶端
第12章-數(shù)據(jù)傳輸協(xié)議protocol buffer
第13章-QPS性能測試
第14章-異步消息任務(wù)機制
第15章-鏈接屬性設(shè)置功能


【Lars系統(tǒng)之DNSService模塊】
第1章-Lars-dns簡介
第2章-數(shù)據(jù)庫創(chuàng)建
第3章-項目目錄結(jié)構(gòu)及環(huán)境構(gòu)建
第4章-Route結(jié)構(gòu)的定義
第5章-獲取Route信息
第6章-Route訂閱模式
第7章-Backend Thread實時監(jiān)控


【Lars系統(tǒng)之Report Service模塊】
第1章-項目概述-數(shù)據(jù)表及proto3協(xié)議定義
第2章-獲取report上報數(shù)據(jù)
第3章-存儲線程池及消息隊列


【Lars系統(tǒng)之LoadBalance Agent模塊】
第1章-項目概述及構(gòu)建
第2章-主模塊業(yè)務(wù)結(jié)構(gòu)搭建
第3章-Report與Dns Client設(shè)計與實現(xiàn)
第4章-負(fù)載均衡模塊基礎(chǔ)設(shè)計
第5章-負(fù)載均衡獲取Host主機信息API
第6章-負(fù)載均衡上報Host主機信息API
第7章-過期窗口清理與過載超時(V0.5)
第8章-定期拉取最新路由信息(V0.6)
第9章-負(fù)載均衡獲取Route信息API(0.7)
第10章-API初始化接口(V0.8)
第11章-Lars Agent性能測試工具
第12章- Lars啟動工具腳本


19-lars-agent_1.png

首先我們要在主線程中,啟動3個UDP Server線程,這個是提供業(yè)務(wù)層/API層的服務(wù)。然后分別啟動report_client線程,用來和reporter Service進行通信,將請求上報信息發(fā)送給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服務(wù),用來接收業(yè)務(wù)層(調(diào)用者/使用者)的消息
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服務(wù),用來接收業(yè)務(wù)層(調(diào)用者/使用者)的消息
    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負(fù)責(zé)將上報請求消息lars::ReportStatusRequest通過thread_queue發(fā)送給reporter service。

? 其中dns_client線程需要攜帶thread_queue<lars::GetRouteRequest>。agent負(fù)責(zé)將請求modid/cmdid的route消息lars::GetRouteRequest通過thread_queue發(fā)送給dns service。

3個udp server的線程開辟實現(xiàn)如下:

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注冊消息分發(fā)路由業(yè)務(wù)

    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創(chuàng)建實現(xiàn)如下:

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 創(chuàng)建客戶端
    tcp_client client(&loop, ip.c_str(), port, "reporter client");

    //3 將 thread_queue消息回調(diào)事件,綁定到loop中
    report_queue->set_loop(&loop);
    report_queue->set_callback()

    //4 啟動事件監(jiān)聽
    loop.event_process();
#endif

    return NULL;
}

void start_report_client()
{
    //開辟一個線程
    pthread_t tid;

    //啟動線程業(yè)務(wù)函數(shù)
    int ret = pthread_create(&tid, NULL, report_client_thread, NULL);
    if (ret == -1) {
        perror("pthread_create");
        exit(1);
    }

    //設(shè)置分離模式
    pthread_detach(tid);
}

dns thread創(chuàng)建實現(xiàn)如下:

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;

    //啟動線程業(yè)務(wù)函數(shù)
    int ret = pthread_create(&tid, NULL, dns_client_thread, NULL);
    if (ret == -1) {
        perror("pthread_create");
        exit(1);
    }

    //設(shè)置分離模式
    pthread_detach(tid);
}

2.3 測試lb_agentV0.1開發(fā)

編譯,然后我們簡單啟動一下./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...
...

關(guān)于作者:

作者:Aceld(劉丹冰)

mail: danbing.at@gmail.com
github: https://github.com/aceld
原創(chuàng)書籍gitbook: http://legacy.gitbook.com/@aceld

原創(chuàng)聲明:未經(jīng)作者允許請勿轉(zhuǎn)載, 如果轉(zhuǎn)載請注明出處

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容