【六】tf和cgi进行联合试验,完成日志服务器
【任务6】tf和cgi进行联合试验,完成日志服务器
改装gen-cpp目录下client.cpp文件
- 代码如下:
#include "RecSys.h"
#include <iostream>
#include <string>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
#include <fcgi_stdio.h>
#include <fcgiapp.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace std;
using std::string;
using boost::shared_ptr;
inline void send_response(
FCGX_Request& request, const std::string& resp_str) {
FCGX_FPrintF(request.out, "Content-type: text/html;charset=utf-8\r\n\r\n");
FCGX_FPrintF(request.out, "%s", resp_str.c_str());
FCGX_Finish_r(&request);
}
int main(int argc, char **argv){
// 1.初始化cgi
FCGX_Init();
FCGX_Request request;
FCGX_InitRequest(&request, 0, 0);
// 2.连接connect server rpc
boost::shared_ptr<TSocket> socket(new TSocket("localhost",9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
transport->open();
RecSysClient client(protocol);
while(FCGX_Accept_r(&request) >= 0) {
// http page -> client
std::string send_data = FCGX_GetParam("QUERY_STRING", request.envp);
string receive_data;
// client >> server
// server >> client
client.rec_data(receive_data,send_data);
cout << "receive http params: " << send_data << std::endl;
cout << "receive server data: " << receive_data << endl;
/*send_response(request, return_str);*/
send_response(request, receive_data);
}
transport->close();
return 0;
}
启动Nginx服务和gen-cpp目录下编译后的"server"
client.cpp
文件代码修改为:
#include "RecSys.h"
#include <iostream>
#include <string>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
#include <fcgi_stdio.h>
#include <fcgiapp.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace std;
using std::string;
using boost::shared_ptr;
inline void send_response(
FCGX_Request& request, const std::string& resp_str) {
FCGX_FPrintF(request.out, "Content-type: text/html;charset=utf-8\r\n\r\n");
FCGX_FPrintF(request.out, "%s", resp_str.c_str());
FCGX_Finish_r(&request);
}
int main(int argc, char **argv){
// 1.初始化cgi
FCGX_Init();
FCGX_Request request;
FCGX_InitRequest(&request, 0, 0);
// 2.连接connect server rpc
boost::shared_ptr<TSocket> socket(new TSocket("localhost",9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
transport->open();
RecSysClient client(protocol);
while(FCGX_Accept_r(&request) >= 0) {
// http page -> client
std::string send_data = FCGX_GetParam("QUERY_STRING", request.envp);
string receive_data;
// client >> server
// server >> client
client.rec_data(receive_data,send_data);
cout << "receive http params: " << send_data << std::endl;
cout << "receive server data: " << receive_data << endl;
/*send_response(request, return_str);*/
send_response(request, receive_data);
}
transport->close();
return 0;
}
Makefile 文件修改为
G++ = g++
CFLAGS = -g -Wall
INCLUDES = -I./ -I/usr/local/include/thrift
LIBS = -L/usr/local/lib/*.so -lthrift
SER_OBJECT = RecSys.cpp RecSys_constants.cpp RecSys_types.cpp RecSys_server.skeleton.cpp
CLI_OBJECT = RecSys.cpp client.cpp
server: $(SER_OBJECT)
$(G++) $(CFLAGS) $(INCLUDES) $(SER_OBJECT) $(LIBS) -o server
client: $(CLI_OBJECT)
$(G++) $(CFLAGS) $(INCLUDES) $(CLI_OBJECT) $(LIBS) -o client
.PHONY: clean
clean:
rm -f server client
-
命令:
make server
、make client
、make clean
等会对应执行名相应的命令
启动Nginx服务
- 命令:
安装目录/sbin/nginx
- 检查端口是否开启:
netstat -antup | grep nginx
,端口为:80 - 打开浏览器查看服务是否开启,查看
启动gen-cpp目录下的"server"
- 命令:
./server
- 此时已经启动了cgi代理,查看端口是否监听成功:
netstat -antup | grep 8088
启动cgi服务
-
启动之前先编译
client.cpp
文件,命令:make client
-
命令:
/usr/local/src/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 8088 -f /test/thrift_test/python_thrift_demo/gen-cpp/client
查看整个流程的服务是否成功
-
打开浏览器,查看,页面(也就是客户端)会返回
server
端输出的内容,同时server
端会接收到客户端(也就是浏览器)发送的信息 -
如图:
-
带参数:
-
客户端:
-
server端:
-