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
888a763a
Commit
888a763a
authored
7 years ago
by
gejun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
replaced baidu::rpc:: with brpc::
parent
c9627fc4
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
22 changed files
with
114 additions
and
114 deletions
+114
-114
bvar_c++.md
docs/cn/bvar_c++.md
+1
-1
client.md
docs/cn/client.md
+0
-0
combo_channel.md
docs/cn/combo_channel.md
+20
-20
connections.md
docs/cn/connections.md
+1
-1
cpu_profiler.md
docs/cn/cpu_profiler.md
+10
-10
dummy_server.md
docs/cn/dummy_server.md
+5
-5
error_code.md
docs/cn/error_code.md
+1
-1
flags.md
docs/cn/flags.md
+2
-2
getting_started.md
docs/cn/getting_started.md
+0
-0
heap_profiler.md
docs/cn/heap_profiler.md
+7
-7
http_client.md
docs/cn/http_client.md
+13
-13
http_service.md
docs/cn/http_service.md
+17
-17
memcached_client.md
docs/cn/memcached_client.md
+6
-6
nshead_service.md
docs/cn/nshead_service.md
+11
-11
redis_client.md
docs/cn/redis_client.md
+13
-13
rpc_replay.md
docs/cn/rpc_replay.md
+3
-3
server.md
docs/cn/server.md
+0
-0
status.md
docs/cn/status.md
+3
-3
ub_client.md
docs/cn/ub_client.md
+0
-0
vars.md
docs/cn/vars.md
+1
-1
ns_access_interval.png
docs/images/ns_access_interval.png
+0
-0
ns_filter.jpg
docs/images/ns_filter.jpg
+0
-0
No files found.
docs/cn/bvar_c++.md
View file @
888a763a
...
@@ -163,7 +163,7 @@ class Variable {
...
@@ -163,7 +163,7 @@ class Variable {
$ cat bvar.echo_server.data
$ cat bvar.echo_server.data
rpc_server_8002_builtin_service_count : 20
rpc_server_8002_builtin_service_count : 20
rpc_server_8002_connection_count : 1
rpc_server_8002_connection_count : 1
rpc_server_8002_nshead_service_adaptor : b
aidu::
rpc::policy::NovaServiceAdaptor
rpc_server_8002_nshead_service_adaptor : brpc::policy::NovaServiceAdaptor
rpc_server_8002_service_count : 1
rpc_server_8002_service_count : 1
rpc_server_8002_start_time : 2015/07/24-21:08:03
rpc_server_8002_start_time : 2015/07/24-21:08:03
rpc_server_8002_uptime_ms : 14740954
rpc_server_8002_uptime_ms : 14740954
...
...
This diff is collapsed.
Click to expand it.
docs/cn/client.md
View file @
888a763a
This diff is collapsed.
Click to expand it.
docs/cn/combo_channel.md
View file @
888a763a
...
@@ -17,7 +17,7 @@ ParallelChannel (“pchan”)同时访问其包含的sub channel,并合并它
...
@@ -17,7 +17,7 @@ ParallelChannel (“pchan”)同时访问其包含的sub channel,并合并它
示例代码见
[
example/parallel_echo_c++
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/example/parallel_echo_c++/
)
。
示例代码见
[
example/parallel_echo_c++
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/example/parallel_echo_c++/
)
。
任何b
aidu::
rpc::ChannelBase的子类都可以加入ParallelChannel,包括ParallelChannel和其他组合Channel。用户可以设置ParallelChannelOptions.fail_limit来控制访问的最大失败次数(r31803前是ParallelChannel::set_fail_limit),当失败的访问达到这个数目时,RPC call会立刻结束而不等待超时。
任何brpc::ChannelBase的子类都可以加入ParallelChannel,包括ParallelChannel和其他组合Channel。用户可以设置ParallelChannelOptions.fail_limit来控制访问的最大失败次数(r31803前是ParallelChannel::set_fail_limit),当失败的访问达到这个数目时,RPC call会立刻结束而不等待超时。
当baidu-rpc >= 1.0.155.31351时,一个sub channel可多次加入同一个ParallelChannel。当你需要对同一个服务发起多次异步访问并等待它们完成的话,这很有用。
当baidu-rpc >= 1.0.155.31351时,一个sub channel可多次加入同一个ParallelChannel。当你需要对同一个服务发起多次异步访问并等待它们完成的话,这很有用。
...
@@ -30,13 +30,13 @@ ParallelChannel的内部结构大致如下:
...
@@ -30,13 +30,13 @@ ParallelChannel的内部结构大致如下:
可通过如下接口把sub channel插入ParallelChannel:
可通过如下接口把sub channel插入ParallelChannel:
```
c++
```
c++
int
AddChannel
(
b
aidu
::
rpc
::
ChannelBase
*
sub_channel
,
int
AddChannel
(
brpc
::
ChannelBase
*
sub_channel
,
ChannelOwnership
ownership
,
ChannelOwnership
ownership
,
CallMapper
*
call_mapper
,
CallMapper
*
call_mapper
,
ResponseMerger
*
response_merger
);
ResponseMerger
*
response_merger
);
```
```
当ownership为b
aidu::rpc::OWNS_CHANNEL时,sub_channel会在ParallelChannel析构时被删除。当baidu-rpc >= 1.0.155.31351时,由于一个sub channel可能会多次加入一个ParallelChannel,只要其中一个指明了ownership为baidu::
rpc::OWNS_CHANNEL,那个sub channel就会在ParallelChannel析构时被删除(一次)。
当ownership为b
rpc::OWNS_CHANNEL时,sub_channel会在ParallelChannel析构时被删除。当baidu-rpc >= 1.0.155.31351时,由于一个sub channel可能会多次加入一个ParallelChannel,只要其中一个指明了ownership为b
rpc::OWNS_CHANNEL,那个sub channel就会在ParallelChannel析构时被删除(一次)。
访问ParallelChannel时调用AddChannel是线程
**不安全**
的。
访问ParallelChannel时调用AddChannel是线程
**不安全**
的。
...
@@ -88,7 +88,7 @@ method/request/response:ParallelChannel.CallMethod()的参数。
...
@@ -88,7 +88,7 @@ method/request/response:ParallelChannel.CallMethod()的参数。
const
google
::
protobuf
::
MethodDescriptor
*
method
,
const
google
::
protobuf
::
MethodDescriptor
*
method
,
const
google
::
protobuf
::
Message
*
request
,
const
google
::
protobuf
::
Message
*
request
,
google
::
protobuf
::
Message
*
response
)
{
google
::
protobuf
::
Message
*
response
)
{
FooRequest
*
copied_req
=
b
aidu
::
rpc
::
Clone
<
FooRequest
>
(
request
);
FooRequest
*
copied_req
=
brpc
::
Clone
<
FooRequest
>
(
request
);
copied_req
->
set_xxx
(...);
copied_req
->
set_xxx
(...);
// 拷贝并修改request,最后的flag告诉pchan在RPC结束后删除Request和Response。
// 拷贝并修改request,最后的flag告诉pchan在RPC结束后删除Request和Response。
return
SubCall
(
method
,
copied_req
,
response
->
New
(),
DELETE_REQUEST
|
DELETE_RESPONSE
);
return
SubCall
(
method
,
copied_req
,
response
->
New
(),
DELETE_REQUEST
|
DELETE_RESPONSE
);
...
@@ -157,7 +157,7 @@ const Controller* sub(int index) const;
...
@@ -157,7 +157,7 @@ const Controller* sub(int index) const;
示例代码见
[
example/selective_echo_c++
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/example/selective_echo_c++/
)
。
示例代码见
[
example/selective_echo_c++
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/example/selective_echo_c++/
)
。
任何b
aidu::
rpc::ChannelBase的子类都可加入SelectiveChannel,包括SelectiveChannel和其他组合Channel。
任何brpc::ChannelBase的子类都可加入SelectiveChannel,包括SelectiveChannel和其他组合Channel。
SelectiveChannel的重试独立于其中的sub channel,当SelectiveChannel访问某个sub channel失败时(可能本身包含了重试),它会重试另外一个sub channel。
SelectiveChannel的重试独立于其中的sub channel,当SelectiveChannel访问某个sub channel失败时(可能本身包含了重试),它会重试另外一个sub channel。
...
@@ -168,10 +168,10 @@ SelectiveChannel的重试独立于其中的sub channel,当SelectiveChannel访
...
@@ -168,10 +168,10 @@ SelectiveChannel的重试独立于其中的sub channel,当SelectiveChannel访
SelectiveChannel的初始化和普通Channel基本一样,但Init不需要指定名字服务,因为SelectiveChannel面向sub channel并通过AddChannel动态添加,而普通Channel面向的server才记录在名字服务中。
SelectiveChannel的初始化和普通Channel基本一样,但Init不需要指定名字服务,因为SelectiveChannel面向sub channel并通过AddChannel动态添加,而普通Channel面向的server才记录在名字服务中。
```
c++
```
c++
#include
<baidu/
rpc/selective_channel.h>
#include
b
rpc/selective_channel.h>
...
...
b
aidu
::
rpc
::
SelectiveChannel
schan
;
brpc
::
SelectiveChannel
schan
;
b
aidu
::
rpc
::
ChannelOptions
schan_options
;
brpc
::
ChannelOptions
schan_options
;
schan_options
.
timeout_ms
=
...;
schan_options
.
timeout_ms
=
...;
schan_options
.
backup_request_ms
=
...;
schan_options
.
backup_request_ms
=
...;
schan_options
.
max_retry
=
...;
schan_options
.
max_retry
=
...;
...
@@ -194,7 +194,7 @@ if (schan.AddChannel(sub_channel, NULL/*ChannelHandle*/) != 0) { // 第二个
...
@@ -194,7 +194,7 @@ if (schan.AddChannel(sub_channel, NULL/*ChannelHandle*/) != 0) { // 第二个
-
和ParallelChannel不同,SelectiveChannel的AddChannel可在任意时刻调用,即使该SelectiveChannel正在被访问(下一次访问时生效)
-
和ParallelChannel不同,SelectiveChannel的AddChannel可在任意时刻调用,即使该SelectiveChannel正在被访问(下一次访问时生效)
-
SelectiveChannel总是own sub channel,这和ParallelChannel可选择ownership是不同的。
-
SelectiveChannel总是own sub channel,这和ParallelChannel可选择ownership是不同的。
-
如果AddChannel第二个参数不为空,会填入一个类型为b
aidu::
rpc::SelectiveChannel::ChannelHandle的值,这个handle可作为RemoveAndDestroyChannel的参数来动态删除一个channel。
-
如果AddChannel第二个参数不为空,会填入一个类型为brpc::SelectiveChannel::ChannelHandle的值,这个handle可作为RemoveAndDestroyChannel的参数来动态删除一个channel。
-
SelectiveChannel会用自身的超时覆盖sub channel初始化时指定的超时。比如某个sub channel的超时为100ms,SelectiveChannel的超时为500ms,实际访问时的超时是500ms,而不是100ms。
-
SelectiveChannel会用自身的超时覆盖sub channel初始化时指定的超时。比如某个sub channel的超时为100ms,SelectiveChannel的超时为500ms,实际访问时的超时是500ms,而不是100ms。
访问SelectiveChannel的方式和普通Channel是一样的。
访问SelectiveChannel的方式和普通Channel是一样的。
...
@@ -211,8 +211,8 @@ if (schan.AddChannel(sub_channel, NULL/*ChannelHandle*/) != 0) { // 第二个
...
@@ -211,8 +211,8 @@ if (schan.AddChannel(sub_channel, NULL/*ChannelHandle*/) != 0) { // 第二个
SelectiveChannel的创建和普通Channel类似,但不需要名字服务,而是通过AddChannel方法插入sub channel。下面的代码创建了一个SelectiveChannel,并插入三个访问不同bns的普通Channel。
SelectiveChannel的创建和普通Channel类似,但不需要名字服务,而是通过AddChannel方法插入sub channel。下面的代码创建了一个SelectiveChannel,并插入三个访问不同bns的普通Channel。
```
c++
```
c++
b
aidu
::
rpc
::
SelectiveChannel
channel
;
brpc
::
SelectiveChannel
channel
;
b
aidu
::
rpc
::
ChannelOptions
schan_options
;
brpc
::
ChannelOptions
schan_options
;
schan_options
.
timeout_ms
=
FLAGS_timeout_ms
;
schan_options
.
timeout_ms
=
FLAGS_timeout_ms
;
schan_options
.
backup_request_ms
=
FLAGS_backup_ms
;
schan_options
.
backup_request_ms
=
FLAGS_backup_ms
;
schan_options
.
max_retry
=
FLAGS_max_retry
;
schan_options
.
max_retry
=
FLAGS_max_retry
;
...
@@ -222,7 +222,7 @@ if (channel.Init("c_murmurhash", &schan_options) != 0) {
...
@@ -222,7 +222,7 @@ if (channel.Init("c_murmurhash", &schan_options) != 0) {
}
}
for
(
int
i
=
0
;
i
<
3
;
++
i
)
{
for
(
int
i
=
0
;
i
<
3
;
++
i
)
{
b
aidu
::
rpc
::
Channel
*
sub_channel
=
new
baidu
::
rpc
::
Channel
;
b
rpc
::
Channel
*
sub_channel
=
new
b
rpc
::
Channel
;
if
(
sub_channel
->
Init
(
bns_node_name
[
i
],
"rr"
,
NULL
)
!=
0
)
{
if
(
sub_channel
->
Init
(
bns_node_name
[
i
],
"rr"
,
NULL
)
!=
0
)
{
LOG
(
ERROR
)
<<
"Fail to init sub channel "
<<
i
;
LOG
(
ERROR
)
<<
"Fail to init sub channel "
<<
i
;
return
-
1
;
return
-
1
;
...
@@ -251,11 +251,11 @@ ParititonChannel只能处理一种分库方法,当用户需要多种分库方
...
@@ -251,11 +251,11 @@ ParititonChannel只能处理一种分库方法,当用户需要多种分库方
首先定制PartitionParser。这个例子中tag的形式是N/M,N代表分库的index,M是分库的个数。比如0/3代表一共3个分库,这是第一个。
首先定制PartitionParser。这个例子中tag的形式是N/M,N代表分库的index,M是分库的个数。比如0/3代表一共3个分库,这是第一个。
```
c++
```
c++
#include
<baidu/
rpc/partition_channel.h>
#include
b
rpc/partition_channel.h>
...
...
class
MyPartitionParser
:
public
b
aidu
::
rpc
::
PartitionParser
{
class
MyPartitionParser
:
public
brpc
::
PartitionParser
{
public
:
public
:
bool
ParseFromTag
(
const
std
::
string
&
tag
,
b
aidu
::
rpc
::
Partition
*
out
)
{
bool
ParseFromTag
(
const
std
::
string
&
tag
,
brpc
::
Partition
*
out
)
{
// "N/M" : #N partition of M partitions.
// "N/M" : #N partition of M partitions.
size_t
pos
=
tag
.
find_first_of
(
'/'
);
size_t
pos
=
tag
.
find_first_of
(
'/'
);
if
(
pos
==
std
::
string
::
npos
)
{
if
(
pos
==
std
::
string
::
npos
)
{
...
@@ -281,11 +281,11 @@ public:
...
@@ -281,11 +281,11 @@ public:
然后初始化PartitionChannel。
然后初始化PartitionChannel。
```
c++
```
c++
#include
<baidu/
rpc/partition_channel.h>
#include
b
rpc/partition_channel.h>
...
...
b
aidu
::
rpc
::
PartitionChannel
channel
;
brpc
::
PartitionChannel
channel
;
b
aidu
::
rpc
::
PartitionChannelOptions
options
;
brpc
::
PartitionChannelOptions
options
;
options
.
protocol
=
...;
// PartitionChannelOptions继承了ChannelOptions,后者有的前者也有
options
.
protocol
=
...;
// PartitionChannelOptions继承了ChannelOptions,后者有的前者也有
options
.
timeout_ms
=
...;
// 同上
options
.
timeout_ms
=
...;
// 同上
options
.
fail_limit
=
1
;
// PartitionChannel自己的选项,意思同ParalellChannel中的fail_limit。这里为1的意思是只要有1个分库访问失败,这次RPC就失败了。
options
.
fail_limit
=
1
;
// PartitionChannel自己的选项,意思同ParalellChannel中的fail_limit。这里为1的意思是只要有1个分库访问失败,这次RPC就失败了。
...
@@ -320,8 +320,8 @@ TRACE: 09-06 10:40:42: * 0 server.cpp:192] S[0]=0 S[1]=0 S[2]=0 [total=0]
...
@@ -320,8 +320,8 @@ TRACE: 09-06 10:40:42: * 0 server.cpp:192] S[0]=0 S[1]=0 S[2]=0 [total=0]
```
c++
```
c++
...
...
b
aidu
::
rpc
::
DynamicPartitionChannel
channel
;
brpc
::
DynamicPartitionChannel
channel
;
b
aidu
::
rpc
::
PartitionChannelOptions
options
;
brpc
::
PartitionChannelOptions
options
;
options
.
succeed_without_server
=
true
;
// 表示允许server_list在DynamicPartitionChannel.Init启动时为空,否则Init会失败。
options
.
succeed_without_server
=
true
;
// 表示允许server_list在DynamicPartitionChannel.Init启动时为空,否则Init会失败。
options
.
fail_limit
=
1
;
// 任何访问分库失败都认为RPC失败。调大这个数值可以使访问更宽松,比如等于2的话表示至少两个分库失败才算失败。
options
.
fail_limit
=
1
;
// 任何访问分库失败都认为RPC失败。调大这个数值可以使访问更宽松,比如等于2的话表示至少两个分库失败才算失败。
if
(
channel
.
Init
(
new
MyPartitionParser
(),
"file://server_list"
,
"rr"
,
&
options
)
!=
0
)
{
if
(
channel
.
Init
(
new
MyPartitionParser
(),
"file://server_list"
,
"rr"
,
&
options
)
!=
0
)
{
...
...
This diff is collapsed.
Click to expand it.
docs/cn/connections.md
View file @
888a763a
...
@@ -23,7 +23,7 @@ channel_short_socket_count: 0
...
@@ -23,7 +23,7 @@ channel_short_socket_count: 0
上述信息分为三段:
上述信息分为三段:
-
第一段是server接受(accept)的连接。
-
第一段是server接受(accept)的连接。
-
第二段是server与下游的单连接(使用b
aidu::
rpc::Channel建立),fd为-1的是虚拟连接,对应第三段中所有相同RemoteSide的连接。
-
第二段是server与下游的单连接(使用brpc::Channel建立),fd为-1的是虚拟连接,对应第三段中所有相同RemoteSide的连接。
-
第三段是server与下游的短连接或连接池(pooled connections),这些连接从属于第二段中的相同RemoteSide的虚拟连接。
-
第三段是server与下游的短连接或连接池(pooled connections),这些连接从属于第二段中的相同RemoteSide的虚拟连接。
表格标题的含义:
表格标题的含义:
...
...
This diff is collapsed.
Click to expand it.
docs/cn/cpu_profiler.md
View file @
888a763a
...
@@ -51,20 +51,20 @@ Wrote profile to /home/gejun/pprof/echo_server.1419501210.0.0.0.0
...
@@ -51,20 +51,20 @@ Wrote profile to /home/gejun/pprof/echo_server.1419501210.0.0.0.0
Removing funlockfile from all stack traces.
Removing funlockfile from all stack traces.
Total: 2946 samples
Total: 2946 samples
1161 39.4% 39.4% 1161 39.4% syscall
1161 39.4% 39.4% 1161 39.4% syscall
248 8.4% 47.8% 248 8.4% b
aidu::b
thread::TaskControl::steal_task
248 8.4% 47.8% 248 8.4% bthread::TaskControl::steal_task
227 7.7% 55.5% 227 7.7% writev
227 7.7% 55.5% 227 7.7% writev
87 3.0% 58.5% 88 3.0% ::cpp_alloc
87 3.0% 58.5% 88 3.0% ::cpp_alloc
74 2.5% 61.0% 74 2.5% __read_nocancel
74 2.5% 61.0% 74 2.5% __read_nocancel
46 1.6% 62.6% 48 1.6% tc_delete
46 1.6% 62.6% 48 1.6% tc_delete
42 1.4% 64.0% 42 1.4% b
aidu::
rpc::Socket::Address
42 1.4% 64.0% 42 1.4% brpc::Socket::Address
41 1.4% 65.4% 41 1.4% epoll_wait
41 1.4% 65.4% 41 1.4% epoll_wait
35 1.2% 66.6% 35 1.2% memcpy
35 1.2% 66.6% 35 1.2% memcpy
33 1.1% 67.7% 33 1.1% __pthread_getspecific
33 1.1% 67.7% 33 1.1% __pthread_getspecific
33 1.1% 68.8% 33 1.1% b
aidu::
rpc::Socket::Write
33 1.1% 68.8% 33 1.1% brpc::Socket::Write
33 1.1% 69.9% 33 1.1% epoll_ctl
33 1.1% 69.9% 33 1.1% epoll_ctl
28 1.0% 70.9% 42 1.4% b
aidu::
rpc::policy::ProcessRpcRequest
28 1.0% 70.9% 42 1.4% brpc::policy::ProcessRpcRequest
27 0.9% 71.8% 27 0.9% ba
idu
::IOBuf::_push_back_ref
27 0.9% 71.8% 27 0.9% ba
se
::IOBuf::_push_back_ref
27 0.9% 72.7% 27 0.9% b
aidu::b
thread::TaskGroup::ending_sched
27 0.9% 72.7% 27 0.9% bthread::TaskGroup::ending_sched
```
```
省略–text进入交互模式,如下图所示:
省略–text进入交互模式,如下图所示:
...
@@ -80,14 +80,14 @@ Welcome to pprof! For help, type 'help'.
...
@@ -80,14 +80,14 @@ Welcome to pprof! For help, type 'help'.
(pprof) top
(pprof) top
Total: 2954 samples
Total: 2954 samples
1099 37.2% 37.2% 1099 37.2% syscall
1099 37.2% 37.2% 1099 37.2% syscall
253 8.6% 45.8% 253 8.6% b
aidu::b
thread::TaskControl::steal_task
253 8.6% 45.8% 253 8.6% bthread::TaskControl::steal_task
240 8.1% 53.9% 240 8.1% writev
240 8.1% 53.9% 240 8.1% writev
90 3.0% 56.9% 90 3.0% ::cpp_alloc
90 3.0% 56.9% 90 3.0% ::cpp_alloc
67 2.3% 59.2% 67 2.3% __read_nocancel
67 2.3% 59.2% 67 2.3% __read_nocancel
47 1.6% 60.8% 47 1.6% ba
idu
::IOBuf::_push_back_ref
47 1.6% 60.8% 47 1.6% ba
se
::IOBuf::_push_back_ref
42 1.4% 62.2% 56 1.9% b
aidu::
rpc::policy::ProcessRpcRequest
42 1.4% 62.2% 56 1.9% brpc::policy::ProcessRpcRequest
41 1.4% 63.6% 41 1.4% epoll_wait
41 1.4% 63.6% 41 1.4% epoll_wait
38 1.3% 64.9% 38 1.3% epoll_ctl
38 1.3% 64.9% 38 1.3% epoll_ctl
37 1.3% 66.1% 37 1.3% memcpy
37 1.3% 66.1% 37 1.3% memcpy
35 1.2% 67.3% 35 1.2% b
aidu::
rpc::Socket::Address
35 1.2% 67.3% 35 1.2% brpc::Socket::Address
```
```
This diff is collapsed.
Click to expand it.
docs/cn/dummy_server.md
View file @
888a763a
...
@@ -12,14 +12,14 @@
...
@@ -12,14 +12,14 @@
你必须手动加入dummy server。你得先查看
[
Getting Started
](
getting_started.md
)
如何下载和编译baidu-rpc,然后在程序入口处加入如下代码片段:
你必须手动加入dummy server。你得先查看
[
Getting Started
](
getting_started.md
)
如何下载和编译baidu-rpc,然后在程序入口处加入如下代码片段:
```
c++
```
c++
#include
<baidu/
rpc/server.h>
#include
b
rpc/server.h>
...
...
int
main
()
{
int
main
()
{
...
...
b
aidu
::
rpc
::
Server
dummy_server
;
brpc
::
Server
dummy_server
;
b
aidu
::
rpc
::
ServerOptions
dummy_server_options
;
brpc
::
ServerOptions
dummy_server_options
;
dummy_server_options
.
num_threads
=
0
;
// 不要改变寄主程序的线程数。
dummy_server_options
.
num_threads
=
0
;
// 不要改变寄主程序的线程数。
if
(
dummy_server
.
Start
(
8888
/*port*/
,
&
dummy_server_options
)
!=
0
)
{
if
(
dummy_server
.
Start
(
8888
/*port*/
,
&
dummy_server_options
)
!=
0
)
{
LOG
(
FATAL
)
<<
"Fail to start dummy server"
;
LOG
(
FATAL
)
<<
"Fail to start dummy server"
;
...
@@ -32,13 +32,13 @@ int main() {
...
@@ -32,13 +32,13 @@ int main() {
r31803之后加入dummy server更容易了,只要一行:
r31803之后加入dummy server更容易了,只要一行:
```
c++
```
c++
#include
<baidu/
rpc/server.h>
#include
b
rpc/server.h>
...
...
int
main
()
{
int
main
()
{
...
...
b
aidu
::
rpc
::
StartDummyServerAt
(
8888
/*port*/
);
brpc
::
StartDummyServerAt
(
8888
/*port*/
);
...
...
}
}
```
```
This diff is collapsed.
Click to expand it.
docs/cn/error_code.md
View file @
888a763a
baidu-rpc使用
[
b
aidu::rpc::Controller
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/brpc/controller.h
)
设置一次RPC的参数和获取一次RPC的结果,ErrorCode()和ErrorText()是Controller的两个方法,分别是该次RPC的错误码和错误描述,只在RPC结束后才能访问,否则结果未定义。ErrorText()由Controller的基类google::protobuf::RpcController定义,ErrorCode()则是baidu::
rpc::Controller定义的。Controller还有个Failed()方法告知该次RPC是否失败,这三者的关系是:
baidu-rpc使用
[
b
rpc::Controller
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/brpc/controller.h
)
设置一次RPC的参数和获取一次RPC的结果,ErrorCode()和ErrorText()是Controller的两个方法,分别是该次RPC的错误码和错误描述,只在RPC结束后才能访问,否则结果未定义。ErrorText()由Controller的基类google::protobuf::RpcController定义,ErrorCode()则是b
rpc::Controller定义的。Controller还有个Failed()方法告知该次RPC是否失败,这三者的关系是:
-
当Failed()为true时,ErrorCode()一定不为0,ErrorText()是非空的错误描述
-
当Failed()为true时,ErrorCode()一定不为0,ErrorText()是非空的错误描述
-
当Failed()为false时,ErrorCode()一定为0,ErrorText()是未定义的(目前在baidu-rpc中会为空,但你最好不要依赖这个事实)
-
当Failed()为false时,ErrorCode()一定为0,ErrorText()是未定义的(目前在baidu-rpc中会为空,但你最好不要依赖这个事实)
...
...
This diff is collapsed.
Click to expand it.
docs/cn/flags.md
View file @
888a763a
...
@@ -117,7 +117,7 @@ r31658之后支持可视化地修改,在浏览器上访问时将看到(R)下
...
@@ -117,7 +117,7 @@ r31658之后支持可视化地修改,在浏览器上访问时将看到(R)下
```
c++
```
c++
DEFINE_bool
(
hex_log_id
,
false
,
"Show log_id in hexadecimal"
);
DEFINE_bool
(
hex_log_id
,
false
,
"Show log_id in hexadecimal"
);
BAIDU_RPC_VALIDATE_GFLAG
(
hex_log_id
,
b
aidu
::
rpc
::
PassValidate
/*always true*/
);
BAIDU_RPC_VALIDATE_GFLAG
(
hex_log_id
,
brpc
::
PassValidate
/*always true*/
);
```
```
这个flag是单纯的开关,修改后不需要更新其他数据(没有处理逻辑),代码中前面看到true后面看到false也不会产生什么后果(不需要线程同步),所以我们让其默认可重载。
这个flag是单纯的开关,修改后不需要更新其他数据(没有处理逻辑),代码中前面看到true后面看到false也不会产生什么后果(不需要线程同步),所以我们让其默认可重载。
...
@@ -126,7 +126,7 @@ BAIDU_RPC_VALIDATE_GFLAG(hex_log_id, baidu::rpc::PassValidate/*always true*/);
...
@@ -126,7 +126,7 @@ BAIDU_RPC_VALIDATE_GFLAG(hex_log_id, baidu::rpc::PassValidate/*always true*/);
```
c++
```
c++
DEFINE_int32
(
health_check_interval
,
3
,
"seconds between consecutive health-checkings"
);
DEFINE_int32
(
health_check_interval
,
3
,
"seconds between consecutive health-checkings"
);
BAIDU_RPC_VALIDATE_GFLAG
(
health_check_interval
,
b
aidu
::
rpc
::
PositiveInteger
);
BAIDU_RPC_VALIDATE_GFLAG
(
health_check_interval
,
brpc
::
PositiveInteger
);
```
```
以上操作都可以在命令行中进行:
以上操作都可以在命令行中进行:
...
...
This diff is collapsed.
Click to expand it.
docs/cn/getting_started.md
View file @
888a763a
This diff is collapsed.
Click to expand it.
docs/cn/heap_profiler.md
View file @
888a763a
...
@@ -74,13 +74,13 @@ Total: 38.9 MB
...
@@ -74,13 +74,13 @@ Total: 38.9 MB
0.
0 0.0% 100.0% 0.5 1.3% __do_global_ctors_aux
0.
0 0.0% 100.0% 0.5 1.3% __do_global_ctors_aux
0.
0 0.0% 100.0% 1.6 4.2% _end
0.
0 0.0% 100.0% 1.6 4.2% _end
0.
0 0.0% 100.0% 0.5 1.3% _init
0.
0 0.0% 100.0% 0.5 1.3% _init
0.
0 0.0% 100.0% 0.6 1.5% b
aidu::
rpc::CloseIdleConnections
0.
0 0.0% 100.0% 0.6 1.5% brpc::CloseIdleConnections
0.
0 0.0% 100.0% 1.1 2.9% b
aidu::
rpc::GlobalUpdate
0.
0 0.0% 100.0% 1.1 2.9% brpc::GlobalUpdate
0.
0 0.0% 100.0% 0.6 1.5% b
aidu::
rpc::PProfService::heap
0.
0 0.0% 100.0% 0.6 1.5% brpc::PProfService::heap
0.
0 0.0% 100.0% 1.9 4.9% b
aidu::
rpc::Socket::Create
0.
0 0.0% 100.0% 1.9 4.9% brpc::Socket::Create
0.
0 0.0% 100.0% 2.9 7.4% b
aidu::
rpc::Socket::Write
0.
0 0.0% 100.0% 2.9 7.4% brpc::Socket::Write
0.
0 0.0% 100.0% 3.8 9.7% b
aidu::
rpc::Span::CreateServerSpan
0.
0 0.0% 100.0% 3.8 9.7% brpc::Span::CreateServerSpan
0.
0 0.0% 100.0% 1.4 3.5% b
aidu::
rpc::SpanQueue::Push
0.
0 0.0% 100.0% 1.4 3.5% brpc::SpanQueue::Push
0.
0 0.0% 100.0% 1.9 4.8% base::ObjectPool
0.
0 0.0% 100.0% 1.9 4.8% base::ObjectPool
0.
0 0.0% 100.0% 0.8 2.0% base::ResourcePool
0.
0 0.0% 100.0% 0.8 2.0% base::ResourcePool
0.
0 0.0% 100.0% 1.0 2.6% base::iobuf::tls_block
0.
0 0.0% 100.0% 1.0 2.6% base::iobuf::tls_block
...
...
This diff is collapsed.
Click to expand it.
docs/cn/http_client.md
View file @
888a763a
...
@@ -2,13 +2,13 @@ http client的例子见[example/http_c++](http://icode.baidu.com/repo/baidu/open
...
@@ -2,13 +2,13 @@ http client的例子见[example/http_c++](http://icode.baidu.com/repo/baidu/open
# 创建Channel
# 创建Channel
b
aidu::
rpc::Channel可访问HTTP服务,ChannelOptions.protocol须指定为PROTOCOL_HTTP。
brpc::Channel可访问HTTP服务,ChannelOptions.protocol须指定为PROTOCOL_HTTP。
设定为HTTP协议后,
`Channel::Init`
的第一个参数可为任意合法的URL。注意:允许任意URL是为了省去用户取出host和port的麻烦,
`Channel::Init`
只用其中的host及port,其他部分都会丢弃。
设定为HTTP协议后,
`Channel::Init`
的第一个参数可为任意合法的URL。注意:允许任意URL是为了省去用户取出host和port的麻烦,
`Channel::Init`
只用其中的host及port,其他部分都会丢弃。
```
c++
```
c++
b
aidu
::
rpc
::
ChannelOptions
options
;
brpc
::
ChannelOptions
options
;
options
.
protocol
=
b
aidu
::
rpc
::
PROTOCOL_HTTP
;
options
.
protocol
=
brpc
::
PROTOCOL_HTTP
;
if
(
channel
.
Init
(
"www.baidu.com"
/*any url*/
,
&
options
)
!=
0
)
{
if
(
channel
.
Init
(
"www.baidu.com"
/*any url*/
,
&
options
)
!=
0
)
{
LOG
(
ERROR
)
<<
"Fail to initialize channel"
;
LOG
(
ERROR
)
<<
"Fail to initialize channel"
;
return
-
1
;
return
-
1
;
...
@@ -20,7 +20,7 @@ http channel也支持bns地址。
...
@@ -20,7 +20,7 @@ http channel也支持bns地址。
# GET
# GET
```
c++
```
c++
b
aidu
::
rpc
::
Controller
cntl
;
brpc
::
Controller
cntl
;
cntl
.
http_request
().
uri
()
=
"www.baidu.com/index.html"
;
// 设置为待访问的URL
cntl
.
http_request
().
uri
()
=
"www.baidu.com/index.html"
;
// 设置为待访问的URL
channel
.
CallMethod
(
NULL
,
&
cntl
,
NULL
,
NULL
,
NULL
/*done*/
);
channel
.
CallMethod
(
NULL
,
&
cntl
,
NULL
,
NULL
,
NULL
/*done*/
);
```
```
...
@@ -34,9 +34,9 @@ HTTP和protobuf无关,所以除了Controller和done,CallMethod的其他参
...
@@ -34,9 +34,9 @@ HTTP和protobuf无关,所以除了Controller和done,CallMethod的其他参
默认的HTTP Method为GET,如果需要做POST,则需要设置。待POST的数据应置入request_attachment(),它(
[
base::IOBuf
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/base/iobuf.h
)
)可以直接append std::string或char
*
默认的HTTP Method为GET,如果需要做POST,则需要设置。待POST的数据应置入request_attachment(),它(
[
base::IOBuf
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/base/iobuf.h
)
)可以直接append std::string或char
*
```
c++
```
c++
b
aidu
::
rpc
::
Controller
cntl
;
brpc
::
Controller
cntl
;
cntl
.
http_request
().
uri
()
=
"..."
;
// 设置为待访问的URL
cntl
.
http_request
().
uri
()
=
"..."
;
// 设置为待访问的URL
cntl
.
http_request
().
set_method
(
b
aidu
::
rpc
::
HTTP_METHOD_POST
);
cntl
.
http_request
().
set_method
(
brpc
::
HTTP_METHOD_POST
);
cntl
.
request_attachment
().
append
(
"{
\"
message
\"
:
\"
hello world!
\"
}"
);
cntl
.
request_attachment
().
append
(
"{
\"
message
\"
:
\"
hello world!
\"
}"
);
channel
.
CallMethod
(
NULL
,
&
cntl
,
NULL
,
NULL
,
NULL
/*done*/
);
channel
.
CallMethod
(
NULL
,
&
cntl
,
NULL
,
NULL
,
NULL
/*done*/
);
```
```
...
@@ -44,9 +44,9 @@ channel.CallMethod(NULL, &cntl, NULL, NULL, NULL/*done*/);
...
@@ -44,9 +44,9 @@ channel.CallMethod(NULL, &cntl, NULL, NULL, NULL/*done*/);
需要大量打印过程的body建议使用base::IOBufBuilder,它的用法和std::ostringstream是一样的。对于有大量对象要打印的场景,IOBufBuilder会简化代码,并且效率也更高。
需要大量打印过程的body建议使用base::IOBufBuilder,它的用法和std::ostringstream是一样的。对于有大量对象要打印的场景,IOBufBuilder会简化代码,并且效率也更高。
```
c++
```
c++
b
aidu
::
rpc
::
Controller
cntl
;
brpc
::
Controller
cntl
;
cntl
.
http_request
().
uri
()
=
"..."
;
// 设置为待访问的URL
cntl
.
http_request
().
uri
()
=
"..."
;
// 设置为待访问的URL
cntl
.
http_request
().
set_method
(
b
aidu
::
rpc
::
HTTP_METHOD_POST
);
cntl
.
http_request
().
set_method
(
brpc
::
HTTP_METHOD_POST
);
base
::
IOBufBuilder
os
;
base
::
IOBufBuilder
os
;
os
<<
"A lot of printing"
<<
printable_objects
<<
...;
os
<<
"A lot of printing"
<<
printable_objects
<<
...;
os
.
move_to
(
cntl
.
request_attachment
());
os
.
move_to
(
cntl
.
request_attachment
());
...
@@ -108,7 +108,7 @@ cntl->http_request().uri().SetQuery("Foo", "value");
...
@@ -108,7 +108,7 @@ cntl->http_request().uri().SetQuery("Foo", "value");
```
```
设置HTTP方法
设置HTTP方法
```
c++
```
c++
cntl
->
http_request
().
set_method
(
b
aidu
::
rpc
::
HTTP_METHOD_POST
);
cntl
->
http_request
().
set_method
(
brpc
::
HTTP_METHOD_POST
);
```
```
设置url
设置url
```
c++
```
c++
...
@@ -149,19 +149,19 @@ Notes on http header:
...
@@ -149,19 +149,19 @@ Notes on http header:
# 压缩request body
# 压缩request body
在r33877后,调用Controller::set_request_compress_type(b
aidu::
rpc::COMPRESS_TYPE_GZIP)可将http body用gzip压缩,并设置"Content-Encoding"为"gzip"。
在r33877后,调用Controller::set_request_compress_type(brpc::COMPRESS_TYPE_GZIP)可将http body用gzip压缩,并设置"Content-Encoding"为"gzip"。
# 解压response body
# 解压response body
出于通用性考虑且解压代码不复杂,baidu-rpc不会自动解压response body,用户可以自己做,方法如下:
出于通用性考虑且解压代码不复杂,baidu-rpc不会自动解压response body,用户可以自己做,方法如下:
```
c++
```
c++
#include
<baidu/
rpc/policy/gzip_compress.h>
#include
b
rpc/policy/gzip_compress.h>
...
...
const
std
::
string
*
encoding
=
cntl
->
http_response
().
GetHeader
(
"Content-Encoding"
);
const
std
::
string
*
encoding
=
cntl
->
http_response
().
GetHeader
(
"Content-Encoding"
);
if
(
encoding
!=
NULL
&&
*
encoding
==
"gzip"
)
{
if
(
encoding
!=
NULL
&&
*
encoding
==
"gzip"
)
{
base
::
IOBuf
uncompressed
;
base
::
IOBuf
uncompressed
;
if
(
!
b
aidu
::
rpc
::
policy
::
GzipDecompress
(
cntl
->
response_attachment
(),
&
uncompressed
))
{
if
(
!
brpc
::
policy
::
GzipDecompress
(
cntl
->
response_attachment
(),
&
uncompressed
))
{
LOG
(
ERROR
)
<<
"Fail to un-gzip response body"
;
LOG
(
ERROR
)
<<
"Fail to un-gzip response body"
;
return
;
return
;
}
}
...
@@ -181,7 +181,7 @@ r33796后baidu-rpc client支持在读取完body前就结束RPC,让用户在RPC
...
@@ -181,7 +181,7 @@ r33796后baidu-rpc client支持在读取完body前就结束RPC,让用户在RPC
1.
首先实现ProgressiveReader,接口如下:
1.
首先实现ProgressiveReader,接口如下:
```c++
```c++
#include
<baidu/
rpc/progressive_reader.h>
#include
b
rpc/progressive_reader.h>
...
...
class ProgressiveReader {
class ProgressiveReader {
public:
public:
...
...
This diff is collapsed.
Click to expand it.
docs/cn/http_service.md
View file @
888a763a
...
@@ -29,8 +29,8 @@ public:
...
@@ -29,8 +29,8 @@ public:
const
HttpRequest
*
/*request*/
,
const
HttpRequest
*
/*request*/
,
HttpResponse
*
/*response*/
,
HttpResponse
*
/*response*/
,
google
::
protobuf
::
Closure
*
done
)
{
google
::
protobuf
::
Closure
*
done
)
{
b
aidu
::
rpc
::
ClosureGuard
done_guard
(
done
);
brpc
::
ClosureGuard
done_guard
(
done
);
b
aidu
::
rpc
::
Controller
*
cntl
=
static_cast
<
baidu
::
rpc
::
Controller
*>
(
cntl_base
);
b
rpc
::
Controller
*
cntl
=
static_cast
<
b
rpc
::
Controller
*>
(
cntl_base
);
// 这里返回纯文本。
// 这里返回纯文本。
cntl
->
http_response
().
set_content_type
(
"text/plain"
);
cntl
->
http_response
().
set_content_type
(
"text/plain"
);
...
@@ -38,7 +38,7 @@ public:
...
@@ -38,7 +38,7 @@ public:
// 把请求的query-string和body打印出来,作为回复内容。
// 把请求的query-string和body打印出来,作为回复内容。
base
::
IOBufBuilder
os
;
base
::
IOBufBuilder
os
;
os
<<
"queries:"
;
os
<<
"queries:"
;
for
(
b
aidu
::
rpc
::
URI
::
QueryIterator
it
=
cntl
->
http_request
().
uri
().
QueryBegin
();
for
(
brpc
::
URI
::
QueryIterator
it
=
cntl
->
http_request
().
uri
().
QueryBegin
();
it
!=
cntl
->
http_request
().
uri
().
QueryEnd
();
++
it
)
{
it
!=
cntl
->
http_request
().
uri
().
QueryEnd
();
++
it
)
{
os
<<
' '
<<
it
->
first
<<
'='
<<
it
->
second
;
os
<<
' '
<<
it
->
first
<<
'='
<<
it
->
second
;
}
}
...
@@ -87,8 +87,8 @@ public:
...
@@ -87,8 +87,8 @@ public:
const
HttpRequest
*
/*request*/
,
const
HttpRequest
*
/*request*/
,
HttpResponse
*
/*response*/
,
HttpResponse
*
/*response*/
,
google
::
protobuf
::
Closure
*
done
)
{
google
::
protobuf
::
Closure
*
done
)
{
b
aidu
::
rpc
::
ClosureGuard
done_guard
(
done
);
brpc
::
ClosureGuard
done_guard
(
done
);
b
aidu
::
rpc
::
Controller
*
cntl
=
static_cast
<
baidu
::
rpc
::
Controller
*>
(
cntl_base
);
b
rpc
::
Controller
*
cntl
=
static_cast
<
b
rpc
::
Controller
*>
(
cntl_base
);
cntl
->
response_attachment
().
append
(
"Getting file: "
);
cntl
->
response_attachment
().
append
(
"Getting file: "
);
cntl
->
response_attachment
().
append
(
cntl
->
http_request
().
unresolved_path
());
cntl
->
response_attachment
().
append
(
cntl
->
http_request
().
unresolved_path
());
}
}
...
@@ -135,7 +135,7 @@ service QueueService {
...
@@ -135,7 +135,7 @@ service QueueService {
```
c++
```
c++
// r33521前星号只能出现在最后
// r33521前星号只能出现在最后
if
(
server
.
AddService
(
&
queue_svc
,
if
(
server
.
AddService
(
&
queue_svc
,
b
aidu
::
rpc
::
SERVER_DOESNT_OWN_SERVICE
,
brpc
::
SERVER_DOESNT_OWN_SERVICE
,
"/v1/queue/start => start,"
"/v1/queue/start => start,"
"/v1/queue/stop => stop,"
"/v1/queue/stop => stop,"
"/v1/queue/stats/* => get_stats"
)
!=
0
)
{
"/v1/queue/stats/* => get_stats"
)
!=
0
)
{
...
@@ -145,7 +145,7 @@ if (server.AddService(&queue_svc,
...
@@ -145,7 +145,7 @@ if (server.AddService(&queue_svc,
// r33521后星号可出现在中间
// r33521后星号可出现在中间
if
(
server
.
AddService
(
&
queue_svc
,
if
(
server
.
AddService
(
&
queue_svc
,
b
aidu
::
rpc
::
SERVER_DOESNT_OWN_SERVICE
,
brpc
::
SERVER_DOESNT_OWN_SERVICE
,
"/v1/*/start => start,"
"/v1/*/start => start,"
"/v1/*/stop => stop,"
"/v1/*/stop => stop,"
"*.data => download_data"
)
!=
0
)
{
"*.data => download_data"
)
!=
0
)
{
...
@@ -230,19 +230,19 @@ status code是http response特有的字段,标记http请求的完成情况。
...
@@ -230,19 +230,19 @@ status code是http response特有的字段,标记http请求的完成情况。
```
c++
```
c++
// Get Status Code
// Get Status Code
if
(
cntl
->
http_response
().
status_code
()
==
b
aidu
::
rpc
::
HTTP_STATUS_NOT_FOUND
)
{
if
(
cntl
->
http_response
().
status_code
()
==
brpc
::
HTTP_STATUS_NOT_FOUND
)
{
LOG
(
FATAL
)
<<
"FAILED: "
<<
controller
.
http_response
().
reason_phrase
();
LOG
(
FATAL
)
<<
"FAILED: "
<<
controller
.
http_response
().
reason_phrase
();
}
}
...
...
// Set Status code
// Set Status code
cntl
->
http_response
().
set_status_code
(
b
aidu
::
rpc
::
HTTP_STATUS_INTERNAL_SERVER_ERROR
);
cntl
->
http_response
().
set_status_code
(
brpc
::
HTTP_STATUS_INTERNAL_SERVER_ERROR
);
cntl
->
http_response
().
set_status_code
(
b
aidu
::
rpc
::
HTTP_STATUS_INTERNAL_SERVER_ERROR
,
"My explanation of the error..."
);
cntl
->
http_response
().
set_status_code
(
brpc
::
HTTP_STATUS_INTERNAL_SERVER_ERROR
,
"My explanation of the error..."
);
```
```
以下代码在302错误时重定向:
以下代码在302错误时重定向:
```
c++
```
c++
cntl
->
http_response
().
set_status_code
(
b
aidu
::
rpc
::
HTTP_STATUS_FOUND
);
cntl
->
http_response
().
set_status_code
(
brpc
::
HTTP_STATUS_FOUND
);
cntl
->
http_response
().
SetHeader
(
"Location"
,
"http://bj.bs.bae.baidu.com/family/image001(4979).jpg"
);
cntl
->
http_response
().
SetHeader
(
"Location"
,
"http://bj.bs.bae.baidu.com/family/image001(4979).jpg"
);
```
```
...
@@ -270,19 +270,19 @@ cntl->http_request().uri().SetQuery("time", "2015/1/2");
...
@@ -270,19 +270,19 @@ cntl->http_request().uri().SetQuery("time", "2015/1/2");
http服务常对http body进行压缩,对于文本网页可以有效减少传输时间,加快页面的展现速度。
http服务常对http body进行压缩,对于文本网页可以有效减少传输时间,加快页面的展现速度。
在r33093后,调用Controller::set_response_compress_type(b
aidu::
rpc::COMPRESS_TYPE_GZIP)将尝试用gzip压缩http body,并设置"Content-Encoding"为"gzip"。“尝试”指的是如果请求中没有设置Accept-encoding或不包含gzip,压缩不会进行。比如curl不加--compressed时是不支持压缩的,这时server端总是会返回不压缩的结果。
在r33093后,调用Controller::set_response_compress_type(brpc::COMPRESS_TYPE_GZIP)将尝试用gzip压缩http body,并设置"Content-Encoding"为"gzip"。“尝试”指的是如果请求中没有设置Accept-encoding或不包含gzip,压缩不会进行。比如curl不加--compressed时是不支持压缩的,这时server端总是会返回不压缩的结果。
# 解压request body
# 解压request body
出于通用性考虑且解压代码不复杂,baidu-rpc不会自动解压request body,用户可以自己做,方法如下:
出于通用性考虑且解压代码不复杂,baidu-rpc不会自动解压request body,用户可以自己做,方法如下:
```
c++
```
c++
#include
<baidu/
rpc/policy/gzip_compress.h>
#include
b
rpc/policy/gzip_compress.h>
...
...
const
std
::
string
*
encoding
=
cntl
->
http_request
().
GetHeader
(
"Content-Encoding"
);
const
std
::
string
*
encoding
=
cntl
->
http_request
().
GetHeader
(
"Content-Encoding"
);
if
(
encoding
!=
NULL
&&
*
encoding
==
"gzip"
)
{
if
(
encoding
!=
NULL
&&
*
encoding
==
"gzip"
)
{
base
::
IOBuf
uncompressed
;
base
::
IOBuf
uncompressed
;
if
(
!
b
aidu
::
rpc
::
policy
::
GzipDecompress
(
cntl
->
request_attachment
(),
&
uncompressed
))
{
if
(
!
brpc
::
policy
::
GzipDecompress
(
cntl
->
request_attachment
(),
&
uncompressed
))
{
LOG
(
ERROR
)
<<
"Fail to un-gzip request body"
;
LOG
(
ERROR
)
<<
"Fail to un-gzip request body"
;
return
;
return
;
}
}
...
@@ -351,13 +351,13 @@ r33796前baidu-rpc server不适合发送超大或无限长的body。r33796后bai
...
@@ -351,13 +351,13 @@ r33796前baidu-rpc server不适合发送超大或无限长的body。r33796后bai
\1
. 调用Controller::CreateProgressiveAttachment()创建可持续发送的body。
\1
. 调用Controller::CreateProgressiveAttachment()创建可持续发送的body。
`boost::intrusive_ptr<b
aidu::
rpc::ProgressiveAttachment> pa(cntl->CreateProgressiveAttachment());`
`boost::intrusive_ptr<brpc::ProgressiveAttachment> pa(cntl->CreateProgressiveAttachment());`
返回的ProgressiveAttachment对象需要用boost::intrusive_ptr
<>
管理,定义在
<baidu
/
rpc
/
progressive_attachment
.
h
>
中。
返回的ProgressiveAttachment对象需要用boost::intrusive_ptr
<>
管理,定义在
b
rpc/progressive_attachment.h>中。
\2
. 调用ProgressiveAttachment::Write()发送数据。如果写入发生在server回调结束前,发送的数据将会被缓存直到回调结束发送了header部分后才会开始发送数据。如果写入发生在server回调结束后,发送的数据将立刻以chunked mode写出。
\2
. 调用ProgressiveAttachment::Write()发送数据。如果写入发生在server回调结束前,发送的数据将会被缓存直到回调结束发送了header部分后才会开始发送数据。如果写入发生在server回调结束后,发送的数据将立刻以chunked mode写出。
\3
. 发送完毕后确保所有的boost::intrusive_ptr
<b
aidu::
rpc::ProgressiveAttachment>
都析构了。
\3
. 发送完毕后确保所有的boost::intrusive_ptr
<brpc::ProgressiveAttachment>
都析构了。
# 持续接收
# 持续接收
...
...
This diff is collapsed.
Click to expand it.
docs/cn/memcached_client.md
View file @
888a763a
...
@@ -16,11 +16,11 @@
...
@@ -16,11 +16,11 @@
创建一个访问memcached的Channel:
创建一个访问memcached的Channel:
```
c++
```
c++
#include
<baidu/
rpc/memcache.h>
#include
b
rpc/memcache.h>
#include
<baidu/
rpc/channel.h>
#include
b
rpc/channel.h>
ChannelOptions
options
;
ChannelOptions
options
;
options
.
protocol
=
b
aidu
::
rpc
::
PROTOCOL_MEMCACHE
;
options
.
protocol
=
brpc
::
PROTOCOL_MEMCACHE
;
if
(
channel
.
Init
(
"0.0.0.0:11211"
,
&
options
)
!=
0
)
{
// 11211是memcached的默认端口
if
(
channel
.
Init
(
"0.0.0.0:11211"
,
&
options
)
!=
0
)
{
// 11211是memcached的默认端口
LOG
(
FATAL
)
<<
"Fail to init channel to memcached"
;
LOG
(
FATAL
)
<<
"Fail to init channel to memcached"
;
return
-
1
;
return
-
1
;
...
@@ -34,9 +34,9 @@ if (channel.Init("0.0.0.0:11211", &options) != 0) { // 11211是memcached的默
...
@@ -34,9 +34,9 @@ if (channel.Init("0.0.0.0:11211", &options) != 0) { // 11211是memcached的默
```
c++
```
c++
// 写入key="hello" value="world" flags=0xdeadbeef,10秒失效,无视cas。
// 写入key="hello" value="world" flags=0xdeadbeef,10秒失效,无视cas。
b
aidu
::
rpc
::
MemcacheRequest
request
;
brpc
::
MemcacheRequest
request
;
b
aidu
::
rpc
::
MemcacheResponse
response
;
brpc
::
MemcacheResponse
response
;
b
aidu
::
rpc
::
Controller
cntl
;
brpc
::
Controller
cntl
;
if
(
!
request
.
Set
(
"hello"
,
"world"
,
0xdeadbeef
/*flags*/
,
10
/*expiring seconds*/
,
0
/*ignore cas*/
))
{
if
(
!
request
.
Set
(
"hello"
,
"world"
,
0xdeadbeef
/*flags*/
,
10
/*expiring seconds*/
,
0
/*ignore cas*/
))
{
LOG
(
FATAL
)
<<
"Fail to SET request"
;
LOG
(
FATAL
)
<<
"Fail to SET request"
;
return
-
1
;
return
-
1
;
...
...
This diff is collapsed.
Click to expand it.
docs/cn/nshead_service.md
View file @
888a763a
...
@@ -85,8 +85,8 @@ public:
...
@@ -85,8 +85,8 @@ public:
const
EchoRequest
*
request
,
const
EchoRequest
*
request
,
EchoResponse
*
response
,
EchoResponse
*
response
,
google
::
protobuf
::
Closure
*
done
)
{
google
::
protobuf
::
Closure
*
done
)
{
b
aidu
::
rpc
::
ClosureGuard
done_guard
(
done
);
brpc
::
ClosureGuard
done_guard
(
done
);
b
aidu
::
rpc
::
Controller
*
cntl
=
static_cast
<
baidu
::
rpc
::
Controller
*>
(
cntl_base
);
b
rpc
::
Controller
*
cntl
=
static_cast
<
b
rpc
::
Controller
*>
(
cntl_base
);
// 填充response。
// 填充response。
response
->
set_message
(
request
->
message
());
response
->
set_message
(
request
->
message
());
...
@@ -99,15 +99,15 @@ public:
...
@@ -99,15 +99,15 @@ public:
const
MultiRequests
*
request
,
const
MultiRequests
*
request
,
MultiResponses
*
response
,
MultiResponses
*
response
,
google
::
protobuf
::
Closure
*
done
)
{
google
::
protobuf
::
Closure
*
done
)
{
b
aidu
::
rpc
::
ClosureGuard
done_guard
(
done
);
brpc
::
ClosureGuard
done_guard
(
done
);
b
aidu
::
rpc
::
Controller
*
cntl
=
static_cast
<
baidu
::
rpc
::
Controller
*>
(
cntl_base
);
b
rpc
::
Controller
*
cntl
=
static_cast
<
b
rpc
::
Controller
*>
(
cntl_base
);
// 填充response。response是我们定义的包含所有idl response的消息。
// 填充response。response是我们定义的包含所有idl response的消息。
response
->
mutable_res1
()
->
set_message
(
request
->
req1
().
message
());
response
->
mutable_res1
()
->
set_message
(
request
->
req1
().
message
());
response
->
mutable_res2
()
->
set_message
(
request
->
req2
().
message
());
response
->
mutable_res2
()
->
set_message
(
request
->
req2
().
message
());
// 告诉RPC有多个request和response。
// 告诉RPC有多个request和response。
cntl
->
set_idl_names
(
b
aidu
::
rpc
::
idl_multi_req_multi_res
);
cntl
->
set_idl_names
(
brpc
::
idl_multi_req_multi_res
);
// 对应idl方法的返回值。
// 对应idl方法的返回值。
cntl
->
set_idl_result
(
17
);
cntl
->
set_idl_result
(
17
);
...
@@ -118,10 +118,10 @@ public:
...
@@ -118,10 +118,10 @@ public:
## 设置ServerOptions.nshead_service
## 设置ServerOptions.nshead_service
```
c++
```
c++
#include
<baidu/
rpc/ubrpc2pb_protocol.h>
#include
b
rpc/ubrpc2pb_protocol.h>
...
...
b
aidu
::
rpc
::
ServerOptions
option
;
brpc
::
ServerOptions
option
;
option
.
nshead_service
=
new
b
aidu
::
rpc
::
policy
::
UbrpcCompackAdaptor
;
// mcpack2用UbrpcMcpack2Adaptor
option
.
nshead_service
=
new
brpc
::
policy
::
UbrpcCompackAdaptor
;
// mcpack2用UbrpcMcpack2Adaptor
```
```
例子见
[
example/echo_c++_ubrpc_compack
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/example/echo_c++_ubrpc_compack/
)
。
例子见
[
example/echo_c++_ubrpc_compack
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/example/echo_c++_ubrpc_compack/
)
。
...
@@ -147,15 +147,15 @@ public:
...
@@ -147,15 +147,15 @@ public:
virtual
~
NsheadService
();
virtual
~
NsheadService
();
// 实现这个方法来处理nshead请求。注意这个方法可能在调用时controller->Failed()已经为true了。
// 实现这个方法来处理nshead请求。注意这个方法可能在调用时controller->Failed()已经为true了。
// 原因可能是Server.Stop()被调用正在退出(错误码是b
aidu::
rpc::ELOGOFF)
// 原因可能是Server.Stop()被调用正在退出(错误码是brpc::ELOGOFF)
// 或触发了ServerOptions.max_concurrency(错误码是b
aidu::
rpc::ELIMIT)
// 或触发了ServerOptions.max_concurrency(错误码是brpc::ELIMIT)
// 在这种情况下,这个方法应该通过返回一个代表错误的response让客户端知道这些错误。
// 在这种情况下,这个方法应该通过返回一个代表错误的response让客户端知道这些错误。
// Parameters:
// Parameters:
// server The server receiving the request.
// server The server receiving the request.
// controller Contexts of the request.
// controller Contexts of the request.
// request The nshead request received.
// request The nshead request received.
// response The nshead response that you should fill in.
// response The nshead response that you should fill in.
// done You must call done->Run() to end the processing, b
aidu::
rpc::ClosureGuard is preferred.
// done You must call done->Run() to end the processing, brpc::ClosureGuard is preferred.
virtual
void
ProcessNsheadRequest
(
const
Server
&
server
,
virtual
void
ProcessNsheadRequest
(
const
Server
&
server
,
Controller
*
controller
,
Controller
*
controller
,
const
NsheadMessage
&
request
,
const
NsheadMessage
&
request
,
...
...
This diff is collapsed.
Click to expand it.
docs/cn/redis_client.md
View file @
888a763a
...
@@ -16,12 +16,12 @@ r32037后加上[-redis_verbose](http://brpc.baidu.com:8765/flags/redis_verbose;r
...
@@ -16,12 +16,12 @@ r32037后加上[-redis_verbose](http://brpc.baidu.com:8765/flags/redis_verbose;r
创建一个访问redis的Channel:
创建一个访问redis的Channel:
```
c++
```
c++
#include
<baidu/
rpc/redis.h>
#include
b
rpc/redis.h>
#include
<baidu/
rpc/channel.h>
#include
b
rpc/channel.h>
b
aidu
::
rpc
::
ChannelOptions
options
;
brpc
::
ChannelOptions
options
;
options
.
protocol
=
b
aidu
::
rpc
::
PROTOCOL_REDIS
;
options
.
protocol
=
brpc
::
PROTOCOL_REDIS
;
b
aidu
::
rpc
::
Channel
redis_channel
;
brpc
::
Channel
redis_channel
;
if
(
redis_channel
.
Init
(
"0.0.0.0:6379"
,
&
options
)
!=
0
)
{
// 6379是redis-server的默认端口
if
(
redis_channel
.
Init
(
"0.0.0.0:6379"
,
&
options
)
!=
0
)
{
// 6379是redis-server的默认端口
LOG
(
ERROR
)
<<
"Fail to init channel to redis-server"
;
LOG
(
ERROR
)
<<
"Fail to init channel to redis-server"
;
return
-
1
;
return
-
1
;
...
@@ -36,9 +36,9 @@ std::string my_key = "my_key_1";
...
@@ -36,9 +36,9 @@ std::string my_key = "my_key_1";
int
my_number
=
1
;
int
my_number
=
1
;
...
...
// 执行"SET <my_key> <my_number>"
// 执行"SET <my_key> <my_number>"
b
aidu
::
rpc
::
RedisRequest
set_request
;
brpc
::
RedisRequest
set_request
;
b
aidu
::
rpc
::
RedisResponse
response
;
brpc
::
RedisResponse
response
;
b
aidu
::
rpc
::
Controller
cntl
;
brpc
::
Controller
cntl
;
set_request
.
AddCommand
(
"SET %s %d"
,
my_key
.
c_str
(),
my_number
);
set_request
.
AddCommand
(
"SET %s %d"
,
my_key
.
c_str
(),
my_number
);
redis_channel
.
CallMethod
(
NULL
,
&
cntl
,
&
set_request
,
&
response
,
NULL
/*done*/
);
redis_channel
.
CallMethod
(
NULL
,
&
cntl
,
&
set_request
,
&
response
,
NULL
/*done*/
);
if
(
cntl
.
Failed
())
{
if
(
cntl
.
Failed
())
{
...
@@ -56,7 +56,7 @@ LOG(INFO) << response.reply(0).c_str() // OK
...
@@ -56,7 +56,7 @@ LOG(INFO) << response.reply(0).c_str() // OK
...
...
// 执行"INCR <my_key>"
// 执行"INCR <my_key>"
b
aidu
::
rpc
::
RedisRequest
incr_request
;
brpc
::
RedisRequest
incr_request
;
incr_request
.
AddCommand
(
"INCR %s"
,
my_key
.
c_str
());
incr_request
.
AddCommand
(
"INCR %s"
,
my_key
.
c_str
());
response
.
Clear
();
response
.
Clear
();
cntl
.
Reset
();
cntl
.
Reset
();
...
@@ -78,9 +78,9 @@ LOG(INFO) << response.reply(0).integer() // 2
...
@@ -78,9 +78,9 @@ LOG(INFO) << response.reply(0).integer() // 2
批量执行incr或decr
批量执行incr或decr
```
c++
```
c++
b
aidu
::
rpc
::
RedisRequest
request
;
brpc
::
RedisRequest
request
;
b
aidu
::
rpc
::
RedisResponse
response
;
brpc
::
RedisResponse
response
;
b
aidu
::
rpc
::
Controller
cntl
;
brpc
::
Controller
cntl
;
request
.
AddCommand
(
"INCR counter1"
);
request
.
AddCommand
(
"INCR counter1"
);
request
.
AddCommand
(
"DECR counter1"
);
request
.
AddCommand
(
"DECR counter1"
);
request
.
AddCommand
(
"INCRBY counter1 10"
);
request
.
AddCommand
(
"INCRBY counter1 10"
);
...
@@ -93,7 +93,7 @@ if (cntl.Failed()) {
...
@@ -93,7 +93,7 @@ if (cntl.Failed()) {
CHECK_EQ
(
4
,
response
.
reply_size
());
CHECK_EQ
(
4
,
response
.
reply_size
());
for
(
int
i
=
0
;
i
<
4
;
++
i
)
{
for
(
int
i
=
0
;
i
<
4
;
++
i
)
{
CHECK
(
response
.
reply
(
i
).
is_integer
());
CHECK
(
response
.
reply
(
i
).
is_integer
());
CHECK_EQ
(
b
aidu
::
rpc
::
REDIS_REPLY_INTEGER
,
response
.
reply
(
i
).
type
());
CHECK_EQ
(
brpc
::
REDIS_REPLY_INTEGER
,
response
.
reply
(
i
).
type
());
}
}
CHECK_EQ
(
1
,
response
.
reply
(
0
).
integer
());
CHECK_EQ
(
1
,
response
.
reply
(
0
).
integer
());
CHECK_EQ
(
0
,
response
.
reply
(
1
).
integer
());
CHECK_EQ
(
0
,
response
.
reply
(
1
).
integer
());
...
...
This diff is collapsed.
Click to expand it.
docs/cn/rpc_replay.md
View file @
888a763a
...
@@ -56,12 +56,12 @@ serialized request (body_size - meta_size bytes, including attachment)
...
@@ -56,12 +56,12 @@ serialized request (body_size - meta_size bytes, including attachment)
baidu-rpc提供了
[
SampleIterator
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/brpc/rpc_dump.h
)
从一个采样目录下的所有文件中依次读取所有的被采样请求,用户可根据需求把serialized request反序列化为protobuf请求,做一些二次开发。
baidu-rpc提供了
[
SampleIterator
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/brpc/rpc_dump.h
)
从一个采样目录下的所有文件中依次读取所有的被采样请求,用户可根据需求把serialized request反序列化为protobuf请求,做一些二次开发。
```
```
#include
<baidu/
rpc/rpc_dump.h>
#include
b
rpc/rpc_dump.h>
...
...
b
aidu::
rpc::SampleIterator it("./rpc_data/rpc_dump/echo_server");
brpc::SampleIterator it("./rpc_data/rpc_dump/echo_server");
for (SampleRequest* req = it->Next(); req != NULL; req = it->Next()) {
for (SampleRequest* req = it->Next(); req != NULL; req = it->Next()) {
...
...
// req->meta的类型是b
aidu::
rpc::RpcDumpMeta,定义在protocol/baidu/rpc/rpc_dump.proto
// req->meta的类型是brpc::RpcDumpMeta,定义在protocol/baidu/rpc/rpc_dump.proto
// req->request的类型是base::IOBuf,对应格式说明中的"serialized request"
// req->request的类型是base::IOBuf,对应格式说明中的"serialized request"
// 使用结束后必须delete req。
// 使用结束后必须delete req。
}
}
...
...
This diff is collapsed.
Click to expand it.
docs/cn/server.md
View file @
888a763a
This diff is collapsed.
Click to expand it.
docs/cn/status.md
View file @
888a763a
...
@@ -18,13 +18,13 @@
...
@@ -18,13 +18,13 @@
-
**processing**
: 正在处理的请求个数。如果持续不为0(特别是在压力归0后),应考虑程序是否有bug。
-
**processing**
: 正在处理的请求个数。如果持续不为0(特别是在压力归0后),应考虑程序是否有bug。
用户可通过让对应Service实现
[
b
aidu::
rpc::Describable
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/brpc/describable.h
)
自定义在/status页面上的描述.
用户可通过让对应Service实现
[
brpc::Describable
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/blob/src/brpc/describable.h
)
自定义在/status页面上的描述.
```
c++
```
c++
class
MyService
:
public
XXXService
,
public
b
aidu
::
rpc
::
Describable
{
class
MyService
:
public
XXXService
,
public
brpc
::
Describable
{
public
:
public
:
...
...
void
DescribeStatus
(
std
::
ostream
&
os
,
const
b
aidu
::
rpc
::
DescribeOptions
&
options
)
const
{
void
DescribeStatus
(
std
::
ostream
&
os
,
const
brpc
::
DescribeOptions
&
options
)
const
{
os
<<
"my_status: blahblah"
;
os
<<
"my_status: blahblah"
;
}
}
};
};
...
...
This diff is collapsed.
Click to expand it.
docs/cn/ub_client.md
View file @
888a763a
This diff is collapsed.
Click to expand it.
docs/cn/vars.md
View file @
888a763a
[
public/bvar
](
..
/src/bvar/
)
是多线程环境下的计数器类库,方便记录和查看用户程序中的各类数值,它利用了thread local存储避免了cache bouncing,相比UbMonitor几乎不会给程序增加性能开销,也快于竞争频繁的原子操作。baidu-rpc集成了bvar,
[
/vars
](
http://brpc.baidu.com:8765/vars
)
可查看所有曝光的bvar,
[
/vars/VARNAME
](
http://brpc.baidu.com:8765/vars/rpc_socket_count
)
可查阅某个bvar,增加计数器的方法请查看
[
bvar
](
http://wiki.baidu.com/display/RPC/bvar
)
。baidu-rpc大量使用了bvar提供统计数值,当你需要在多线程环境中计数并展现时,应该第一时间想到bvar。但bvar不能代替所有的计数器,它的本质是把写时的竞争转移到了读:读得合并所有写过的线程中的数据,而不可避免地变慢了。当你读写都很频繁并得基于数值做一些逻辑判断时,你不应该用bvar。
[
public/bvar
](
http://icode.baidu.com/repo/baidu/opensource/baidu-rpc/files/master/tree
/src/bvar/
)
是多线程环境下的计数器类库,方便记录和查看用户程序中的各类数值,它利用了thread local存储避免了cache bouncing,相比UbMonitor几乎不会给程序增加性能开销,也快于竞争频繁的原子操作。baidu-rpc集成了bvar,
[
/vars
](
http://brpc.baidu.com:8765/vars
)
可查看所有曝光的bvar,
[
/vars/VARNAME
](
http://brpc.baidu.com:8765/vars/rpc_socket_count
)
可查阅某个bvar,增加计数器的方法请查看
[
bvar
](
http://wiki.baidu.com/display/RPC/bvar
)
。baidu-rpc大量使用了bvar提供统计数值,当你需要在多线程环境中计数并展现时,应该第一时间想到bvar。但bvar不能代替所有的计数器,它的本质是把写时的竞争转移到了读:读得合并所有写过的线程中的数据,而不可避免地变慢了。当你读写都很频繁并得基于数值做一些逻辑判断时,你不应该用bvar。
## 查询方法
## 查询方法
...
...
This diff is collapsed.
Click to expand it.
docs/images/ns_access_interval.png
0 → 100644
View file @
888a763a
37.3 KB
This diff is collapsed.
Click to expand it.
docs/images/ns_filter.jpg
0 → 100644
View file @
888a763a
27.7 KB
This diff is collapsed.
Click to expand it.
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