同步操作将从 feimat/fastrpc 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
简单易用,几乎和调用本地方法一样使用即可。
使用protobuf生成代码 支持同时处理pbrpc调用和http请求
客户端支持 windows linux android使用
支持多进程协程和多线程协程两种模式
支持同步异步方式调用 支持单向推送
支持hook sys socket,不用修改任何代码让你业务里的同步socket操作变异步
支持闲时关闭链接 支持自动重连与断开事件处理
同步压测 4核机子虚拟机
简单echo(因为nginx是读文件公平竞争 我们也是读文件) 1000并发 6wqps; nginx 4.5wqps
异步压测10并发 50+w qps
另有400多行实现的 python web服务器,原理一样的,以供参考(通用的多线程reactor模式,用非阻塞模拟异步io)
http://www.oschina.net/p/fastpy
例子
首先定义protobuf 协议
package echo;
message EchoRequest
{
required string message = 1;
};
message EchoResponse
{
required string response = 1;
};
service EchoService
{
rpc Echo(EchoRequest) returns (EchoResponse);
};
option cc_generic_services = true;
然后客户端调用:
RpcClient client(10, "192.168.1.13", 8999, 1000); // 1:并发sock数 2:host 3:ip 4:超时时间
echo::EchoService::Stub stub(&client);
echo::EchoRequest req;
req.set_message("cli hello");
echo::EchoResponse res;
stub.Echo(NULL, &req, &res, NULL); // 同步
服务器调用:
class EchoServiceImpl : public echo::EchoService { // 实现pb生成的接口
virtual void Echo(::google::protobuf::RpcController* controller,
const ::echo::EchoRequest* request,
::echo::EchoResponse* response,
::google::protobuf::Closure* done) {
response->set_response(request->message()+" server_hello");
if (done) {
done->Run();
}
}
};
RpcServer server("192.168.1.13", 8999);
RPCREGI_ONEP(server, EchoServiceImpl);
server.start();
服务器http 处理:
class MyHttpHandler : public HttpHandler {
public:
virtual void Init(CASyncSvr* svr) {}
virtual void OnRec(HttpRequest* request,
::google::protobuf::Closure done) {
CHttpParser ps = request->ps;
ps->parse_form_body(); // 解析post数据
std::string kk = ps->get_param("kk");
std::string res = kk + " server hello":
std::string content_type = "text/html";
std::string add_head = "Connection: keep-alive\r\n";
CHttpResponseMaker::make_string(kk,
request->response,
content_type,
add_head);
if (done) {
done->Run();
}
}
virtual void Finish(CASyncSvr* svr) {}
};
RpcServer server("192.168.1.13", 8999);
RPCREGI(server, EchoServiceImpl);
HTTPREGI(server, MyHttpHandler);
server.start();
浏览器访问 http://192.168.1.13:8999/xxx?kk=hello
上面只是简单演示了同步rpc请求的例子,
异步调用、协程使用、服务器向客户端单向推送、定时器、线程池的使用和各种网络事件处理的例子参考以下两个main.cpp文件即可
客户端:example/client/main.cpp
服务器端: example/server/main.cpp
http的处理库:http_codec/http/http_util.cpp
更多丰富功能请参阅代码,如果要用到python开发包请参考这里安装http://web.49jie.com/?p=1644
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。