brpc 是用 c++语言编写的工业级 RPC 框架,常用于搜索、存储、机器学习、广告、推荐等高性能系统。
brpc的远程调用思想;将数据处理的过程不在放在本地进行,而是放在服务器中去
接口使用
客户端和服务端的使用
服务端:
1.继承EchoServerImpl的一个子类 并实现rpc调用的业务
2.构建服务器对象
3.新增echo服务
4.启动服务器
设置服务器设置ServerOption
#include <brpc/server.h>
#include <brpc/closure_guard.h>
#include <butil/logging.h>
#include "main.pb.h"
//1.构建子类brpcserver对象实现brpc业务函数
class EchoServiceImpl:public example::EchoService
{
public:
EchoServiceImpl(){}
~EchoServiceImpl() override
{}
void Echo(google::protobuf::RpcController* controller,
const ::example::EchoRequest* request,
::example::EchoResponse* response,
::google::protobuf::Closure* done) override
{
brpc::ClosureGuard rpc_guard(done);
// ~ClosureGuard() {
//if (_done) {
//_done->Run();
//}
std::cout<<"收到了消息"<<request->message()<<std::endl;
response->set_message(request->message()+"好的,我知道了");
//_done->Run();
}
};
int main(int argc,char * argv[])
{
logging::LoggingSettings logger;
logger.logging_dest= logging :: LoggingDestination::LOG_TO_NONE;
logging::InitLogging(logger);
//2.创建服务器并添加业务
brpc::Server server;
EchoServiceImpl echosservice;
if(&echosservice==nullptr)
{
std::cout << "添加Rpc服务失败!\n";
exit(0);
}
int n=server.AddService(&echosservice,brpc::ServiceOwnership::SERVER_DOESNT_OWN_SERVICE);
if(n<0)
{
std::cout<<"AddService error"<<std::endl;
exit(0);
}
//函数不匹配,源码有可能设置的是指针类型,tips
//3.设置服务器选项,并且启动服务器
brpc::ServerOptions opt;
opt.idle_timeout_sec=-1;//尝试连接时间 超时则退出
opt.num_threads=1;//number of pthreads that server runs on. 单线程
opt.max_concurrency=10; //number of requests processed in parallel(并行)// Default: 0 (unlimited)
int m=server.Start(8080,&opt);
if(m<0)
{
std::cout<<"server Start error"<<std::endl;
exit(1);
}
server.RunUntilAskedToQuit();//运行等待服务结束
}
客户端:
1.构建channel信道,连接服务器
2.构造Echoservice_Stub对象,进行rpc调用
3.rpc调用 定义请求 构造请求
#include<brpc/channel.h>
#include<brpc/grpc_health_check.pb.h>
#include"main.pb.h"
void callback(brpc::Controller* control,example::EchoResponse* response1)
{
std::unique_ptr<brpc::Controller> cntl_guard(control);
std::unique_ptr<example::EchoResponse> resp_guard(response1);
if(control->Failed())
{
std::cout<<"rpc echo service failed"<<control->ErrorText()<<std::endl;
exit(1);
}
std::cout<<response1->message()<<std::endl;
}
int main(int argc,char *argv[])
{
//构建信道并且初始化
brpc::Channel channel;
brpc::ChannelOptions options;
options.connect_timeout_ms=200;//尝试连接时长
Default: 200 (milliseconds)
options.timeout_ms=-1;//rpc调用等待时间
//Max duration of RPC over this Channel RPC调用在信道的期间
options.protocol="baidu_std";
options.max_retry=3;
channel.Init("127.0.0.1",8080,&options);
//调用EchoService_Stub 进行rpc业务调用
example::EchoService_Stub echoServiceStub(&channel);
brpc::Controller* control=new brpc::Controller();
control->Reset();
example::EchoRequest request1;
example::EchoResponse* response1=new example::EchoResponse();
request1.set_message("你好啊少年");
//rpc业务调用
// echoServiceStub.Echo(control, &request1,response1,nullptr);
// if(control->Failed())
// {
// std::cout<<"rpc echo service failed"<<control->ErrorText()<<std::endl;
// exit(1);
// }
// std::cout<<"client收到响应"<<response1->message()<<std::endl;
//Echo要设置成异步的话
auto closure1=google::protobuf::NewCallback(callback,control,response1);
echoServiceStub.Echo(control, &request1,response1,closure1);
std::cout<<"异步访问结束"<<std::endl;
}
main.proto
syntax="proto3";
package example;
option cc_generic_services = true;
message EchoRequest {
string message = 1;
};
message EchoResponse {
string message = 1;
};
service EchoService {
rpc Echo(EchoRequest) returns (EchoResponse);
};