[thrift](https://thrift.apache.org/)is a RPC framework used widely in production environment which was developed by Facebook. In order to access thrift servers more conveniently and make full use of concurrency ability of bthread, brpc directly supports the thrift protocol.
Check [example/thrift_extension_c++](https://github.com/brpc/brpc/tree/master/example/thrift_extension_c++/) for an example.
[thrift](https://thrift.apache.org/) is a RPC framework used widely in various environments, which was developed by Facebook and adopted by Apache later. In order to interact with thrift servers and solves issues on thread-safety, usabilities and concurrencies, brpc directly supports the thrift protocol that is used by thrift in NonBlocking mode.
Advantages compared to the official thrift client:
- Thread safety. No need to set up separate clients for each thread.
- Supports synchronous, asynchronous, batch synchronous, batch asynchronous, and other access methods. Combination channels such as ParallelChannel are also supported.
- Support various connection types(short, connection pool). Support timeout, backup request, cancellation, tracing, built-in services, and other benefits offered by brpc.
# Compile and Run
In order to not depend on and compile with thrift library for most users, the thrift protocol wasn't supported in brpc by default. Configure brpc with --with-thrift if you want to enable thrift protocol in brpc.
# Compile
To reuse the parsing code, brpc depends on the thrift lib and the code generated by thrift tools to support thrift. Please read official documents to find out methods to write thrift files, generate code, compilations etc.
brpc does not enable thrift support or depend on the thrift lib by default. If the thrift support is needed, config brpc with extra --with-thrift.
Install Thrift in Ubuntu
Install thrift under Ubuntu
Read [Official wiki](https://thrift.apache.org/docs/install/debian) to install depended libs and tools, then download the thrift source code from [official site](https://thrift.apache.org/download), uncompress and compile。
```bash
Download thrift source code from offical [web site](https://thrift.apache.org/download)
Config brpc with thrift support, then make. The compiled libbrpc.a includes extended code for thrift support and can be linked normally as in other brpc projects.
```bash
sh config_brpc.sh --headers=/usr/include --libs=/usr/lib --nodebugsymbols--with-thrift
```
Thrift extension was supported via static library, libbrpc.a was generated after compile, link with it if users want to enable thrift protocol.
# Thrift message definition, echo.thrift:
```c++
namespacecppexample
structEchoRequest{
1:requiredstringdata;
2:requiredi32s;
}
structEchoResponse{
1:requiredstringdata;
}
serviceEchoService{
EchoResponseEcho(1:EchoRequestrequest);
}
sh config_brpc.sh --headers=/usr/include --libs=/usr/lib --with-thrift
```
# Client accesses thrift server
Steps:
- Create a Channel with protocol set to brpc::PROTOCOL_THRIFT
- Define and use brpc::ThriftMessage<Native-Request> as the request, brpc::ThriftMessage<Native-Response> as the response. raw() method returns the native thrift message.
- Set method-name for thrift via Controller::set_thrift_method_name()
DEFINE_string(server,"0.0.0.0:8019","IP Address of thrift server");
...
...
@@ -69,39 +53,34 @@ if (thrift_channel.Init(Flags_server.c_str(), FLAGS_load_balancer.c_str(), &opti
LOG(ERROR)<<"Fail to initialize thrift channel";
return-1;
}
...
```
Construct a thrift request and send it to server, ThriftMessage is a template class that hosts thrift navtive messages, raw() methods can directly manipulate native thrift messages.
```c++
// wrapper thrift raw request into ThriftMessage
// example::[EchoRequest/EchoResponse]is thrift native message generated by thrift toolkits
LOG(ERROR)<<"Fail to send thrift request, "<<cntl.ErrorText();
return-1;
}
if(cntl.Failed()){
LOG(ERROR)<<"Fail to send thrift request, "<<cntl.ErrorText();
return-1;
}
```
# Server side processing upstream thrift requests
Users need to implement their own thrift handler which was inherited from brpc::ThriftService.
Due to the limitation of thrift protocol itself, only the method name can be obtained on the server (via the controller.thrift_method_name() method), and the service name cannot be obtained. This is consistent with the native thrift implementation, which means that there can only be one thrift service in one brpc server.
# Server processes thrift requests
Inherit brpc::ThriftService to implement the processing code, which may call the native handler generated by thrift to re-use older entry directly, or read the request and set the response directly just as in other protobuf services.