查看cmakelist文件可以知道:
项目生成的主要可执行程序run_server,对应的代码文件是: AnyQ/deme/run_server.cpp,详细代码如下:
run_server.cpp主要是加载配置文件,服务初始化,并启动服务一直运行,等待请求。
AnyQ/deme/run_server.cpp部分代码如下所示:
#include <glog/logging.h>
#include "server/http_server.h"
#include "common/utils.h"
#include "common/plugin_header.h"
int main(int argc, char* argv[]) {
google::InitGoogleLogging(argv[0]);
FLAGS_stderrthreshold = google::INFO;
anyq::HttpServer server;
std::string anyq_brpc_conf = "./example/conf/anyq_brpc.conf"; # 加载配置文件,详细内容见下面。应该改成输入参数,可配置化
if (server.init(anyq_brpc_conf) != 0) { # 服务初始化,代码详细内容见下
FATAL_LOG("server init failed");
return -1;
}
if (server.always_run() != 0) { # 服务一直运行,等待请求
FATAL_LOG("server run failed");
return -1;
}
return 0;
}
相关路径:AnyQ/src/server/http_server.cpp
#include "server/http_server.h"
#include "server/session_data_factory.h"
namespace brpc {
DECLARE_bool(usercode_in_pthread);
}
namespace anyq {
HttpServer::HttpServer() {
}
HttpServer::~HttpServer() {
}
# 服务初始化函数
int HttpServer::init(std::string& http_server_conf) {
if (load_config_from_file(http_server_conf, _server_config) != 0) {
fprintf(stderr, "load_config_from_file failed\n");
return -1;
}
_idle_timeout_sec = _server_config.idle_timeout_sec(); # 获取./example/conf/anyq_brpc.conf中的各种配置参数
_max_concurrency = _server_config.max_concurrency();
_port = _server_config.port();
_server_conf_dir = _server_config.server_conf_dir();
//_log_conf_file = _server_config.log_conf_file();
_anyq_dict_conf_dir = _server_config.anyq_dict_conf_dir();
_anyq_conf_dir = _server_config.anyq_conf_dir();
if (_server_config.has_solr_clear_passwd()) {
_solr_clear_passwd = _server_config.solr_clear_passwd();
}
/*int ret = com_loadlog(_server_conf_dir.c_str(), _log_conf_file.c_str());
if (ret != 0) {
fprintf(stderr, "load log conf error\n");
return -1;
}*/
// 启动时,加载词典 # 加载词典
if (_dict_manager.load_dict(_anyq_dict_conf_dir) != 0) {
FATAL_LOG("load dict error, _anyq_dict_conf_dir=%s", _anyq_dict_conf_dir.c_str());
return -1;
}
return 0;
}
# start好像没有项目调用到
int HttpServer::start() {
// SessionDataFactory声明, 用于创建sessiondata
SessionDataFactory session_data_factory(
&_dict_manager, _anyq_conf_dir, _solr_clear_passwd);
// server options
brpc::ServerOptions options;
options.idle_timeout_sec = _idle_timeout_sec;
options.max_concurrency = _max_concurrency;
options.session_local_data_factory = &session_data_factory;
// Instance of your service.
HttpServiceImpl http_service_impl;
if (http_service_impl.init(_server_config) != 0) {
WARNING_LOG("http_service_impl init failed: req pre/post processor conf issue.");
return -1;
}
// Add the service into server.
int ret = _server.AddService(&http_service_impl,
brpc::SERVER_DOESNT_OWN_SERVICE,
"/anyq => anyq,"
"/solr/v1/delete => solr_delete,"
"/solr/v1/insert => solr_insert,"
"/solr/v1/update => solr_update,"
"/solr/v1/clear => solr_clear");
if (ret != 0) {
FATAL_LOG("Fail to add service to server");
return -1;
}
// Start the server.
if (_server.Start(_port, &options) != 0) {
FATAL_LOG("Fail to start HttpServer");
return -1;
}
return 0;
}
# 服务一直运行
int HttpServer::always_run() {
// 开辟栈空间
brpc::FLAGS_usercode_in_pthread = true;
// SessionDataFactory声明, 用于创建sessiondata
SessionDataFactory session_data_factory(&_dict_manager, _anyq_conf_dir, _solr_clear_passwd);
// server options,服务器参数选项
brpc::ServerOptions options;
options.idle_timeout_sec = _idle_timeout_sec;
options.max_concurrency = _max_concurrency;
options.session_local_data_factory = &session_data_factory;
// Instance of your service. 你的服务实例
HttpServiceImpl http_service_impl; # HttpServiceImpl中,比较重要的接口,问答语义检索 void anyq(),这个实例下面会塞给_server
# HttpServiceImpl这个是比较重要的函数,代码详解见:http_service_impl.cpp
if (http_service_impl.init(_server_config) != 0) { # http_service_impl初始化
WARNING_LOG("http_service_impl init failed: req pre/post processor conf issue.");
return -1;
}
// Add the service into server, 添加服务到服务器里,后面是路由映射
int ret = _server.AddService(&http_service_impl,
brpc::SERVER_DOESNT_OWN_SERVICE,
"/anyq => anyq," # 我们的请求是:http://bi-ml-2:8999/anyq
"/solr/v1/delete => solr_delete,"
"/solr/v1/insert => solr_insert,"
"/solr/v1/update => solr_update,"
"/solr/v1/clear => solr_clear");
if (ret != 0) {
FATAL_LOG("Fail to add service to server");
return -1;
}
// Start the server. 开始服务
if (_server.Start(_port, &options) != 0) {
FATAL_LOG("Fail to start HttpServer");
return -1;
}
// Wait until Ctrl-C is pressed, then Stop() and Join() the server.安全退出
_server.RunUntilAskedToQuit();
return 0;
}
} // namespace anyq
配置端口号,输出目录等信息:./example/conf/anyq_brpc.conf
idle_timeout_sec : -1
max_concurrency : 8 # 并发数
port : 8999 # 端口号
server_conf_dir : "./example/conf/" # 配置文件目录,路径都是指到./example/conf/
log_conf_file : "log.conf"
anyq_dict_conf_dir : "./example/conf/"
anyq_conf_dir: "./example/conf/"
preproc_plugin {
name : "default_preproc"
type : "AnyqPreprocessor"
}
postproc_plugin {
name : "default_postproc"
type : "AnyqPostprocessor"
}