Skip to content

Commit 6d5f7b5

Browse files
committed
add example
1 parent 6b5b191 commit 6d5f7b5

File tree

20 files changed

+295
-20
lines changed

20 files changed

+295
-20
lines changed

cse_reporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func GetReporter() (*Reporter, error) {
4444
openlogging.GetLogger().Warnf("Get Monitoring URL failed, CSE monitoring function disabled, err: %v", err)
4545
errResult = err
4646
}
47-
openlogging.GetLogger().Infof("send metric to monitoring service : %s", monitorServerURL)
47+
openlogging.GetLogger().Infof("init monitoring client : %s", monitorServerURL)
4848
tlsConfig, err := getTLSForClient(monitorServerURL)
4949
if err != nil {
5050
openlogging.GetLogger().Errorf("Get %s.%s TLS config failed,error : %s", monitorServerURL, common.Consumer, err)

csemonitor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ func NewReporter(config *CseCollectorConfig) (*Reporter, error) {
2626
openlogging.GetLogger().Errorf("Get cse monitor client failed:%s", err)
2727
return nil, err
2828
}
29-
reporter := &Reporter{
30-
environment: config.Env,
31-
}
3229
IsMonitoringConnected = true
3330
return &Reporter{
3431
environment: config.Env,
@@ -40,6 +37,9 @@ func NewReporter(config *CseCollectorConfig) (*Reporter, error) {
4037
func (reporter *Reporter) Send(cb *hystrix.CircuitBreaker) {
4138
if archaius.GetBool("cse.monitor.client.enable", true) {
4239
monitorData := reporter.getData(cb)
40+
openlogging.Debug("send metrics", openlogging.WithTags(openlogging.Tags{
41+
"data": monitorData,
42+
}))
4343
err := reporter.c.PostMetrics(monitorData)
4444
if err != nil {
4545
openlogging.GetLogger().Warnf("unable to report to monitoring server, err: %v", err)
@@ -60,6 +60,6 @@ func (reporter *Reporter) getData(cb *hystrix.CircuitBreaker) monitoring.Monitor
6060
monitorData.Memory = getProcessInfo()
6161
monitorData.Thread = threadCreateProfile.Count()
6262
monitorData.CPU = float64(runtime.NumCPU())
63-
monitorData.AppendInterfaceInfo(cb.Name, cb.Metrics.DefaultCollector())
63+
monitorData.AppendInterfaceInfo(cb)
6464
return *monitorData
6565
}

example/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
4+
## build
5+
```shell
6+
cd example
7+
export GOPROXY=https://goproxy.io
8+
export REPO=swr.cn-east-2.myhuaweicloud.com/tian
9+
build_image.sh
10+
```
11+
12+
13+
14+

example/build_image.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
4+
export ROOT=$PWD
5+
cd order
6+
set -e
7+
set -x
8+
#GO111MODULE=on go mod vendor
9+
build_image(){
10+
cd ${ROOT}/$1
11+
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main
12+
sudo docker build --build-arg VERSION=$2 -t ${REPO}/$1:$2 .
13+
sudo docker push ${REPO}/$1:$2
14+
}
15+
16+
build_image order 1.0.0
17+
build_image restaurant 1.0.0
18+
build_image restaurant 1.0.1

example/order/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM alpine:latest
2+
WORKDIR /opt/order
3+
COPY conf /opt/order/conf
4+
COPY main /opt/order
5+
ENTRYPOINT ["/opt/order/main"]

example/order/conf/chassis.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
#APPLICATION_ID: CSE optional
3+
cse:
4+
service:
5+
registry:
6+
address: http://127.0.0.1:30100
7+
protocols:
8+
rest:
9+
listenAddress: 0.0.0.0:5000
10+
advertiseAddress: 127.0.0.1:5000
11+
handler:
12+
chain:
13+
Consumer:
14+
default: router,bizkeeper-consumer,loadbalance,transport

example/order/conf/lager.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
# LoggerLevel: |DEBUG|INFO|WARN|ERROR|FATAL
3+
logger_level: DEBUG
4+
5+
# LoggerFile: used to output the name of log.可配置绝对路径,也可以配置用于拼接CHASSIS_HOME的相对路径。
6+
logger_file: log/chassis.log
7+
8+
# LogFormatText:设定日志的输出格式是 json 还是 plaintext (类似于log4j),默认为 false,不建议修改,如果开发过程中想本地查看日志的话,
9+
# 可以设定 LoggerFile 和 LogFormatText 为 true,这样会输出类似于 log4j 格式的本地日志。
10+
log_format_text: false
11+
12+
#rollingPolicy daily/size;用于配置根据时间,还是根据大小进行日志rotate操作。
13+
rollingPolicy: size
14+
15+
# MaxDaily of a log file before rotate. By D Days.;日志rotate时间配置,单位"day"。
16+
log_rotate_date: 1
17+
18+
# MaxSize of a log file before rotate. By M Bytes.;日志rotate文件大小配置,单位"MB"。
19+
log_rotate_size: 10
20+
21+
# Max counts to keep of a log's backup files.日志最大存储数量,单位“个”。
22+
log_backup_count: 7
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
#微服务的私有属性
3+
service_description:
4+
name: order
5+
version: 1.0.0

example/order/main.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"github.com/go-chassis/go-chassis"
5+
"github.com/go-mesh/openlogging"
6+
_ "github.com/huaweicse/auth/adaptor/gochassis"
7+
_ "github.com/huaweicse/cse-collector"
8+
"github.com/huaweicse/cse-collector/example/order/resource"
9+
"log"
10+
"os"
11+
)
12+
13+
//if you use go run main.go instead of binary run, plz export CHASSIS_HOME=/{path}/{to}/rest/client/
14+
15+
func main() {
16+
envs := os.Environ()
17+
for _, e := range envs {
18+
log.Println(e)
19+
}
20+
chassis.RegisterSchema("rest", &resource.OrderResource{})
21+
//Init framework
22+
if err := chassis.Init(); err != nil {
23+
openlogging.Error("Init failed." + err.Error())
24+
return
25+
}
26+
27+
err := chassis.Run()
28+
if err != nil {
29+
panic(err)
30+
}
31+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package resource
19+
20+
import (
21+
"github.com/go-chassis/go-chassis/client/rest"
22+
"github.com/go-chassis/go-chassis/core"
23+
"github.com/go-chassis/go-chassis/pkg/util/httputil"
24+
"github.com/go-chassis/go-chassis/server/restful"
25+
"net/http"
26+
)
27+
28+
type OrderResource struct {
29+
}
30+
31+
func (r *OrderResource) Get(context *restful.Context) {
32+
orderID := context.ReadPathParameter("order_id")
33+
invoker := core.NewRestInvoker()
34+
req, err := rest.NewRequest("GET", "http://restaurant/v1/restaurant/"+orderID, nil)
35+
if err != nil {
36+
context.Write([]byte(err.Error()))
37+
context.WriteHeader(http.StatusInternalServerError)
38+
}
39+
resp, err := invoker.ContextDo(context.Ctx, req)
40+
if err != nil {
41+
context.Write([]byte(err.Error()))
42+
context.WriteHeader(http.StatusInternalServerError)
43+
return
44+
}
45+
restaurant := httputil.ReadBody(resp)
46+
context.Write(restaurant)
47+
}
48+
49+
func (r *OrderResource) URLPatterns() []restful.Route {
50+
return []restful.Route{
51+
{Method: http.MethodGet, Path: "/v1/order/{order_id}", ResourceFuncName: "Get"},
52+
}
53+
}

0 commit comments

Comments
 (0)