Skip to content

Commit

Permalink
add nacos naming service
Browse files Browse the repository at this point in the history
  • Loading branch information
yyweii committed Sep 14, 2022
1 parent 6457ad7 commit 1034b00
Show file tree
Hide file tree
Showing 9 changed files with 564 additions and 1 deletion.
26 changes: 26 additions & 0 deletions docs/cn/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ 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。


### nacos://\<service-name\>

NacosNamingService使用[Open-Api](https://nacos.io/zh-cn/docs/open-api.html)定时从nacos获取服务列表。
NacosNamingService支持[简单鉴权](https://nacos.io/zh-cn/docs/auth.html)

`<service-name>`是一个http uri query,具体参数参见`/nacos/v1/ns/instance/list`文档。
注意:`<service-name>`需要urlencode。
```
nacos://serviceName=test&groupName=g&namespaceId=n&clusters=c&healthyOnly=true
```

NacosNamingService拉取列表的时间间隔为`/nacos/v1/ns/instance/list`api返回的`cacheMillis`
NacosNamingService只支持整形的权重值。

| GFlags | 描述 | 默认值 |
| ---------------------------------- | -------------------------- | ---------------------------- |
| nacos_address | nacos http url | "" |
| nacos_service_discovery_path | nacos服务发现路径 | "/nacos/v1/ns/instance/list" |
| nacos_service_auth_path | nacos登陆路径 | "/nacos/v1/auth/login" |
| nacos_service_timeout_ms | 连接nacos超时时间(毫秒) | 200 |
| nacos_username | 用户名(urlencode编码) | "" |
| nacos_password | 密码(urlencode编码) | "" |
| nacos_load_balancer | nacos集群的负载均衡 | "rr" |


### 更多命名服务
用户可以通过实现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)

Expand Down
26 changes: 26 additions & 0 deletions docs/en/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,32 @@ 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.


### nacos://\<service-name\>

NacosNamingService gets a list of servers from nacos periodically by [Open-Api](https://nacos.io/en-us/docs/open-api.html).
NacosNamingService supports [simple authentication](https://nacos.io/en-us/docs/auth.html).

`<service-name>` is a http uri query,For more detail, refer to `/nacos/v1/ns/instance/list` api document.
NOTE: `<service-name>` must be url-encoded.
```
nacos://serviceName=test&groupName=g&namespaceId=n&clusters=c&healthyOnly=true
```

The server list is cached for `cacheMillis` milliseconds as specified in the response of `/nacos/v1/ns/instance/list` api.
NOTE: The value of server weight must be an integer.

| GFlags | Description | Default value |
| ---------------------------------- | ------------------------------------ | ---------------------------- |
| nacos_address | nacos http url | "" |
| nacos_service_discovery_path | path for discovery | "/nacos/v1/ns/instance/list" |
| nacos_service_auth_path | path for login | "/nacos/v1/auth/login" |
| nacos_service_timeout_ms | timeout for connecting to nacos(ms) | 200 |
| nacos_username | url-encoded username | "" |
| nacos_password | url-encoded password | "" |
| nacos_load_balancer | load balancer for nacos clusters | "rr" |


### 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.

Expand Down
3 changes: 3 additions & 0 deletions src/brpc/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "brpc/policy/remote_file_naming_service.h"
#include "brpc/policy/consul_naming_service.h"
#include "brpc/policy/discovery_naming_service.h"
#include "brpc/policy/nacos_naming_service.h"

// Load Balancers
#include "brpc/policy/round_robin_load_balancer.h"
Expand Down Expand Up @@ -135,6 +136,7 @@ struct GlobalExtensions {
RemoteFileNamingService rfns;
ConsulNamingService cns;
DiscoveryNamingService dcns;
NacosNamingService nns;

RoundRobinLoadBalancer rr_lb;
WeightedRoundRobinLoadBalancer wrr_lb;
Expand Down Expand Up @@ -358,6 +360,7 @@ static void GlobalInitializeOrDieImpl() {
NamingServiceExtension()->RegisterOrDie("remotefile", &g_ext->rfns);
NamingServiceExtension()->RegisterOrDie("consul", &g_ext->cns);
NamingServiceExtension()->RegisterOrDie("discovery", &g_ext->dcns);
NamingServiceExtension()->RegisterOrDie("nacos", &g_ext->nns);

// Load Balancers
LoadBalancerExtension()->RegisterOrDie("rr", &g_ext->rr_lb);
Expand Down
6 changes: 5 additions & 1 deletion src/brpc/periodic_naming_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ DEFINE_int32(ns_access_interval, 5,
"Wait so many seconds before next access to naming service");
BRPC_VALIDATE_GFLAG(ns_access_interval, PositiveInteger);

int PeriodicNamingService::GetNamingServiceAccessIntervalMs() const {
return std::max(FLAGS_ns_access_interval, 1) * 1000;
}

int PeriodicNamingService::RunNamingService(
const char* service_name, NamingServiceActions* actions) {
std::vector<ServerNode> servers;
Expand All @@ -47,7 +51,7 @@ int PeriodicNamingService::RunNamingService(
actions->ResetServers(servers);
}

if (bthread_usleep(std::max(FLAGS_ns_access_interval, 1) * 1000000L) < 0) {
if (bthread_usleep(GetNamingServiceAccessIntervalMs() * 1000UL) < 0) {
if (errno == ESTOP) {
RPC_VLOG << "Quit NamingServiceThread=" << bthread_self();
return 0;
Expand Down
2 changes: 2 additions & 0 deletions src/brpc/periodic_naming_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class PeriodicNamingService : public NamingService {
virtual int GetServers(const char *service_name,
std::vector<ServerNode>* servers) = 0;

virtual int GetNamingServiceAccessIntervalMs() const;

int RunNamingService(const char* service_name,
NamingServiceActions* actions);
};
Expand Down
Loading

0 comments on commit 1034b00

Please sign in to comment.