generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from kitex-contrib/update-and-readme
feat: add CN readme & update dependency
- Loading branch information
Showing
9 changed files
with
261 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# 基于规则的 Kitex 解析器 | ||
|
||
[English](README.md) | 中文 | ||
|
||
这个项目为 Kitex 提供了一个基于规则的解析器。它允许用户在服务发现中配置规则来过滤服务实例,实现流量切分的功能。 | ||
|
||
这个解析器需要一个已实现的 Resolver,能够从注册中心解析实例,同时还需要一些定制化的过滤规则(例如,在实例中根据标签进行过滤)。 | ||
|
||
## 用法 | ||
1. 实现你自己的解析器。参考这个文档关于解析器的定义: [服务发现扩展](https://www.cloudwego.io/zh/docs/kitex/tutorials/framework-exten/service_discovery/) | ||
|
||
2. 定义过滤规则。 | ||
|
||
```go | ||
// 定义一个过滤函数 | ||
// 例如,只获取具有标签 {"k":"v"} 的实例 | ||
filterFunc := func(ctx context.Context, instance []discovery.Instance) []discovery.Instance { | ||
var res []discovery.Instance | ||
for _, ins := range instance { | ||
if v, ok := ins.Tag("k"); ok && v == "v" { | ||
res = append(res, ins) | ||
} | ||
} | ||
return res | ||
} | ||
// 构造过滤规则 | ||
filterRule := &FilterRule{Name: "rule-name", Funcs: []FilterFunc{filterFunc}} | ||
``` | ||
注意:FilterFuncs 将按顺序执行。 | ||
|
||
3. 配置解析器 | ||
|
||
```go | ||
import ( | ||
ruleBasedResolver "github.com/kitex-contrib/resolver-rule-based" | ||
"github.com/cloudwego/kitex/client" | ||
"github.com/cloudwego/kitex/pkg/discovery" | ||
) | ||
// 实现你的解析器 | ||
var newResolver discovery.Resolver | ||
// 使用 `newResolver` 和 `filterRule` 构造一个 RuleBasedResolver | ||
tagResolver := ruleBasedResolver.NewRuleBasedResolver(resolver, filterRule) | ||
// 在构建 Kitex 客户端时添加此选项 | ||
opt := client.WithResolver(tagResolver) | ||
``` | ||
|
||
## 示例 | ||
请参考 `/demo` 获取详情。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
# Demo | ||
This demo uses [etcd-registry](github.com/kitex-contrib/registry-etcd) as the original resolver and configure a filter rule to use the rule-based resolver. | ||
|
||
Please make sure [etcd](https://etcd.io/) is configured before running the demo. | ||
Please make sure [etcd](https://etcd.io/) is configured before running the demo. | ||
|
||
# 示例 | ||
|
||
这个示例使用 [etcd-registry](github.com/kitex-contrib/registry-etcd) 作为解析器,并配置了一个过滤规则以使用基于规则的解析器。 | ||
|
||
在运行演示之前,请确保已配置好 [etcd](https://etcd.io/)。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.