diff --git a/.images/metrics.png b/.images/metrics.png new file mode 100644 index 0000000000..1539e6412e Binary files /dev/null and b/.images/metrics.png differ diff --git a/metric/README_zh.md b/metric/README_zh.md new file mode 100644 index 0000000000..8c740cdf7d --- /dev/null +++ b/metric/README_zh.md @@ -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) 来运行本例。 diff --git a/metric/go-client/cmd/client.go b/metric/go-client/cmd/client.go index d7587c33c8..535f7b214f 100644 --- a/metric/go-client/cmd/client.go +++ b/metric/go-client/cmd/client.go @@ -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" ) @@ -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) + } +} \ No newline at end of file diff --git a/metric/go-server/cmd/server.go b/metric/go-server/cmd/server.go index 3332f6e910..f24f7bbdea 100644 --- a/metric/go-server/cmd/server.go +++ b/metric/go-server/cmd/server.go @@ -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" diff --git a/metric/go-server/conf/server.yml b/metric/go-server/conf/server.yml index 8de7e26fa5..c869d8a7c3 100644 --- a/metric/go-server/conf/server.yml +++ b/metric/go-server/conf/server.yml @@ -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" @@ -58,4 +58,4 @@ protocol_conf: tcp_write_timeout: "5s" wait_timeout: "1s" max_msg_len: 1024000 - session_name: "server" + session_name: "server" \ No newline at end of file diff --git a/metric/go-server/docker/config/prometheus.yml b/metric/go-server/docker/config/prometheus.yml new file mode 100644 index 0000000000..6293507c33 --- /dev/null +++ b/metric/go-server/docker/config/prometheus.yml @@ -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' ] \ No newline at end of file diff --git a/metric/go-server/docker/docker-compose.yml b/metric/go-server/docker/docker-compose.yml index 8724179af5..e859fd0a15 100644 --- a/metric/go-server/docker/docker-compose.yml +++ b/metric/go-server/docker/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/metric/go-server/pkg/user.go b/metric/go-server/pkg/user.go index 84042ea70a..949d6031fc 100644 --- a/metric/go-server/pkg/user.go +++ b/metric/go-server/pkg/user.go @@ -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() @@ -64,4 +65,4 @@ func (u *UserProvider) Reference() string { func (u User) JavaClassName() string { return "org.apache.dubbo.User" -} +} \ No newline at end of file