Skip to content

Commit

Permalink
refactor:polaris examples (#466)
Browse files Browse the repository at this point in the history
* refactor:polaris examples

* examples:finish polaris examples

* examples:finish polaris examples

* examples:finish polaris examples

* examples:finish polaris examples

* examples:finish polaris examples

* examples:finish polaris examples

* examples:finish polaris examples

* examples:finish polaris examples

* examples:finish polaris examples
  • Loading branch information
chuntaojun authored Dec 3, 2022
1 parent 2a8fb6c commit e7a8938
Show file tree
Hide file tree
Showing 28 changed files with 1,408 additions and 45 deletions.
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
module github.com/apache/dubbo-go-samples

require (
dubbo.apache.org/dubbo-go/v3 v3.0.3-0.20220730033103-013f0b2804ce
dubbo.apache.org/dubbo-go/v3 v3.0.4-rc1
github.com/SkyAPM/go2sky v1.5.0
github.com/SkyAPM/go2sky-plugins/dubbo-go v0.0.0-20220718123631-cb8f743b16cf
github.com/apache/dubbo-go-hessian2 v1.11.3
github.com/dubbogo/gost v1.12.6-0.20220719055648-01d5bc07b111
github.com/apache/dubbo-go-hessian2 v1.11.4
github.com/dubbogo/gost v1.13.2
github.com/dubbogo/grpc-go v1.42.10
github.com/dubbogo/triple v1.1.9
github.com/dubbogo/triple v1.2.2-rc2
github.com/golang/protobuf v1.5.2
github.com/opentracing/opentracing-go v1.2.0
github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5
github.com/openzipkin/zipkin-go v0.2.2
github.com/pkg/errors v0.9.1
github.com/seata/seata-go v0.1.0-rc1
github.com/stretchr/testify v1.8.1
github.com/uber/jaeger-client-go v2.29.1+incompatible
github.com/uber/jaeger-client-go v2.30.0+incompatible
go.opentelemetry.io/otel v1.11.0
go.opentelemetry.io/otel/exporters/jaeger v1.10.0
go.opentelemetry.io/otel/sdk v1.10.0
Expand Down
122 changes: 86 additions & 36 deletions go.sum

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions polaris/limit/README-zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Dubbo Go & Polaris TpsLimiter Example

[English](./README.md) | 中文

## 使用服务限流功能

在 dubbogo 中快速体验北极星的服务限流能力

## 北极星服务端安装

[北极星服务端单机版本安装文档](https://polarismesh.cn/docs/%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%89%E8%A3%85/%E5%8D%95%E6%9C%BA%E7%89%88%E5%AE%89%E8%A3%85/)

[北极星服务端集群版本安装文档](https://polarismesh.cn/docs/%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%89%E8%A3%85/%E9%9B%86%E7%BE%A4%E7%89%88%E5%AE%89%E8%A3%85/)

## 如何使用

[北极星服务限流使用文档](https://polarismesh.cn/docs/%E5%8C%97%E6%9E%81%E6%98%9F%E6%98%AF%E4%BB%80%E4%B9%88/%E5%8A%9F%E8%83%BD%E7%89%B9%E6%80%A7/%E6%B5%81%E9%87%8F%E7%AE%A1%E7%90%86/#%E8%AE%BF%E9%97%AE%E9%99%90%E6%B5%81)

### dubbogo.yaml 配置文件

在使用 PolarisMesh 的服务限流能力时,需要先启用 PolarisMesh 在 dubbogo 中的注册发现功能。

```yaml
dubbo:
registries:
polarisMesh:
protocol: polaris
address: ${北极星服务端IP}:8091
namespace: ${北极星命名空间信息}
token: ${北极星资源鉴权 token} # 如果北极星服务端开启了针对客户端的鉴权,则需要配置该参数
provider:
services:
UserProvider:
interface: org.apache.dubbo.UserProvider.Test
tps.limiter: polaris-limit # 配置 tps.limiter 为 polaris-limiter 即可

```

注意:PolarisMesh 的服务限流能力是工作在 Provider 侧的。

### 如何配置服务限流参数

dubbogo 中的 PolarisMesh TpsLimiter 扩展点实现,能够根据用户配置的限流规则,自动的从当前 RPC 调用上下文以及请求信息中识别出需要参与限流的请求标签信息

![](./images/dubbogo-ratelimit-rule.png)

- 请求匹配规则为 **请求参数(QUERY)**
- 标签来源: RPC 调用中方法的入参,即 Invoaction.Arguments()
- 标签键规则
- 方法仅有一个入参: param.$.${fieldName}, **param.** 为固定前缀,后面的表达式为标准的 JSONPath expressions,[参考文档](https://goessner.net/articles/JsonPath/)
- 方法有多个参数: param[${index}].$.${fieldName}, **param[${index}].** 为固定前缀,${index} 代表是方法入参中的第几个参数,下标起始位置从 0 开始;后面的表达式为标准的 JSONPath expressions,[参考文档](https://goessner.net/articles/JsonPath/)
- 请求匹配规则为 **请求头(HEADER)**:
- 标签来源: RPC 调用的额外标签信息,即 Invoaction.Attachments()

### 运行服务提供者

进入 go-server 的 cmd 目录,执行以下命令

```
export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yaml"
go run .
```

当看到以下日志时即表示 server 端启动成功

```log
INFO dubbo/dubbo_protocol.go:84 [DUBBO Protocol] Export service:
```


### 运行服务调用者

进入 go-client 的 cmd 目录,执行以下命令


```
export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml"
go run .
```

当看到以下日志时即表示 go-client 成功发现 go-server 并发起了 RPC 调用

```log
INFO cmd/main.go:75 response: &{A001 Alex Stocks 18 2022-11-19 12:52:38.092 +0800 CST}
```

当看到以下日志时即表示在 PolarisMesh 针对 go-server 配置的限流策略已生效

```log
ERROR handler/rejected_execution_handler_only_log.go:73 The invocation was rejected
```
91 changes: 91 additions & 0 deletions polaris/limit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Dubbo Go & Polaris TpsLimiter Example

English | [中文](./README-zh.md)

## Use the service current limit function

Quickly experience Polaris' service current limiting capabilities in dubbogo

## Polaris server installation

[Polaris Server Standalone Version Installation Documentation](https://polarismesh.cn/docs/%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%89%E8%A3%85/%E5%8D%95%E6%9C%BA%E7%89%88%E5%AE%89%E8%A3%85/)

[Polaris Server Cluster Version Installation Documentation](https://polarismesh.cn/docs/%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%89%E8%A3%85/%E9%9B%86%E7%BE%A4%E7%89%88%E5%AE%89%E8%A3%85/)

## how to use

[Polaris Service Current Limitation Documentation](https://polarismesh.cn/docs/%E5%8C%97%E6%9E%81%E6%98%9F%E6%98%AF%E4%BB%80%E4%B9%88/%E5%8A%9F%E8%83%BD%E7%89%B9%E6%80%A7/%E6%B5%81%E9%87%8F%E7%AE%A1%E7%90%86/#%E8%AE%BF%E9%97%AE%E9%99%90%E6%B5%81)

### dubbogo.yaml configuration file

When using PolarisMesh's service current limiting capability, you need to enable PolarisMesh's registration discovery function in dubbogo firstly.

````yaml
dubbo:
registries:
polarisMesh:
protocol: polaris
address: ${Polaris server IP}:8091
namespace: ${Polaris namespace information}
token: ${Polaris resource authentication token} # If the Polaris server has enabled authentication for the client, you need to configure this parameter
provider:
services:
UserProvider:
interface: org.apache.dubbo.UserProvider.Test
tps.limiter: polaris-limit # Configure tps.limiter as polaris-limiter

````

Note: The service current limiting capability of PolarisMesh works on the Provider side.

### How to configure service current limit parameters

The implementation of the PolarisMesh TpsLimiter extension point in dubbogo can automatically identify the request tag information that needs to participate in current limiting from the current RPC call context and request information according to the current limiting rules configured by the user.

![](./images/dubbogo-ratelimit-rule.png)

- The request matching rule is **Request parameter(QUERY)**
- Tag source: the input parameter of the method in the RPC call, namely Invoaction.Arguments()
- Tag key rules
- The method has only one input parameter: param.$.${fieldName}, **param.** is a fixed prefix, the following expressions are standard JSONPath expressions, [reference document](https://goessner.net/ articles/JsonPath/)
- The method has multiple parameters: param[${index}].$.${fieldName}, **param[${index}].** is a fixed prefix, ${index} represents the first parameter in the method parameter Several parameters, the starting position of the subscript starts from 0; the following expressions are standard JSONPath expressions, [reference document](https://goessner.net/articles/JsonPath/)
- The request matching rule is **request header (HEADER)**:
- Tag source: Additional tag information for RPC calls, i.e. Invoaction.Attachments()

### Running the service provider

Enter the cmd directory of go-server and execute the following command

````
export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml"
go run .
````

When you see the following log, it means that the server side started successfully

````log
INFO dubbo/dubbo_protocol.go:84 [DUBBO Protocol] Export service:
````


### Run the service caller

Enter the cmd directory of go-client and execute the following command


````
export DUBBO_GO_CONFIG_PATH="../conf/dubbogo.yml"
go run .
````

When you see the following log, it means that go-client successfully discovered go-server and made an RPC call

````log
INFO cmd/main.go:75 response: &{A001 Alex Stocks 18 2022-11-19 12:52:38.092 +0800 CST}
````

When you see the following log, it means that the current limiting policy configured for go-server in PolarisMesh has taken effect

````log
ERROR handler/rejected_execution_handler_only_log.go:73 The invocation was rejected
````
84 changes: 84 additions & 0 deletions polaris/limit/go-client/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"context"
"errors"
"time"
)

import (
"dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/imports"

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

"github.com/dubbogo/gost/log/logger"
)

type UserProviderWithCustomGroupAndVersion struct {
GetUser func(ctx context.Context, req *User) (rsp *User, err error)
}

type UserProvider struct {
GetUser func(ctx context.Context, req *User) (rsp *User, err error)
}

type User struct {
ID string
Name string
Age int32
Time time.Time
}

func (u *User) JavaClassName() string {
return "org.apache.dubbo.User"
}

func main() {
var userProvider = &UserProvider{}
var userProviderWithCustomRegistryGroupAndVersion = &UserProviderWithCustomGroupAndVersion{}
config.SetConsumerService(userProvider)
config.SetConsumerService(userProviderWithCustomRegistryGroupAndVersion)
hessian.RegisterPOJO(&User{})
err := config.Load()
if err != nil {
panic(err)
}

logger.Infof("\n\n\nstart to test dubbo")

var successCount, failCount int64
for i := 0; i < 10; i++ {
time.Sleep(50 * time.Millisecond)
user, err := userProvider.GetUser(context.TODO(), &User{Name: "Alex03"})
if err != nil {
failCount++
logger.Infof("error: %v\n", err)
} else {
successCount++
}
logger.Infof("response: %v\n", user)
}
logger.Infof("successCount=%v, failCount=%v\n", successCount, failCount)

if failCount == 0 {
panic(errors.New("need failCount > 0"))
}
}
23 changes: 23 additions & 0 deletions polaris/limit/go-client/conf/dubbogo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
dubbo:
application:
name: myApp # metadata: application=myApp; name=myApp
module: opensource #metadata: module=opensource
group: myAppGroup # no metadata record
organization: dubbo # metadata: organization=dubbo
owner: laurence # metadata: owner=laurence
version: myversion # metadata: app.version=myversion
environment: pro # metadata: environment=pro
registries:
polarisMesh:
protocol: polaris
address: 183.47.111.80:8091
namespace: dubbogo
consumer:
references:
UserProvider:
protocol: dubbo
interface: org.apache.dubbo.UserProvider.Test
UserProviderWithCustomGroupAndVersion:
protocol: dubbo
interface: org.apache.dubbo.UserProvider.Test2
version: myInterfaceVersion # dubbo interface version must be same with server
Loading

0 comments on commit e7a8938

Please sign in to comment.