Skip to content

Commit

Permalink
Merge pull request apache#4 from apache/master
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
PhilYue committed Mar 4, 2021
2 parents eb2e0ee + 52644af commit 97e33bb
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 26 deletions.
Binary file added .images/metrics.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions metric/README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# metric 示例

### 背景

[dubbo metric](https://github.com/alibaba/metrics) 是一套标准度量库,我们可以轻易列举出来在 RPC 领域里面我们所关心的各种指标,诸如每个服务的调用次数,响应时间;如果更加细致一点,还有各种响应时间的分布,平均响应时间.对原理感兴趣的可以去看下 [eBay 邓明:dubbo-go 中 metrics 的设计](https://dubbo.apache.org/zh/blog/2021/01/11/dubbo-go-中-metrics-的设计) 以下我将要使用[prometheus](https://prometheus.io/docs/introduction/overview) 来展示metric信息

### 效果图

![metric](../.images/metrics.png)

### 注意事项

* [prometheus](https://prometheus.io/docs/introduction/overview) 需要的是一个合法的名称而根据 [dubbo-go源码](https://github.com/apache/dubbo-go/blob/master/metrics/prometheus/reporter.go) 得知`namespace = config.GetApplicationConfig().Name`说你你服务的名称字母、数组、下划线才可以

* 需要修改[prometheus.yml](./go-server/docker/config/prometheus.yml)ip为本地物理机器的ip

> prometheus.yml配置如下
```yaml
# my global config
global:
scrape_interval: 120s
evaluation_interval: 120s
external_labels:
monitor: 'metric-dubbo-go-server'
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 120s
static_configs:
- targets: [ 'localhost:9090' ]

- job_name: 'metric-dubbo-server'
scheme: http
scrape_interval: 10s
static_configs:
# 这里需要写本机机器的ip
- targets: [ '本地ip:8080' ]
```
请参阅根目录中的 [HOWTO.md](../HOWTO_zh.md) 来运行本例。
30 changes: 13 additions & 17 deletions metric/go-client/cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ package main

import (
"context"
"io/ioutil"
"net/http"
"os"
"time"
)

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

Expand All @@ -51,22 +49,20 @@ func init() {

// need to setup environment variable "CONF_CONSUMER_FILE_PATH" to "conf/client.yml" before run
func main() {
hessian.RegisterPOJO(&pkg.User{})
config.Load()
time.Sleep(3 * time.Second)

gxlog.CInfo("\n\n\nstart to test dubbo")
gxlog.CInfo("\n\n\nstart to test dubbo\n")
user := &pkg.User{}
err := userProvider.GetUser(context.TODO(), []interface{}{"A001"}, user)
if err != nil {
gxlog.CError("error: %v\n", err)
os.Exit(1)
return
}
gxlog.CInfo("response result: %v\n", user)

res, err := http.Get("http://localhost:8080/metrics")
defer res.Body.Close()
data, _ := ioutil.ReadAll(res.Body)
gxlog.CInfo("metrics: %v\n", string(data))
}
for {
err := userProvider.GetUser(context.TODO(), []interface{}{"dubbo-go"}, user)
if err != nil {
gxlog.CError("error: %v\n", err)
os.Exit(1)
return
}
gxlog.CInfo("response result: %v\n", user)
time.Sleep(3 * time.Second)
}
}
2 changes: 1 addition & 1 deletion metric/go-server/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
hessian "github.com/apache/dubbo-go-hessian2"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/apache/dubbo-go-samples/helloworld/go-server/pkg"
"github.com/apache/dubbo-go-samples/metric/go-server/pkg"
_ "github.com/apache/dubbo-go/cluster/cluster_impl"
_ "github.com/apache/dubbo-go/cluster/loadbalance"
"github.com/apache/dubbo-go/common/logger"
Expand Down
4 changes: 2 additions & 2 deletions metric/go-server/conf/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ metrics:
# application config
application:
organization: "dubbo.io"
name: "UserInfoServer"
name: "metric_dubbo_server"
module: "dubbo-go user-info server"
version: "0.0.1"
environment: "dev"
Expand Down Expand Up @@ -58,4 +58,4 @@ protocol_conf:
tcp_write_timeout: "5s"
wait_timeout: "1s"
max_msg_len: 1024000
session_name: "server"
session_name: "server"
18 changes: 18 additions & 0 deletions metric/go-server/docker/config/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# my global config
global:
scrape_interval: 120s
evaluation_interval: 120s
external_labels:
monitor: 'metric-dubbo-go-server'
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 120s
static_configs:
- targets: [ 'localhost:9090' ]

- job_name: 'metric-dubbo-server'
scheme: http
scrape_interval: 10s
static_configs:
# 这里需要写本机机器的ip
- targets: [ 'localhost:8080' ]
40 changes: 36 additions & 4 deletions metric/go-server/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
version: '3'
version: '3.5'

services:
zookeeper:
image: zookeeper
image: zookeeper:latest
container_name: zookeeper
networks:
- dubbo
ports:
- 2181:2181
restart: on-failure

# grafana:
# image: grafana/grafana:latest
# container_name: grafana
# restart: unless-stopped
# networks:
# - dubbo
# ports:
# - "3000:3000"
# environment:
# - GF_SECURITY_ADMIN_PASSWORD=admin
# - GF_USERS_ALLOW_SIGN_UP=false
# - GF_SERVER_DOMAIN=grafana.io
# - GF_SMTP_ENABLED=true
# - GF_SMTP_HOST=smtp.gmail.com:587
# - GF_SMTP_USER=myadrress@gmail.com
# - GF_SMTP_PASSWORD=mypassword
# - GF_SMTP_FROM_ADDRESS=myaddress@gmail.com
prometheus:
image: prom/prometheus:latest
container_name: prometheus
networks:
- dubbo
volumes:
- ./config:/etc/prometheus/
ports:
- 9090:9090
# 网络
networks:
dubbo:
name: dubbo-go-samples
driver: bridge
5 changes: 3 additions & 2 deletions metric/go-server/pkg/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ type UserProvider struct {

func (u *UserProvider) GetUser(ctx context.Context, req []interface{}) (*User, error) {
gxlog.CInfo("req:%#v", req)
rsp := User{"A001", "Alex Stocks", 18, time.Now()}
id := req[0].(string)
rsp := User{id, "Alex Stocks", 18, time.Now()}
span, ctx := opentracing.StartSpanFromContext(ctx, "User-Server-Span")
time.Sleep(100 * time.Millisecond)
span.Finish()
Expand All @@ -64,4 +65,4 @@ func (u *UserProvider) Reference() string {

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

0 comments on commit 97e33bb

Please sign in to comment.