forked from apache/dubbo-go
-
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 apache#66 from beiwei30/filter
fix: enhance filter samples.
- Loading branch information
Showing
40 changed files
with
823 additions
and
83 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
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> |
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,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> |
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,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> |
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,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> |
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,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> |
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,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> |
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,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> |
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,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> |
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,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} | ||
`` |
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,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} | ||
``` |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.