自学内容网 自学内容网

【聊天室后端服务器开发】消息存储子服务测试

数据管理操作测试

插入测试

void insert_test(mag::MessageTable &tb) {
    mag::Message m1("消息ID1", "会话ID1", "用户ID1", 0, boost::posix_time::time_from_string("2002-01-20 23:59:59.000"));
    tb.insert(m1);
    mag::Message m2("消息ID2", "会话ID1", "用户ID2", 0, boost::posix_time::time_from_string("2002-01-21 23:59:59.000"));
    tb.insert(m2);
    mag::Message m3("消息ID3", "会话ID1", "用户ID3", 0, boost::posix_time::time_from_string("2002-01-22 23:59:59.000"));
    tb.insert(m3);

    mag::Message m4("消息ID4", "会话ID2", "用户ID4", 0, boost::posix_time::time_from_string("2002-01-20 23:59:59.000"));
    tb.insert(m4);
    mag::Message m5("消息ID5", "会话ID2", "用户ID5", 0, boost::posix_time::time_from_string("2002-01-21 23:59:59.000"));
    tb.insert(m5);
}

移除测试

void remove_test(mag::MessageTable &tb) {
    tb.remove("会话ID2");
}

获取最近消息测试

void recent_test(mag::MessageTable &tb) {
    auto res = tb.recent("会话ID1", 2);
    auto begin = res.rbegin();
    auto end = res.rend();
    for (; begin != end; ++begin) {
        std::cout << begin->message_id() << std::endl;
        std::cout << begin->session_id() << std::endl;
        std::cout << begin->user_id() << std::endl;
        std::cout << boost::posix_time::to_simple_string(begin->create_time()) << std::endl;
    }
}

获取指定范围消息测试

void range_test(mag::MessageTable &tb) {
    boost::posix_time::ptime stime(boost::posix_time::time_from_string("2002-01-20 23:59:59.000"));
    boost::posix_time::ptime etime(boost::posix_time::time_from_string("2002-01-21 23:59:59.000"));
    auto res = tb.range("会话ID1", stime, etime);
    for (const auto &m : res) {
        std::cout << m.message_id() << std::endl;
        std::cout << m.session_id() << std::endl;
        std::cout << m.user_id() << std::endl;
        std::cout << boost::posix_time::to_simple_string(m.create_time()) << std::endl;
    }
}

ES操作封装测试

