Skip to content

Commit

Permalink
Merge pull request #2 from kitex-contrib/update-and-readme
Browse files Browse the repository at this point in the history
feat: add CN readme & update dependency
  • Loading branch information
GuangmingLuo authored Dec 6, 2023
2 parents 0fc32c3 + 5148e9f commit 37fd482
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 57 deletions.
66 changes: 35 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Kitex Rule Based Resolver

English| [中文](README_CN.md)

This project provides a rule-based resolver for Kitex. It allows user to configure rules to filter service instances in Service Discovery, achieving the function of traffic split.

This resolver needs an implemented Resolver, which is able to resolve instances from the registry center, and some customized filter rules (e.g. filter by tags in the instances).
Expand All @@ -8,40 +11,41 @@ This resolver needs an implemented Resolver, which is able to resolve instances

2. Define filter rules.

```
// Define a filter function.
// For example, only get the instances with a tag of {"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)
```go
// Define a filter function.
// For example, only get the instances with a tag of {"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
}
// Construct the filterRule
filterRule := &FilterRule{Name: "rule-name", Funcs: []FilterFunc{filterFunc}}
```
Notice: the FilterFuncs will be executed sequentially.
return res
}
// Construct the filterRule
filterRule := &FilterRule{Name: "rule-name", Funcs: []FilterFunc{filterFunc}}
```
Notice: the FilterFuncs will be executed sequentially.

3. Configure the resolver
```
import (
ruleBasedResolver "github.com/kitex-contrib/resolver-rule-based"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/discovery"
)
// implement your resolver
var newResolver discovery.Resolver
// construct a RuleBasedResolver with the `newResolver` and `filterRule`
tagResolver := ruleBasedResolver.NewRuleBasedResolver(resolver, filterRule)
// add this option when construct Kitex Client
opt := client.WithResolver(tagResolver)
```

```go
import (
ruleBasedResolver "github.com/kitex-contrib/resolver-rule-based"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/discovery"
)
// implement your resolver
var newResolver discovery.Resolver
// construct a RuleBasedResolver with the `newResolver` and `filterRule`
tagResolver := ruleBasedResolver.NewRuleBasedResolver(resolver, filterRule)
// add this option when construct Kitex Client
opt := client.WithResolver(tagResolver)
```

## Demo
Please refer to the `/demo` for details.
51 changes: 51 additions & 0 deletions README_CN.md
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` 获取详情。
8 changes: 7 additions & 1 deletion demo/README.md
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/)
12 changes: 7 additions & 5 deletions demo/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ module demo
go 1.18

require (
github.com/cloudwego/kitex v0.5.0
github.com/kitex-contrib/registry-etcd v0.1.0
github.com/kitex-contrib/resolver-rule-based v0.0.0-20230309083100-6cf7b6e5f2b4
github.com/cloudwego/kitex v0.8.0
github.com/kitex-contrib/registry-etcd v0.2.0
github.com/kitex-contrib/resolver-rule-based v0.0.0-20230309083851-0fc32c3edcd9
)

require (
github.com/apache/thrift v0.13.0 // indirect
github.com/choleraehyq/pid v0.0.16 // indirect
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect
github.com/choleraehyq/pid v0.0.17 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3 // indirect
go.etcd.io/etcd/api/v3 v3.5.5 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.5 // indirect
go.etcd.io/etcd/client/v3 v3.5.5 // indirect
go.uber.org/atomic v1.8.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2 // indirect
golang.org/x/text v0.6.0 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
Expand Down
Loading

0 comments on commit 37fd482

Please sign in to comment.