Skip to content

Commit

Permalink
Merge pull request apache#66 from beiwei30/filter
Browse files Browse the repository at this point in the history
fix: enhance filter samples.
  • Loading branch information
zouyx authored Mar 11, 2021
2 parents ae3feca + d75c0be commit e5999a2
Show file tree
Hide file tree
Showing 40 changed files with 823 additions and 83 deletions.
15 changes: 15 additions & 0 deletions .run/filter-custom-client.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="filter-custom-client" type="GoApplicationRunConfiguration" factoryName="Go Application" singleton="false">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/filter/custom/go-client/conf/log.yml" />
<env name="CONF_CONSUMER_FILE_PATH" value="$PROJECT_DIR$/filter/custom/go-client/conf/client.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/filter/custom/go-client/cmd/client.go" />
<package value="github.com/apache/dubbo-go-samples/filter/custom/go-client/cmd" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
15 changes: 15 additions & 0 deletions .run/filter-custom-server.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="filter-custom-server" type="GoApplicationRunConfiguration" factoryName="Go Application" singleton="false">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="CONF_PROVIDER_FILE_PATH" value="$PROJECT_DIR$/filter/custom/go-server/conf/server.yml" />
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/filter/custom/go-server/conf/log.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/filter/custom/go-server/cmd/server.go" />
<package value="github.com/apache/dubbo-go-samples/filter/custom/go-server/cmd" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
17 changes: 17 additions & 0 deletions .run/filter-custom-test.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="filter-custom-test" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$/filter/custom" />
<useCustomBuildTags value="true" />
<envs>
<env name="CONF_CONSUMER_FILE_PATH" value="go-server/conf/client.yml" />
<env name="APP_LOG_CONF_FILE" value="go-server/conf/log.yml" />
</envs>
<framework value="gotest" />
<kind value="PACKAGE" />
<package value="github.com/apache/dubbo-go-samples/filter/custom/go-server/tests/integration" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
15 changes: 15 additions & 0 deletions .run/filter-sentinel-client.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="filter-sentinel-client" type="GoApplicationRunConfiguration" factoryName="Go Application" singleton="false">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/filter/sentinel/go-client/conf/log.yml" />
<env name="CONF_CONSUMER_FILE_PATH" value="$PROJECT_DIR$/filter/sentinel/go-client/conf/client.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/filter/sentinel/go-client/cmd/client.go" />
<package value="github.com/apache/dubbo-go-samples/filter/sentinel/go-client/cmd" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
15 changes: 15 additions & 0 deletions .run/filter-sentinel-server.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="filter-sentinel-server" type="GoApplicationRunConfiguration" factoryName="Go Application" singleton="false">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="CONF_PROVIDER_FILE_PATH" value="$PROJECT_DIR$/filter/sentinel/go-server/conf/server.yml" />
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/filter/sentinel/go-server/conf/log.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/filter/sentinel/go-server/cmd/server.go" />
<package value="github.com/apache/dubbo-go-samples/filter/sentinel/go-server/cmd" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
17 changes: 17 additions & 0 deletions .run/filter-sentinel-test.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="filter-sentinel-test" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$/filter/sentinel" />
<useCustomBuildTags value="true" />
<envs>
<env name="CONF_CONSUMER_FILE_PATH" value="go-server/conf/client.yml" />
<env name="APP_LOG_CONF_FILE" value="go-server/conf/log.yml" />
</envs>
<framework value="gotest" />
<kind value="PACKAGE" />
<package value="github.com/apache/dubbo-go-samples/filter/sentinel/go-server/tests/integration" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
15 changes: 15 additions & 0 deletions .run/filter-tpslimit-server.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="filter-tpslimit-server" type="GoApplicationRunConfiguration" factoryName="Go Application" singleton="false">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="CONF_PROVIDER_FILE_PATH" value="$PROJECT_DIR$/filter/tpslimit/go-server/conf/server.yml" />
<env name="APP_LOG_CONF_FILE" value="$PROJECT_DIR$/filter/tpslimit/go-server/conf/log.yml" />
</envs>
<kind value="PACKAGE" />
<filePath value="$PROJECT_DIR$/filter/tpslimit/go-server/cmd/server.go" />
<package value="github.com/apache/dubbo-go-samples/filter/tpslimit/go-server/cmd" />
<directory value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
17 changes: 17 additions & 0 deletions .run/filter-tpslimit-test.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="filter-tpslimit-test" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="dubbo-go-samples" />
<working_directory value="$PROJECT_DIR$/filter/tpslimit" />
<useCustomBuildTags value="true" />
<envs>
<env name="CONF_CONSUMER_FILE_PATH" value="go-server/conf/client.yml" />
<env name="APP_LOG_CONF_FILE" value="go-server/conf/log.yml" />
</envs>
<framework value="gotest" />
<kind value="PACKAGE" />
<package value="github.com/apache/dubbo-go-samples/filter/tpslimit/go-server/tests/integration" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
93 changes: 93 additions & 0 deletions filter/custom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Custom Filter Sample