root@hcss-ecs-b4a9:/home/chatServer/chatServer/src/server/message/test/es_test# ./main
[default-logger][10:43:43][1353793][debug   ][../../../common/icsearch.hpp:123] {
"mappings" : 
{
"dynamic" : true,
"properties" : 
{
"chat_session_id" : 
{
"analyzer" : "standard",
"type" : "keyword"
},
"content" : 
{
"analyzer" : "ik_max_word",
"type" : "text"
},
"create_time" : 
{
"analyzer" : "standard",
"enabled" : false,
"type" : "long"
},
"message_id" : 
{
"analyzer" : "standard",
"enabled" : false,
"type" : "keyword"
},
"user_id" : 
{
"analyzer" : "standard",
"enabled" : false,
"type" : "keyword"
}
}
},
"settings" : 
{
"analysis" : 
{
"analyzer" : 
{
"ik" : 
{
"tokenizer" : "ik_max_word"
}
}
}
}
}
[default-logger][10:43:49][1353793][error   ][../../../common/icsearch.hpp:132] 创建ES索引 message 失败: All hosts failed for request.
[default-logger][10:43:49][1353793][info    ][../../../common/data_es.hpp:128] 消息信息索引创建失败!
[default-logger][10:43:49][1353793][error   ][main.cc:36] 索引创建测试失败。
[default-logger][10:43:49][1353793][debug   ][../../../common/icsearch.hpp:192] {
"chat_session_id" : "\u4f1a\u8bddID1",
"content" : "\u5403\u996d\u4e86\u5417\uff1f",
"create_time" : 1723025035,
"message_id" : "\u6d88\u606fID1",
"user_id" : "\u7528\u6237ID1"
}
[default-logger][10:43:50][1353793][info    ][../../../common/data_es.hpp:153] 消息数据新增/更新成功!
[default-logger][10:43:50][1353793][info    ][main.cc:60] 插入消息 消息ID1 成功。
[default-logger][10:43:50][1353793][debug   ][../../../common/icsearch.hpp:192] {
"chat_session_id" : "\u4f1a\u8bddID1",
"content" : "\u5403\u7684\u76d6\u6d47\u996d\uff01",
"create_time" : 1723024935,
"message_id" : "\u6d88\u606fID2",
"user_id" : "\u7528\u6237ID2"
}
[default-logger][10:43:50][1353793][info    ][../../../common/data_es.hpp:153] 消息数据新增/更新成功!
[default-logger][10:43:50][1353793][info    ][main.cc:60] 插入消息 消息ID2 成功。
[default-logger][10:43:50][1353793][debug   ][../../../common/icsearch.hpp:192] {
"chat_session_id" : "\u4f1a\u8bddID2",
"content" : "\u5403\u996d\u4e86\u5417\uff1f",
"create_time" : 1723025035,
"message_id" : "\u6d88\u606fID3",
"user_id" : "\u7528\u6237ID3"
}
[default-logger][10:43:50][1353793][info    ][../../../common/data_es.hpp:153] 消息数据新增/更新成功!
[default-logger][10:43:50][1353793][info    ][main.cc:60] 插入消息 消息ID3 成功。
[default-logger][10:43:50][1353793][debug   ][../../../common/icsearch.hpp:192] {
"chat_session_id" : "\u4f1a\u8bddID2",
"content" : "\u5403\u7684\u76d6\u6d47\u996d\uff01",
"create_time" : 1723024935,
"message_id" : "\u6d88\u606fID4",
"user_id" : "\u7528\u6237ID4"
}
[default-logger][10:43:50][1353793][info    ][../../../common/data_es.hpp:153] 消息数据新增/更新成功!
[default-logger][10:43:50][1353793][info    ][main.cc:60] 插入消息 消息ID4 成功。
[default-logger][10:43:50][1353793][debug   ][../../../common/icsearch.hpp:192] {
"chat_session_id" : "\u4f1a\u8bddID1",
"content" : "\u4eca\u665a\u6709\u7a7a\u5417\uff1f",
"create_time" : 1723025036,
"message_id" : "\u6d88\u606fID5",
"user_id" : "\u7528\u6237ID5"
}
[default-logger][10:43:50][1353793][info    ][../../../common/data_es.hpp:153] 消息数据新增/更新成功!
[default-logger][10:43:50][1353793][info    ][main.cc:60] 插入消息 消息ID5 成功。
[default-logger][10:43:50][1353793][debug   ][../../../common/icsearch.hpp:192] {
"chat_session_id" : "\u4f1a\u8bddID3",
"content" : "\u76d6\u6d47\u996d\u5728\u54ea\u91cc\uff1f",
"create_time" : 1723024936,
"message_id" : "\u6d88\u606fID6",
"user_id" : "\u7528\u6237ID6"
}
[default-logger][10:43:50][1353793][info    ][../../../common/data_es.hpp:153] 消息数据新增/更新成功!
[default-logger][10:43:50][1353793][info    ][main.cc:60] 插入消息 消息ID6 成功。
[default-logger][10:43:50][1353793][debug   ][../../../common/icsearch.hpp:364] 查询请求体: {
"query" : 
{
"bool" : 
{
"must" : 
[
{
"term" : 
{
"chat_session_id.keyword" : "\u4f1a\u8bddID1"
}
},
{
"match" : 
{
"content" : "\u76d6\u6d47"
}
}
]
}
}
}
[default-logger][10:43:54][1353793][debug   ][../../../common/icsearch.hpp:378] 查询响应: {"took":2550,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":2.5426364,"hits":[{"_index":"message","_type":"_doc","_id":"æ¶æ¯ID2","_score":2.5426364,"_source":{
"chat_session_id" : "\u4f1a\u8bddID1",
"content" : "\u5403\u7684\u76d6\u6d47\u996d\uff01",
"create_time" : 1723024935,
"message_id" : "\u6d88\u606fID2",
"user_id" : "\u7528\u6237ID2"
}}]}}
[default-logger][10:43:54][1353793][debug   ][../../../common/data_es.hpp:186] 检索结果条目数量:1
搜索结果数量:1
-----------------
用户ID: 用户ID2
消息ID: 消息ID2
会话ID: 会话ID1
创建时间: 2024-Aug-07 10:02:15
内容: 吃的盖浇饭!
[default-logger][10:43:54][1353793][info    ][main.cc:86] 搜索测试通过。
[default-logger][10:43:54][1353793][info    ][../../../common/data_es.hpp:164] 消息数据删除成功!
[default-logger][10:43:54][1353793][info    ][main.cc:92] 删除消息 消息ID2 成功。
[default-logger][10:43:54][1353793][info    ][../../../common/data_es.hpp:164] 消息数据删除成功!
[default-logger][10:43:54][1353793][info    ][main.cc:92] 删除消息 消息ID4 成功。
[default-logger][10:43:54][1353793][info    ][../../../common/data_es.hpp:164] 消息数据删除成功!
[default-logger][10:43:54][1353793][info    ][main.cc:92] 删除消息 消息ID6 成功。
[default-logger][10:43:54][1353793][debug   ][../../../common/icsearch.hpp:364] 查询请求体: {
"query" : 
{
"bool" : 
{
"must" : 
[
{
"term" : 
{
"chat_session_id.keyword" : "\u4f1a\u8bddID1"
}
},
{
"match" : 
{
"content" : "\u76d6\u6d47"
}
}
]
}
}
}
[default-logger][10:43:54][1353793][debug   ][../../../common/icsearch.hpp:378] 查询响应: {"took":14,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":2.5426364,"hits":[{"_index":"message","_type":"_doc","_id":"æ¶æ¯ID2","_score":2.5426364,"_source":{
"chat_session_id" : "\u4f1a\u8bddID1",
"content" : "\u5403\u7684\u76d6\u6d47\u996d\uff01",
"create_time" : 1723024935,
"message_id" : "\u6d88\u606fID2",
"user_id" : "\u7528\u6237ID2"
}}]}}
[default-logger][10:43:54][1353793][debug   ][../../../common/data_es.hpp:186] 检索结果条目数量:1
main: main.cc:108: int main(int, char**): Assertion `!found' failed.
Aborted (core dumped)
root@hcss-ecs-b4a9:/ho
#include "../../../common/data_es.hpp"
#include <gflags/gflags.h>
#include <iostream>
#include <memory>
#include <vector>
#include <cassert>

DEFINE_bool(run_mode, false, "程序的运行模式,false-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下,用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下,用于指定日志输出等级");
DEFINE_string(es_host, "http://127.0.0.1:9200/", "ES服务器URL");

int main(int argc , char *argv[])
{
    // 解析命令行参数
    google::ParseCommandLineFlags(&argc, &argv, true);

    // 初始化日志系统
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    // 创建 Elasticsearch 客户端
    auto es_client = mag::ESClientFactory::create({FLAGS_es_host});
    if (!es_client) {
        LOG_ERROR("Elasticsearch 客户端创建失败!");
        return -1;
    }

    //创建 Elasticsearch实例
    auto es_msg = std::make_shared<mag::ESMessage>(es_client);
    assert(es_msg != nullptr);

    // 1. 创建索引
    if (es_msg->createIndex()) {
        LOG_INFO("索引创建测试通过。");
    } else {
        LOG_ERROR("索引创建测试失败。");
        // 继续测试可能会失败,视情况而定
    }

    // 2. 插入数据
    struct TestMessage {
        std::string user_id;
        std::string message_id;
        long create_time;
        std::string chat_session_id;
        std::string content;
    };

    std::vector<TestMessage> test_messages = {
        {"用户ID1", "消息ID1", 1723025035, "会话ID1", "吃饭了吗?"},
        {"用户ID2", "消息ID2", 1723024935, "会话ID1", "吃的盖浇饭!"},
        {"用户ID3", "消息ID3", 1723025035, "会话ID2", "吃饭了吗?"},
        {"用户ID4", "消息ID4", 1723024935, "会话ID2", "吃的盖浇饭!"},
        {"用户ID5", "消息ID5", 1723025036, "会话ID1", "今晚有空吗?"},
        {"用户ID6", "消息ID6", 1723024936, "会话ID3", "盖浇饭在哪里?"}
    };

    for (const auto &msg : test_messages) {
        if (es_msg->appendData(msg.user_id, msg.message_id, msg.create_time, msg.chat_session_id, msg.content)) {
            LOG_INFO("插入消息 {} 成功。", msg.message_id);
        } else {
            LOG_ERROR("插入消息 {} 失败。", msg.message_id);
        }
    }

    // 3. 搜索数据
    std::string search_key = "盖浇";
    std::string search_ssid = "会话ID1";
    auto search_results = es_msg->search(search_key, search_ssid);

    std::cout << "搜索结果数量:" << search_results.size() << std::endl;
    for (const auto &msg : search_results) {
        std::cout << "-----------------" << std::endl;
        std::cout << "用户ID: " << msg.user_id() << std::endl;
        std::cout << "消息ID: " << msg.message_id() << std::endl;
        std::cout << "会话ID: " << msg.session_id() << std::endl;
        std::cout << "创建时间: " << boost::posix_time::to_simple_string(msg.create_time()) << std::endl;
        std::cout << "内容: " << msg.content() << std::endl;
    }

    // 验证搜索结果
    for (const auto &msg : search_results) {
        assert(msg.content().find(search_key) != std::string::npos);
        assert(msg.session_id() == search_ssid);
    }
    LOG_INFO("搜索测试通过。");

    // 4. 删除数据
    std::vector<std::string> delete_message_ids = {"消息ID2", "消息ID4", "消息ID6"};
    for (const auto &mid : delete_message_ids) {
        if (es_msg->remove(mid)) {
            LOG_INFO("删除消息 {} 成功。", mid);
        } else {
            LOG_ERROR("删除消息 {} 失败。", mid);
        }
    }

    // 5. 验证删除操作
    for (const auto &mid : delete_message_ids) {
        auto verify_search = es_msg->search("盖浇", search_ssid);
        bool found = false;
        for (const auto &msg : verify_search) {
            if (msg.message_id() == mid) {
                found = true;
                break;
            }
        }
        assert(!found);
        LOG_INFO("验证删除消息 {} 成功。", mid);
    }

    // 6. 最终搜索验证
    auto final_search = es_msg->search("盖浇", "会话ID1");
    std::cout << "最终搜索结果数量:" << final_search.size() << std::endl;
    for (const auto &msg : final_search) {
        std::cout << "-----------------" << std::endl;
        std::cout << "用户ID: " << msg.user_id() << std::endl;
        std::cout << "消息ID: " << msg.message_id() << std::endl;
        std::cout << "会话ID: " << msg.session_id() << std::endl;
        std::cout << "创建时间: " << boost::posix_time::to_simple_string(msg.create_time()) << std::endl;
        std::cout << "内容: " << msg.content() << std::endl;
    }

    // 确保删除后的搜索结果不包含已删除的消息
    for (const auto &msg : final_search) {
        for (const auto &del_id : delete_message_ids) {
            assert(msg.message_id() != del_id);
        }
    }
    LOG_INFO("删除后搜索验证通过。");

    LOG_INFO("综合测试完成。");

    return 0;
}

测试客户端

测试结果

服务端信息

[default-logger][11:11:41][1418398][info    ][/home/chatServer/chatServer/src/server/message/../common/data_es.hpp:131] 消息信息索引创建成功!
W1212 11:11:41.340286 1418398     0 /home/chatServer/chatServer/Test/brpc/brpc/src/bthread/bthread.cpp:409] Fail to set concurrency by tag: 0, tag concurrency must larger than old oncurrency. old concurrency: 9, new concurrency: 4
I1212 11:11:41.343655 1418398     0 /home/chatServer/chatServer/Test/brpc/brpc/src/brpc/server.cpp:1208] Server[mag::MessageServiceImpl] is serving on port=10005.
I1212 11:11:41.344201 1418398     0 /home/chatServer/chatServer/Test/brpc/brpc/src/brpc/server.cpp:1211] Check out http://hcss-ecs-b4a9:10005 in web browser.
[default-logger][11:11:41][1418398][debug   ][/home/chatServer/chatServer/src/server/message/../common/rabbitmq.hpp:99] 开始订阅 msg_queue 队列消息!
[default-logger][11:11:41][1418398][info    ][/home/chatServer/chatServer/src/server/message/source/message_server.cc:73] 注册服务到注册中心,注册中心主机:http://127.0.0.1:2379,服务名称:/service/message_service/instance
[default-logger][11:11:41][1418398][info    ][/home/chatServer/chatServer/src/server/message/source/message_server.cc:78] 服务器构建完成,正在启动...
[default-logger][11:11:41][1418445][debug   ][/home/chatServer/chatServer/src/server/message/../common/channel.hpp:96] /service/message_service-127.0.0.1:10005 服务上线了,但是当前并不关心!
[default-logger][11:11:41][1418445][debug   ][/home/chatServer/chatServer/src/server/message/../common/etcd.hpp:92] 新增服务:/service/message_service/instance-127.0.0.1:10005
[default-logger][11:11:53][1418445][debug   ][/home/chatServer/chatServer/src/server/message/../common/channel.hpp:113] /service/file_service-127.0.0.1:10002 服务上线新节点,进行添加管理!
[default-logger][11:11:53][1418445][debug   ][/home/chatServer/chatServer/src/server/message/../common/etcd.hpp:92] 新增服务:/service/file_service/instance-127.0.0.1:10002
[default-logger][11:12:05][1418445][debug   ][/home/chatServer/chatServer/src/server/message/../common/channel.hpp:96] /service/transmite_service-127.0.0.1:10004 服务上线了,但是当前并不关心!
[default-logger][11:12:05][1418445][debug   ][/home/chatServer/chatServer/src/server/message/../common/etcd.hpp:92] 新增服务:/service/transmite_service/instance-127.0.0.1:10004
[default-logger][11:12:16][1418445][debug   ][/home/chatServer/chatServer/src/server/message/../common/channel.hpp:113] /service/user_service-127.0.0.1:10003 服务上线新节点,进行添加管理!
[default-logger][11:12:16][1418445][debug   ][/home/chatServer/chatServer/src/server/message/../common/etcd.hpp:92] 新增服务:/service/user_service/instance-127.0.0.1:10003
[default-logger][11:12:28][1418445][debug   ][/home/chatServer/chatServer/src/server/message/../common/channel.hpp:96] /service/speech_service-127.0.0.1:10001 服务上线了,但是当前并不关心!
[default-logger][11:12:28][1418445][debug   ][/home/chatServer/chatServer/src/server/message/../common/etcd.hpp:92] 新增服务:/service/speech_service/instance-127.0.0.1:10001
[default-logger][11:12:42][1418448][debug   ][/home/chatServer/chatServer/src/server/message/../common/icsearch.hpp:364] 查询请求体: {
"query" : 
{
"bool" : 
{
"must" : 
[
{
"term" : 
{
"chat_session_id.keyword" : "\u4f1a\u8bddID1"
}
},
{
"match" : 
{
"content" : "\u76d6\u6d47"
}
}
]
}
}
}
[default-logger][11:12:42][1418448][debug   ][/home/chatServer/chatServer/src/server/message/../common/icsearch.hpp:378] 查询响应: {"took":425,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":2.0712707,"hits":[{"_index":"message","_type":"_doc","_id":"4e0f-95350759-0001","_score":2.0712707,"_source":{
"chat_session_id" : "\u4f1a\u8bddID1",
"content" : "\u5403\u7684\u76d6\u6d47\u996d\uff01\uff01",
"create_time" : 1733732792,
"message_id" : "4e0f-95350759-0001",
"user_id" : "0b24-e341f8f9-0001"
}},{"_index":"message","_type":"_doc","_id":"e9e5-3579f1df-0001","_score":2.0712707,"_source":{
"chat_session_id" : "\u4f1a\u8bddID1",
"content" : "\u5403\u7684\u76d6\u6d47\u996d\uff01\uff01",
"create_time" : 1733734240,
"message_id" : "e9e5-3579f1df-0001",
"user_id" : "0b24-e341f8f9-0001"
}}]}}
[default-logger][11:12:42][1418448][debug   ][/home/chatServer/chatServer/src/server/message/../common/data_es.hpp:186] 检索结果条目数量:2

客户端信息

root@hcss-ecs-b4a9:/home/chatServer/chatServer/src/server/message/build# ./message_client
[default-logger][11:12:41][1418731][debug   ][/home/chatServer/chatServer/src/server/message/../common/channel.hpp:96] /service/file_service-127.0.0.1:10002 服务上线了,但是当前并不关心!
[default-logger][11:12:41][1418731][debug   ][/home/chatServer/chatServer/src/server/message/../common/channel.hpp:96] /service-127.0.0.1:9090 服务上线了,但是当前并不关心!
[default-logger][11:12:41][1418731][debug   ][/home/chatServer/chatServer/src/server/message/../common/channel.hpp:113] /service/message_service-127.0.0.1:10005 服务上线新节点,进行添加管理!
[default-logger][11:12:41][1418731][debug   ][/home/chatServer/chatServer/src/server/message/../common/channel.hpp:96] /service/speech_service-127.0.0.1:10001 服务上线了,但是当前并不关心!
[default-logger][11:12:41][1418731][debug   ][/home/chatServer/chatServer/src/server/message/../common/channel.hpp:96] /service/transmite_service-127.0.0.1:10004 服务上线了,但是当前并不关心!
[default-logger][11:12:41][1418731][debug   ][/home/chatServer/chatServer/src/server/message/../common/channel.hpp:96] /service/user_service-127.0.0.1:10003 服务上线了,但是当前并不关心!

----------------------- 获取区间消息 --------------------------

---------------------- 获取最近消息 ---------------------------
消息 ID: 9bbd-dff5a286-0000
会话 ID: 会话ID1
时间戳: 2024-Dec-09 08:50:40
发送者 ID: 8d25-14d9ff25-0000
发送者昵称: 小猪佩奇
发送者头像: 猪爸爸头像数据
文本消息: 吃饭了吗?
------------------------------------------------------------
消息 ID: e9e5-3579f1df-0001
会话 ID: 会话ID1
时间戳: 2024-Dec-09 08:50:40
发送者 ID: 0b24-e341f8f9-0001
发送者昵称: 小猪乔治
发送者头像: 猪妈妈头像数据
文本消息: 吃的盖浇饭!!
------------------------------------------------------------

---------------------- 关键字搜索消息 ---------------------------
消息 ID: 4e0f-95350759-0001
会话 ID: 会话ID1
时间戳: 2024-Dec-09 08:26:32
发送者 ID: 0b24-e341f8f9-0001
发送者昵称: 小猪乔治
发送者头像: 猪妈妈头像数据
文本消息: 吃的盖浇饭!!
------------------------------------------------------------
消息 ID: e9e5-3579f1df-0001
会话 ID: 会话ID1
时间戳: 2024-Dec-09 08:50:40
发送者 ID: 0b24-e341f8f9-0001
发送者昵称: 小猪乔治
发送者头像: 猪妈妈头像数据
文本消息: 吃的盖浇饭!!
------------------------------------------------------------
[warn] watcher does't exit normally
root@hcss-ecs-b4a9:/home/chatServer/chatServer/src/server/message/build# 

用户管理子服务

[default-logger][11:12:16][1418612][info    ][/home/chatServer/chatServer/src/server/user/source/user_server.cc:130] UserServer 实例构建完成,准备启动。
[default-logger][11:12:16][1418612][info    ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:1174] RPC 服务器开始运行。
[default-logger][11:12:28][1418668][debug   ][/home/chatServer/chatServer/src/server/user/../common/channel.hpp:96] /service/speech_service-127.0.0.1:10001 服务上线了,但是当前并不关心!
[default-logger][11:12:28][1418668][debug   ][/home/chatServer/chatServer/src/server/user/../common/etcd.hpp:92] 新增服务:/service/speech_service/instance-127.0.0.1:10001
[default-logger][11:12:42][1418661][info    ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:634] 进入 GetMultiUserInfo 方法,Request ID: 5627-ca07de02-0001
[default-logger][11:12:42][1418661][debug   ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:653] Request ID: 5627-ca07de02-0001 - 提取用户ID列表,共 2 个用户
[default-logger][11:12:42][1418661][info    ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:663] Request ID: 5627-ca07de02-0001 - 从数据库查找用户信息成功,共找到 2 个用户
[default-logger][11:12:42][1418661][debug   ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:684] Request ID: 5627-ca07de02-0001 - 调用文件服务获取头像,共请求 2 个文件
[default-logger][11:12:42][1418659][info    ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:695] Request ID: 5627-ca07de02-0001 - 文件子服务调用成功,获取 2 个头像
[default-logger][11:12:42][1418659][debug   ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:711] Request ID: 5627-ca07de02-0001 - 用户 0b24-e341f8f9-0001 的头像已获取
[default-logger][11:12:42][1418659][debug   ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:711] Request ID: 5627-ca07de02-0001 - 用户 8d25-14d9ff25-0000 的头像已获取
[default-logger][11:12:42][1418659][info    ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:721] Request ID: 5627-ca07de02-0001 - GetMultiUserInfo 成功,耗时: 153 ms
[default-logger][11:12:42][1418662][info    ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:634] 进入 GetMultiUserInfo 方法,Request ID: 8be3-f9ce4600-0002
[default-logger][11:12:42][1418662][debug   ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:653] Request ID: 8be3-f9ce4600-0002 - 提取用户ID列表,共 1 个用户
[default-logger][11:12:42][1418662][info    ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:663] Request ID: 8be3-f9ce4600-0002 - 从数据库查找用户信息成功,共找到 1 个用户
[default-logger][11:12:42][1418662][debug   ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:684] Request ID: 8be3-f9ce4600-0002 - 调用文件服务获取头像,共请求 1 个文件
[default-logger][11:12:42][1418661][info    ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:695] Request ID: 8be3-f9ce4600-0002 - 文件子服务调用成功,获取 1 个头像
[default-logger][11:12:42][1418661][debug   ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:711] Request ID: 8be3-f9ce4600-0002 - 用户 0b24-e341f8f9-0001 的头像已获取
[default-logger][11:12:42][1418661][info    ][/home/chatServer/chatServer/src/server/user/source/user_server.hpp:721] Request ID: 8be3-f9ce4600-0002 - GetMultiUserInfo 成功,耗时: 3 ms

具体实现

获取指定区间消息

// 获取指定时间区间内的消息
void range_test(const std::string &ssid, 
    const boost::posix_time::ptime &stime,
    const boost::posix_time::ptime &etime) {

    // 获取通信信道
    auto channel = sm->choose(FLAGS_message_service);
    if (!channel) {
        std::cerr << "【错误】获取通信信道失败!" << std::endl;
        return;
    }

    // 创建消息存储服务的 Stub
    mag::MsgStorageService_Stub stub(channel.get());
    mag::GetHistoryMsgReq req;
    mag::GetHistoryMsgRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_chat_session_id(ssid);
    req.set_start_time(boost::posix_time::to_time_t(stime));  // 转换为 Unix 时间戳
    req.set_over_time(boost::posix_time::to_time_t(etime));  // 转换为 Unix 时间戳

    // 发送请求
    brpc::Controller cntl;
    stub.GetHistoryMsg(&cntl, &req, &rsp, nullptr);

    // 检查请求是否失败
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());

    // 打印消息内容
    std::cout << "\n----------------------- 获取区间消息 --------------------------\n";
    for (int i = 0; i < rsp.msg_list_size(); i++) {
        const auto &msg = rsp.msg_list(i);
        std::cout << "消息 ID: " << msg.message_id() << std::endl;
        std::cout << "会话 ID: " << msg.chat_session_id() << std::endl;
        std::cout << "时间戳: " << boost::posix_time::to_simple_string(boost::posix_time::from_time_t(msg.timestamp())) << std::endl;
        std::cout << "发送者 ID: " << msg.sender().user_id() << std::endl;
        std::cout << "发送者昵称: " << msg.sender().nickname() << std::endl;
        std::cout << "发送者头像: " << msg.sender().avatar() << std::endl;

        // 根据消息类型输出不同的内容
        switch (msg.message().message_type()) {
            case mag::MessageType::STRING:
                std::cout << "文本消息: " << msg.message().string_message().content() << std::endl;
                break;
            case mag::MessageType::IMAGE:
                std::cout << "图片消息: " << msg.message().image_message().image_content() << std::endl;
                break;
            case mag::MessageType::FILE:
                std::cout << "文件消息: " << msg.message().file_message().file_contents() << std::endl;
                std::cout << "文件名称: " << msg.message().file_message().file_name() << std::endl;
                break;
            case mag::MessageType::SPEECH:
                std::cout << "语音消息: " << msg.message().speech_message().file_contents() << std::endl;
                break;
            default:
                std::cerr << "【错误】未知的消息类型!" << std::endl;
                break;
        }
        std::cout << "------------------------------------------------------------\n";
    }
}

获取会话最近消息

// 获取指定会话的最近消息
void recent_test(const std::string &ssid, int count) {

    // 获取通信信道
    auto channel = sm->choose(FLAGS_message_service);
    if (!channel) {
        std::cerr << "【错误】获取通信信道失败!" << std::endl;
        return;
    }

    // 创建消息存储服务的 Stub
    mag::MsgStorageService_Stub stub(channel.get());
    mag::GetRecentMsgReq req;
    mag::GetRecentMsgRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_chat_session_id(ssid);
    req.set_msg_count(count);

    // 发送请求
    brpc::Controller cntl;
    stub.GetRecentMsg(&cntl, &req, &rsp, nullptr);

    // 检查请求是否失败
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());

    // 打印消息内容
    std::cout << "\n---------------------- 获取最近消息 ---------------------------\n";
    for (int i = 0; i < rsp.msg_list_size(); i++) {
        const auto &msg = rsp.msg_list(i);
        std::cout << "消息 ID: " << msg.message_id() << std::endl;
        std::cout << "会话 ID: " << msg.chat_session_id() << std::endl;
        std::cout << "时间戳: " << boost::posix_time::to_simple_string(boost::posix_time::from_time_t(msg.timestamp())) << std::endl;
        std::cout << "发送者 ID: " << msg.sender().user_id() << std::endl;
        std::cout << "发送者昵称: " << msg.sender().nickname() << std::endl;
        std::cout << "发送者头像: " << msg.sender().avatar() << std::endl;

        // 根据消息类型输出不同的内容
        switch (msg.message().message_type()) {
            case mag::MessageType::STRING:
                std::cout << "文本消息: " << msg.message().string_message().content() << std::endl;
                break;
            case mag::MessageType::IMAGE:
                std::cout << "图片消息: " << msg.message().image_message().image_content() << std::endl;
                break;
            case mag::MessageType::FILE:
                std::cout << "文件消息: " << msg.message().file_message().file_contents() << std::endl;
                std::cout << "文件名称: " << msg.message().file_message().file_name() << std::endl;
                break;
            case mag::MessageType::SPEECH:
                std::cout << "语音消息: " << msg.message().speech_message().file_contents() << std::endl;
                break;
            default:
                std::cerr << "【错误】未知的消息类型!" << std::endl;
                break;
        }
        std::cout << "------------------------------------------------------------\n";
    }
}

根据关键字搜索消息

// 根据关键字搜索消息
void search_test(const std::string &ssid, const std::string &key) {

    // 获取通信信道
    auto channel = sm->choose(FLAGS_message_service);
    if (!channel) {
        std::cerr << "【错误】获取通信信道失败!" << std::endl;
        return;
    }

    // 创建消息存储服务的 Stub
    mag::MsgStorageService_Stub stub(channel.get());
    mag::MsgSearchReq req;
    mag::MsgSearchRsp rsp;
    req.set_request_id(mag::uuid());
    req.set_chat_session_id(ssid);
    req.set_search_key(key);

    // 发送请求
    brpc::Controller cntl;
    stub.MsgSearch(&cntl, &req, &rsp, nullptr);

    // 检查请求是否失败
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());

    // 打印消息内容
    std::cout << "\n---------------------- 关键字搜索消息 ---------------------------\n";
    for (int i = 0; i < rsp.msg_list_size(); i++) {
        const auto &msg = rsp.msg_list(i);
        std::cout << "消息 ID: " << msg.message_id() << std::endl;
        std::cout << "会话 ID: " << msg.chat_session_id() << std::endl;
        std::cout << "时间戳: " << boost::posix_time::to_simple_string(boost::posix_time::from_time_t(msg.timestamp())) << std::endl;
        std::cout << "发送者 ID: " << msg.sender().user_id() << std::endl;
        std::cout << "发送者昵称: " << msg.sender().nickname() << std::endl;
        std::cout << "发送者头像: " << msg.sender().avatar() << std::endl;

        // 根据消息类型输出不同的内容
        switch (msg.message().message_type()) {
            case mag::MessageType::STRING:
                std::cout << "文本消息: " << msg.message().string_message().content() << std::endl;
                break;
            case mag::MessageType::IMAGE:
                std::cout << "图片消息: " << msg.message().image_message().image_content() << std::endl;
                break;
            case mag::MessageType::FILE:
                std::cout << "文件消息: " << msg.message().file_message().file_contents() << std::endl;
                std::cout << "文件名称: " << msg.message().file_message().file_name() << std::endl;
                break;
            case mag::MessageType::SPEECH:
                std::cout << "语音消息: " << msg.message().speech_message().file_contents() << std::endl;
                break;
            default:
                std::cerr << "【错误】未知的消息类型!" << std::endl;
                break;
        }
        std::cout << "------------------------------------------------------------\n";
    }
}


原文地址:https://blog.csdn.net/gma999/article/details/144374052

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!