用C++搭建的web服务不支持多线程异步,可以通过打开多个不同端口的应用程序,然后配置nginx实现。
1、实现通过命令参数运行应用程序;这里以mfc应用程序为例,可通过属性设置调试;
2、批处理打开多个不同端口的应用程序;
echo off
taskkill /f /im XRayDetector.exe
start XRayDetector.exe 9697
start XRayDetector.exe 9698
start XRayDetector.exe 9699
start XRayDetector.exe 9700
3、配置nginx;下载nginx,记事本打开conf/nginx.conf,设置api路径和代理端口;
upstream serverswitch {
server localhost:9697;
server localhost:9698;
server localhost:9699;
server localhost:9700;
}
server {
listen 9696;
server_name localhost;
location /api/xxx{
proxy_pass http://serverswitch;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4、保存运行nginx.exe即可以将http://localhost:9696/api/xxx,这个请求自动分配到之前打开的端口号为9697、9698的应用程序上面;