### Background

Dubbo-go supports custom filter on both provider side and consumer side. By doing this, user has the opportunity to do some customized operations during the request, for example: logging or auditing, etc.

### Example

This example shows how to add a custom filter on the provider side.

##### 1. Code

A) First implement a custom filter on the provider side as shown below:

```go
type myCustomFilter struct{}

func (mf myCustomFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
// the logic put here...
// you can get many params in url. And the invocation provides more information about
url := invoker.GetUrl()
serviceKey := url.ServiceKey()
gxlog.CInfo("Here is the my custom filter. The service is invoked: %s", serviceKey)
return invoker.Invoke(ctx, invocation)
}

func (mf myCustomFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
// you can do something here with result
gxlog.CInfo("Got result!")
if user, ok := result.Result().(*User); ok {
user.Name = strings.ToUpper(user.Name)
user.Age = user.Age + 10
}
return result
}

func GetMyCustomFilter() filter.Filter {
return &myCustomFilter{}
}
```

This filter implementation will modify the response result before send it back to the consumer, for example: capitalize user's name, and add user's age by 10. At the same time, this filter also prints out messages for logging purpose.

B) Register this custom filter with the name "MyCustomFilter":

```go
func init() {
/**
* MyCustomFilter would be the name that used in your configuration file.
* it can be used as reference filter and provider filter.
* For example, using this filter in server, and the configure file looks like:
*
* filter: "MyCustomFilter",
* registries:
* "demoZk":
* protocol: "zookeeper"
* timeout : "3s"
* address: "127.0.0.1:2181"
* Another important things is that you should make sure this statement executed. It usually means that
* this file should be imported.
*/
extension.SetFilter("MyCustomFilter", GetMyCustomFilter)

// or using the singleton
// filter.SetFilter("MyCustomFilter", GetMyCustomFilterSingleton)
}
```

##### 2. Configuration

Configure this filter in the provider's configuration file like this:

```yaml
# filter config
filter: "MyCustomFilter"
```
##### 3. Run
Pls. refer to [HOWTO.md](../../HOWTO.md) under the root directory to run this sample.
The provider side will print out:
```bash
[2021-03-10/16:30:52 github.com/apache/dubbo-go-samples/filter/custom/go-server/pkg.myCustomFilter.Invoke: custom_filter.go: 61] %s
Here is the my custom filter. The service is invoked: org.apache.dubbo.UserProvider
```
And the consumer side will print out:
```bash
[2021-03-10/16:32:06 main.main: client.go: 64] %s response result: &{A001 ALEX STOCKS 28 2021-03-10 16:32:06.643 +0800 CST}
``
93 changes: 93 additions & 0 deletions filter/custom/README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# 自定义 Filter 示例

### 背景

Dubbo-go 支持在服务端和客户端扩展自己的 filter,通过这样做,在请求链路上做一些自定义的处理,比如,监控或者审计等。

### 示例

在本例中,将以服务端为例,展示如何增加一个自定义的 filter。

##### 1. 代码

A) 首先在服务端实现一个自定义的 filter,如下所示:

```go
type myCustomFilter struct{}

func (mf myCustomFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
// the logic put here...
// you can get many params in url. And the invocation provides more information about
url := invoker.GetUrl()
serviceKey := url.ServiceKey()
gxlog.CInfo("Here is the my custom filter. The service is invoked: %s", serviceKey)
return invoker.Invoke(ctx, invocation)
}

func (mf myCustomFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
// you can do something here with result
gxlog.CInfo("Got result!")
if user, ok := result.Result().(*User); ok {
user.Name = strings.ToUpper(user.Name)
user.Age = user.Age + 10
}
return result
}

func GetMyCustomFilter() filter.Filter {
return &myCustomFilter{}
}
```

该 filter 会修改返回给客户端的结果,将用户名改为大写,并将用户的年龄加十。同时,还将在服务端打印日志。

B) 把该 filter 注册成名为 "MyCustomFilter" 的 filter:

```go
func init() {
/**
* MyCustomFilter would be the name that used in your configuration file.
* it can be used as reference filter and provider filter.
* For example, using this filter in server, and the configure file looks like:
*
* filter: "MyCustomFilter",
* registries:
* "demoZk":
* protocol: "zookeeper"
* timeout : "3s"
* address: "127.0.0.1:2181"
* Another important things is that you should make sure this statement executed. It usually means that
* this file should be imported.
*/
extension.SetFilter("MyCustomFilter", GetMyCustomFilter)

// or using the singleton
// filter.SetFilter("MyCustomFilter", GetMyCustomFilterSingleton)
}
```

##### 2. 配置

在服务端的配置文件中,按如下所示配置该 filter:

```yaml
# filter config
filter: "MyCustomFilter"
```
##### 3. 运行
请参阅根目录中的 [HOWTO.md](../../HOWTO_zh.md) 来运行本例。
观察服务端的输出:
```bash
[2021-03-10/16:30:52 github.com/apache/dubbo-go-samples/filter/custom/go-server/pkg.myCustomFilter.Invoke: custom_filter.go: 61] %s
Here is the my custom filter. The service is invoked: org.apache.dubbo.UserProvider
```
观察客户端的输出:
```bash
[2021-03-10/16:32:06 main.main: client.go: 64] %s response result: &{A001 ALEX STOCKS 28 2021-03-10 16:32:06.643 +0800 CST}
```
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ import (

import (
hessian "github.com/apache/dubbo-go-hessian2"
"github.com/apache/dubbo-go-samples/helloworld/go-client/pkg"
"github.com/dubbogo/gost/log"
)

import (
_ "github.com/apache/dubbo-go/cluster/cluster_impl"
_ "github.com/apache/dubbo-go/cluster/loadbalance"
_ "github.com/apache/dubbo-go/common/proxy/proxy_factory"
Expand All @@ -38,6 +33,11 @@ import (
_ "github.com/apache/dubbo-go/protocol/dubbo"
_ "github.com/apache/dubbo-go/registry/protocol"
_ "github.com/apache/dubbo-go/registry/zookeeper"
"github.com/dubbogo/gost/log"
)

import (
"github.com/apache/dubbo-go-samples/filter/custom/go-client/pkg"
)

var userProvider = new(pkg.UserProvider)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main

import (
"fmt"
"github.com/apache/dubbo-go-samples/filter/custom_filter/go-server/pkg"
"os"
"os/signal"
"syscall"
Expand All @@ -28,7 +27,6 @@ import (

import (
hessian "github.com/apache/dubbo-go-hessian2"

_ "github.com/apache/dubbo-go/cluster/cluster_impl"
_ "github.com/apache/dubbo-go/cluster/loadbalance"
"github.com/apache/dubbo-go/common/logger"
Expand All @@ -40,6 +38,10 @@ import (
_ "github.com/apache/dubbo-go/registry/zookeeper"
)

import (
"github.com/apache/dubbo-go-samples/filter/custom/go-server/pkg"
)

var (
survivalTimeout = int(3e9)
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ import (
"sync"
)

import (
"github.com/dubbogo/gost/log"
)

import (
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/filter"
"github.com/apache/dubbo-go/protocol"
"github.com/dubbogo/gost/log"
)

func init() {
Expand Down
Loading

0 comments on commit e5999a2

Please sign in to comment.