Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
78561f07
Unverified
Commit
78561f07
authored
May 24, 2018
by
Ge Jun
Committed by
GitHub
May 24, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish docs/cn/thrift.md
parent
a078785d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
48 deletions
+44
-48
thrift.md
docs/cn/thrift.md
+44
-48
No files found.
docs/cn/thrift.md
View file @
78561f07
[
English Version
](
../en/thrift.md
)
[
thrift
](
https://thrift.apache.org/
)
是近几年应用较广的Facebook发布的RPC服务, 为了使用户更方便,快捷的利用bthread的并发能力,brpc实现并支持thrift工作在NonBlocking模式下的协议(FramedProtocol), 注意本文中所说的thrift协议一律指的是此种情况下的thrift协议.
示例程序:
[
example/thrift_extension_c++
](
https://github.com/brpc/brpc/tree/master/example/thrift_extension_c++/
)
[
thrift
](
https://thrift.apache.org/
)
是应用较广的RPC框架,最初由Facebook发布,后交由Apache维护。为了和thrift服务互通,同时解决thrift原生方案在多线程安全、易用性、并发能力等方面的一系列问题,brpc实现并支持thrift在NonBlocking模式下的协议(FramedProtocol), 下文均直接称为thrift协议。
相比使用官方原生的优势有:
示例程序:
[
example/thrift_extension_c++
](
https://github.com/brpc/brpc/tree/master/example/thrift_extension_c++/
)
相比使用原生方案的优势有:
-
线程安全。用户不需要为每个线程建立独立的client.
-
支持同步、异步、批量同步、批量异步等访问方式,能使用ParallelChannel等组合访问方式.
-
支持多种连接方式(连接池, 短连接), 支持超时、backup request、取消、tracing、内置服务等一系列RPC基本福利.
# 编译依赖及运行
默认brpc编译是不启用thrift协议支持的, 目的是在用户不需要thrift协议支持的情况下可以不安装thrift依赖. 如果用户启用thrift协议的话, 在配置brpc环境的时候
加上--with-thrift参数.
brpc编译默认不启用thrift协议支持, 目的是在用户不需要thrift协议支持时可以不安装thrift依赖. 如果用户启用thrift协议, 配置brpc环境的时候需
加上--with-thrift参数.
安装thrift依赖, ubuntu环境下
Ubuntu环境下安装thrift依赖
先参考
[
官方wiki
](
https://thrift.apache.org/docs/install/debian
)
安装好必备的依赖和工具,然后从
[
官网
](
https://thrift.apache.org/download
)
下载thrift源代码,解压编译。
```
bash
从[官网]
(
https://thrift.apache.org/download
)
下载thrift源代码
wget http://www.us.apache.org/dist/thrift/0.11.0/thrift-0.11.0.tar.gz
tar
-xf
thrift-0.11.0.tar.gz
cd
thrift-0.11.0/
...
...
@@ -22,14 +22,12 @@ cd thrift-0.11.0/
make
CPPFLAGS
=
-DFORCE_BOOST_SMART_PTR
-j
3
-s
sudo
make
install
```
Debian请参考
[
官方wiki
]
](https://thrift.apache.org/docs/install/debian)
配置brpc支持thrift协议
```
bash
sh config_brpc.sh
--headers
=
/usr/include
--libs
=
/usr/lib
--
nodebugsymbols
--
with-thrift
sh config_brpc.sh
--headers
=
/usr/include
--libs
=
/usr/lib
--with-thrift
```
编译完成后会生成libbrpc.a, thrift扩展协议以静态库的方式提供给用户, 用户在需要启用thrift协议的时候链接即可.
编译完成后会生成libbrpc.a, 其中包含了支持thrift协议的扩展代码, 像正常使用brpc的代码一样链接即可。
# Thrift 原生消息定义, echo.thrift:
```
c++
...
...
@@ -50,14 +48,16 @@ service EchoService {
```
# Client端访问下游thrift server
创建一个访问thrift server的Channel:
# Client端访问thrift server
基本步骤:
-
创建一个协议设置为brpc::PROTOCOL_THRIFT的Channel
-
定义brpc::ThriftMessage
<
原生
Request
>
作为请求,brpc::ThriftMessage
<
原生
Response
>
作为回复。raw()方法可以操作原生thrift消息。
-
通过Controller::set_thrift_method_name()设置thrift方法名。
示例代码如下:
```
c++
#include <brpc/channel.h>
#include <brpc/details/thrift_utils.h>
#include <brpc/thrift_message.h>
#include <brpc/thrift_message.h> // 定义了ThriftMessage
...
DEFINE_string
(
server
,
"0.0.0.0:8019"
,
"IP Address of thrift server"
);
...
...
@@ -71,39 +71,34 @@ if (thrift_channel.Init(Flags_server.c_str(), FLAGS_load_balancer.c_str(), &opti
LOG
(
ERROR
)
<<
"Fail to initialize thrift channel"
;
return
-
1
;
}
...
```
构造thrift请求, 并发送, ThriftMessage是模板类, 里面托管了thrift原生消息, 通过raw()方法可以可以直接操作原生thrift消息
```
c++
// wrapper thrift raw request into ThriftMessage
// example::[EchoRequest/EchoResponse]是thrfit原生定义的消息(通过thrift代码生成工具生成)
brpc
::
ThriftMessage
<
example
::
EchoRequest
>
req
;
brpc
::
ThriftMessage
<
example
::
EchoResponse
>
res
;
req
.
raw
().
data
=
"hello"
;
// wrapper thrift raw request into ThriftMessage
// example::[EchoRequest/EchoResponse]是thrfit原生定义的消息(通过thrift代码生成工具生成)
brpc
::
ThriftMessage
<
example
::
EchoRequest
>
req
;
brpc
::
ThriftMessage
<
example
::
EchoResponse
>
res
;
cntl
.
set_thrift_method_name
(
"Echo"
);
req
.
raw
().
data
=
"hello"
;
cntl
.
set_thrift_method_name
(
"Echo"
);
channel
.
CallMethod
(
NULL
,
&
cntl
,
&
req
,
&
res
,
NULL
);
channel
.
CallMethod
(
NULL
,
&
cntl
,
&
req
,
&
res
,
NULL
);
if
(
cntl
.
Failed
())
{
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端处理上游thrfit请求
类似原生brpc协议, 用户需要实现自己的thrift handler, 继承自brpc::ThriftService
由于thrift协议本身的限制, 在服务端只能获取到method name(通过controller.thrift_method_name()方法), 无法获取到service name, 这和原生的thrift实现是一致的, 也就意味着在一个brpc server中只能有一个thrift service
# Server端处理thrift请求
用户通过继承brpc::ThriftService实现处理逻辑,既可以调用thrift生成的handler以直接复用原有的函数入口,也可以像protobuf服务那样直接读取request和设置response。
```
c++
// Implement User Thrift Service handler
class
MyThriftProtocolPbManner
:
public
brpc
::
ThriftService
{
class
MyThriftProtocol
:
public
brpc
::
ThriftService
{
public
:
void
ProcessThriftFramedRequest
(
const
brpc
::
Server
&
,
brpc
::
Controller
*
cntl
,
brpc
::
ThriftMessage
*
request
,
brpc
::
ThriftMessage
*
response
,
brpc
::
Thrift
Framed
Message
*
request
,
brpc
::
Thrift
Framed
Message
*
response
,
brpc
::
ThriftClosure
*
done
)
{
// This object helps you to call done->Run() in RAII style. If you need
// to process the request asynchronously, pass done_guard.release().
...
...
@@ -119,21 +114,23 @@ public:
example
::
EchoRequest
*
req
=
request
->
Cast
<
example
::
EchoRequest
>
();
example
::
EchoResponse
*
res
=
response
->
Cast
<
example
::
EchoResponse
>
();
// process with req and res
res
->
data
=
req
->
data
+
"user data"
;
LOG
(
INFO
)
<<
"success to process thrift request in brpc with pb manner"
;
// 通过cntl->thrift_method_name()获得被访问的方法名
if
(
_native_handler
)
{
_native_handler
->
Echo
(
*
res
,
*
req
);
}
else
{
res
->
data
=
req
->
data
+
"user data"
;
}
}
private
:
EchoServiceHandler
*
_native_handler
;
};
```
注册thrift
service并启动服务
实现好thrift service后,设置到ServerOptions.thrift_
service并启动服务
```
c++
brpc
::
Server
server
;
brpc
::
Server
server
;
brpc
::
ServerOptions
options
;
options
.
thrift_service
=
new
MyThriftProtocol
PbManner
;
options
.
thrift_service
=
new
MyThriftProtocol
;
options
.
idle_timeout_sec
=
FLAGS_idle_timeout_s
;
options
.
max_concurrency
=
FLAGS_max_concurrency
;
...
...
@@ -142,4 +139,4 @@ brpc::Server server;
LOG
(
ERROR
)
<<
"Fail to start EchoServer"
;
return
-
1
;
}
```
\ No newline at end of file
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment