Commit a1692d9a authored by Ge Jun's avatar Ge Jun

more docs on naming service

parent 8b3ee09e
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# ![brpc](docs/images/logo.png) # ![brpc](docs/images/logo.png)
A industrial-grade RPC framework used throughout [Baidu](http://ir.baidu.com/phoenix.zhtml?c=188488&p=irol-irhome), with 1,000,000+ instances(not counting clients) and thousands kinds of services, called "**baidu-rpc**" inside Baidu. Only C++ implementation is opensourced right now. An industrial-grade RPC framework used throughout [Baidu](http://ir.baidu.com/phoenix.zhtml?c=188488&p=irol-irhome), with 1,000,000+ instances(not counting clients) and thousands kinds of services, called "**baidu-rpc**" inside Baidu. Only C++ implementation is opensourced right now.
You can use it to: You can use it to:
* Build a server that can talk in multiple protocols (**on same port**), or access all sorts of services * Build a server that can talk in multiple protocols (**on same port**), or access all sorts of services
......
...@@ -4,7 +4,7 @@ includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ ...@@ -4,7 +4,7 @@ includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@ libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
Name: brpc Name: brpc
Description: A industrial-grade RPC framework used throughout Baidu, with 1,000,000+ instances(not counting clients) and thousands kinds of services, called "baidu-rpc" inside Baidu. Description: An industrial-grade RPC framework used throughout Baidu, with 1,000,000+ instances(not counting clients) and thousands kinds of services, called "baidu-rpc" inside Baidu.
Version: @BRPC_VERSION@ Version: @BRPC_VERSION@
Cflags: -I${includedir} Cflags: -I${includedir}
Libs: -L${libdir}/ -lbrpc Libs: -L${libdir}/ -lbrpc
......
...@@ -86,15 +86,30 @@ BNS是百度内常用的命名服务,比如bns://rdev.matrix.all,其中"bns" ...@@ -86,15 +86,30 @@ BNS是百度内常用的命名服务,比如bns://rdev.matrix.all,其中"bns"
### file://\<path\> ### file://\<path\>
服务器列表放在`path`所在的文件里,比如"file://conf/local_machine_list"中的“conf/local_machine_list”对应一个文件,其中每行应是一台服务器的地址。当文件更新时, brpc会重新加载。 服务器列表放在`path`所在的文件里,比如"file://conf/machine_list"中的“conf/machine_list”对应一个文件:
* 每行是一台服务器的地址。
* \#之后的是注释会被忽略
* 地址后出现的非注释内容被认为是tag,由一个或多个空格与前面的地址分隔,相同的地址+不同的tag被认为是不同的实例。
* 当文件更新时, brpc会重新加载。
```
# 此行会被忽略
10.24.234.17 tag1 # 这是注释,会被忽略
10.24.234.17 tag2 # 此行和上一行被认为是不同的实例
10.24.234.18
10.24.234.19
```
此命名服务的优点是易于修改,方便单测;缺点是非中心管理,更新时需要修改每个上游的列表文件,不适合线上部署。
### list://\<addr1\>,\<addr2\>... ### list://\<addr1\>,\<addr2\>...
服务器列表直接跟在list://之后,以逗号分隔,比如"list://db-bce-81-3-186.db01:7000,m1-bce-44-67-72.m1:7000,cp01-rd-cos-006.cp01:7000" 中有三个地址。 服务器列表直接跟在list://之后,以逗号分隔,比如"list://db-bce-81-3-186.db01:7000,m1-bce-44-67-72.m1:7000,cp01-rd-cos-006.cp01:7000"中有三个地址。
地址后可以声明tag,用一个或多个空格分隔,相同的地址+不同的tag被认为是不同的实例。
此命名服务的优点是可在命令行中直接配置,方便单测;缺点是无法在运行时修改,完全不能用于线上部署。
### http://\<url\> ### http://\<url\>
连接一个域名下所有的机器, 例如http://www.baidu.com:80 ,注意连接单点的Init(两个参数)虽然也可传入域名,但只会连接域名下的一台机器。 连接一个域名下所有的机器, 例如http://www.baidu.com:80 ,注意连接单点的Init(两个参数)虽然也可传入域名,但只会连接域名下的一台机器。
此命名服务的优点在于DNS的通用性,公网内网均可使用;缺点是受限于DNS的格式限制无法传递复杂的meta数据,也无法实现通知机制。
### consul://\<service-name\> ### consul://\<service-name\>
...@@ -108,6 +123,18 @@ BNS是百度内常用的命名服务,比如bns://rdev.matrix.all,其中"bns" ...@@ -108,6 +123,18 @@ BNS是百度内常用的命名服务,比如bns://rdev.matrix.all,其中"bns"
如果consul不可访问,服务可自动降级到file naming service获取服务列表。此功能默认关闭,可通过设置-consul\_enable\_degrade\_to\_file\_naming\_service来打开。服务列表文件目录通过-consul \_file\_naming\_service\_dir来设置,使用service-name作为文件名。该文件可通过consul-template生成,里面会保存consul不可用之前最新的下游服务节点。当consul恢复时可自动恢复到consul naming service。 如果consul不可访问,服务可自动降级到file naming service获取服务列表。此功能默认关闭,可通过设置-consul\_enable\_degrade\_to\_file\_naming\_service来打开。服务列表文件目录通过-consul \_file\_naming\_service\_dir来设置,使用service-name作为文件名。该文件可通过consul-template生成,里面会保存consul不可用之前最新的下游服务节点。当consul恢复时可自动恢复到consul naming service。
### 更多命名服务
用户可以通过实现brpc::NamingService来对接更多命名服务,具体见[这里](https://github.com/brpc/brpc/blob/master/docs/cn/load_balancing.md#%E5%91%BD%E5%90%8D%E6%9C%8D%E5%8A%A1)
### 命名服务中的tag
tag的用途主要是实现“粗粒度的wrr”,当给同一个地址加上不同的tag后,它们会被认为是不同的实例,从而让那个地址被负载均衡器正比与不同tag个数的流量。不过,你应当优先考虑使用[wrr算法](#wrr),相比使用tag可以实现更加精细的按权重分流。
### VIP相关的问题
VIP一般是4层负载均衡器的公网ip,背后有多个RS。当客户端连接至VIP时,VIP会选择一个RS建立连接,当客户端连接断开时,VIP也会断开与对应RS的连接。
如果客户端只与VIP建立一个连接(brpc中的单连接),那么来自这个客户端的所有流量都会落到一台RS上。如果客户端的数量非常多,至少在集群的角度,所有的RS还是会分到足够多的连接,从而基本均衡。但如果客户端的数量不多,或客户端的负载差异很大,那么可能在个别RS上出现热点。另一个问题是当有多个vip可选时,客户端分给它们的流量与各自后面的RS数量可能不一致。
解决这个问题的一种方法是使用连接池模式(pooled),这样客户端对一个vip就可能建立多个连接(约为一段时间内的最大并发度),从而让负载落到多个RS上。如果有多个vip,可以用[wrr负载均衡](#wrr)给不同的vip声明不同的权重从而分到对应比例的流量,或给相同的vip后加上多个不同的tag而被认为是多个不同的实例。
注意:在客户端使用单连接时,给相同的vip加上不同的tag确实能让这个vip分到更多的流量,但连接仍然只会有一个,这是由目前brpc实现决定的。
### 命名服务过滤器 ### 命名服务过滤器
当命名服务获得机器列表后,可以自定义一个过滤器进行筛选,最后把结果传递给负载均衡: 当命名服务获得机器列表后,可以自定义一个过滤器进行筛选,最后把结果传递给负载均衡:
......
...@@ -34,7 +34,7 @@ Note that Channel neither modifies `options` nor accesses `options` after comple ...@@ -34,7 +34,7 @@ Note that Channel neither modifies `options` nor accesses `options` after comple
Init() can connect one server or a cluster(multiple servers). Init() can connect one server or a cluster(multiple servers).
# Connect a server # Connect to a server
```c++ ```c++
// Take default values when options is NULL. // Take default values when options is NULL.
...@@ -53,7 +53,7 @@ Invalid "server_addr_and_port": ...@@ -53,7 +53,7 @@ Invalid "server_addr_and_port":
- 127.0.0.1:90000 # too large port - 127.0.0.1:90000 # too large port
- 10.39.2.300:8000 # invalid IP - 10.39.2.300:8000 # invalid IP
# Connect a cluster # Connect to a cluster
```c++ ```c++
int Init(const char* naming_service_url, int Init(const char* naming_service_url,
...@@ -86,15 +86,30 @@ If the list in BNS is non-empty, but Channel says "no servers", the status bit o ...@@ -86,15 +86,30 @@ If the list in BNS is non-empty, but Channel says "no servers", the status bit o
### file://\<path\> ### file://\<path\>
Servers are put in the file specified by `path`. In "file://conf/local_machine_list", "conf/local_machine_list" is the file and each line in the file is address of a server. brpc reloads the file when it's updated. Servers are put in the file specified by `path`. For example, in "file://conf/machine_list", "conf/machine_list" is the file:
* in which each line is address of a server.
* contents after \# are comments and ignored.
* non-comment contents after addresses are tags, which are separated from addresses by one or more spaces, same address + different tags are treated as different instances.
* brpc reloads the file when it's updated.
```
# This line is ignored
10.24.234.17 tag1 # a comment
10.24.234.17 tag2 # an instance different from the instance on last line
10.24.234.18
10.24.234.19
```
Pros: easy to modify, convenient for unittests. Cons: need to update every client when the list changes, not suitable for online deployment.
### list://\<addr1\>,\<addr2\>... ### list://\<addr1\>,\<addr2\>...
Servers are directly written after list://, separated by comma. For example: "list://db-bce-81-3-186.db01:7000,m1-bce-44-67-72.m1:7000,cp01-rd-cos-006.cp01:7000" has 3 addresses. Servers are directly written after list://, separated by comma. For example: "list://db-bce-81-3-186.db01:7000,m1-bce-44-67-72.m1:7000,cp01-rd-cos-006.cp01:7000" has 3 addresses.
Tags can be appended to addresses, separated with one or more spaces. Same address + different tags are treated as different instances.
Pros: directly configurable in CLI, convenient for unittests. Cons: cannot be updated at runtime, not suitable for online deployment at all.
### http://\<url\> ### http://\<url\>
Connect all servers under the domain, for example: http://www.baidu.com:80. Note: although Init() for connecting single server(2 parameters) accepts hostname as well, it only connects one server under the domain. Connect all servers under the domain, for example: http://www.baidu.com:80. Note: although Init() for connecting single server(2 parameters) accepts hostname as well, it only connects one server under the domain.
Pros: Versatility of DNS, useable both in private or public network. Cons: limited by transmission formats of DNS, unable to implement notification mechanisms.
### consul://\<service-name\> ### consul://\<service-name\>
...@@ -108,6 +123,18 @@ If the server list returned by the consul does not follow [response format](http ...@@ -108,6 +123,18 @@ If the server list returned by the consul does not follow [response format](http
If consul is not accessible, the naming service can be automatically downgraded to file naming service. This feature is turned off by default and can be turned on by setting -consul\_enable\_degrade\_to\_file\_naming\_service. After downgrading, in the directory specified by -consul\_file\_naming\_service\_dir, the file whose name is the service-name will be used. This file can be generated by the consul-template, which holds the latest server list before the consul is unavailable. The consul naming service is automatically restored when consul is restored. If consul is not accessible, the naming service can be automatically downgraded to file naming service. This feature is turned off by default and can be turned on by setting -consul\_enable\_degrade\_to\_file\_naming\_service. After downgrading, in the directory specified by -consul\_file\_naming\_service\_dir, the file whose name is the service-name will be used. This file can be generated by the consul-template, which holds the latest server list before the consul is unavailable. The consul naming service is automatically restored when consul is restored.
### More naming services
User can extend to more naming services by implementing brpc::NamingService, check [this link](https://github.com/brpc/brpc/blob/master/docs/cn/load_balancing.md#%E5%91%BD%E5%90%8D%E6%9C%8D%E5%8A%A1) for details.
### The tag in naming service
tag was used for implementing "coarse-grained wrr": different tags are appended to a same address to make different instances, such that the address gets traffic proportional to the number of different tags. However, you should consider using [wrr algorithm](#wrr) first which distributes traffic proportional to assigned weights and is more fine-grained.
### VIP related issues
VIP is often the public IP of layer-4 load balancer, which proxies traffic to RS behide. When a client connects to the VIP, a connection is established to a chosen RS. When the client connection is broken, the connection to the RS is reset as well.
If one client establishes only one connection to the VIP("single" connection type in brpc), all traffic from the client lands on one RS. If number of clients are large enough, each RS should gets many connections and roughly balanced, at least from the cluster perspective. However, if clients are not large enough or workload from clients are very different, some RS may be overloaded. Another issue is that when multiple VIP are listed together, the traffic to them may not be proportional to the number of RS behide them.
One solution to these issues is to use "pooled" connection type, so that one client may create multiple connections to one VIP (roughly the max concurrency recently) to make traffic land on different RS. If more than one VIP are present, consider using [wrr load balancing](#wrr) to assign weights to different VIP, or add different tags to VIP to form more instances.
Note: When the client uses "single" connection type, even if one VIP appended with different tags can get more traffic, the connection is still one. This is decided by current implementation in brpc.
### Naming Service Filter ### Naming Service Filter
Users can filter servers got from the NamingService before pushing to LoadBalancer. Users can filter servers got from the NamingService before pushing to LoadBalancer.
......
...@@ -34,7 +34,7 @@ Common doubts on RPC: ...@@ -34,7 +34,7 @@ Common doubts on RPC:
# What is ![brpc](../images/logo.png)? # What is ![brpc](../images/logo.png)?
A industrial-grade RPC framework used throughout [Baidu](http://ir.baidu.com/phoenix.zhtml?c=188488&p=irol-irhome), with 1,000,000+ instances(not counting clients) and thousands kinds of services, called "**baidu-rpc**" inside Baidu. Only C++ implementation is opensourced right now. An industrial-grade RPC framework used throughout [Baidu](http://ir.baidu.com/phoenix.zhtml?c=188488&p=irol-irhome), with 1,000,000+ instances(not counting clients) and thousands kinds of services, called "**baidu-rpc**" inside Baidu. Only C++ implementation is opensourced right now.
You can use it to: You can use it to:
* Build a server that can talk in multiple protocols (**on same port**), or access all sorts of services * Build a server that can talk in multiple protocols (**on same port**), or access all sorts of services
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment