forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·65 lines (58 loc) · 1.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"log"
"time"
"github.com/go-chassis/go-chassis"
_ "github.com/go-chassis/go-chassis/bootstrap"
"github.com/go-chassis/go-chassis/client/rest"
"github.com/go-chassis/go-chassis/core"
"github.com/go-chassis/go-chassis/core/lager"
)
//if you use go run main.go instead of binary run, plz export CHASSIS_HOME=/path/to/conf/folder
func main() {
//chassis operation
if err := chassis.Init(); err != nil {
lager.Logger.Error("Init failed.", err)
return
}
restInvoker := core.NewRestInvoker()
// use the configured chain
for {
callRest(restInvoker, 10)
<-time.After(time.Second)
}
}
func callRest(invoker *core.RestInvoker, i int) {
url := "cse://istioserver/sayhello/b"
if i < 10 {
url = "cse://istioserver/sayhello/a"
}
req, _ := rest.NewRequest("GET", url)
//use the invoker like http client.
resp1, err := invoker.ContextDo(context.TODO(), req)
if err != nil {
log.Println(err)
//lager.Logger.Errorf(err, "call request fail (%s) (%d) ", string(resp1.ReadBody()), resp1.GetStatusCode())
//return
}
log.Println(i, "REST SayHello ------------------------------ ", resp1.GetStatusCode(), string(resp1.ReadBody()))
//req, _ = rest.NewRequest(http.MethodPost, "cse://Server/sayhi", []byte(`{"name": "peter wang and me"}`))
//req.SetHeader("Content-Type", "application/json")
//resp1, err = invoker.ContextDo(context.TODO(), req)
//if err != nil {
// log.Println(err)
// return
//}
//log.Printf("Rest Server sayhi[POST] %s", string(resp1.ReadBody()))
//
//req, _ = rest.NewRequest(http.MethodGet, "cse://Server/sayerror", []byte(""))
//resp1, err = invoker.ContextDo(context.TODO(), req)
//if err != nil {
// log.Println(err)
// return
//}
//log.Printf("Rest Server sayerror[GET] %s ", string(resp1.ReadBody()))
req.Close()
resp1.Close()
}