-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
149 lines (130 loc) · 3.36 KB
/
main.go
File metadata and controls
149 lines (130 loc) · 3.36 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"flag"
"fmt"
proto "gmicro/rpc"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
"github.com/gmsec/micro"
"github.com/xxjwxc/gowp/workpool"
"github.com/xxjwxc/public/mylog"
)
var tag string
func init() {
addFlag(flag.CommandLine)
}
func addFlag(fs *flag.FlagSet) {
// env
fs.StringVar(&tag, "tag", "client", "service or client")
}
func main() {
flag.Parse()
// reg := registry.NewDNSNamingRegistry()
// 初始化服务
service := micro.NewService(
micro.WithName("lp.srv.eg1"),
// micro.WithRegisterTTL(time.Second*30), //指定服务注册时间
micro.WithRegisterInterval(time.Second*15), //让服务在指定时间内重新注册
//micro.WithRegistryNaming(reg),
)
if tag == "server" {
proto.RegisterHelloServer(service.Server(), &hello{})
// run server
if err := service.Run(); err != nil {
panic(err)
}
fmt.Println("stop service")
} else {
go func() { // 通过服务发现模块连接
wp := workpool.New(200) //设置最大线程数
for i := 0; i < 2000; i++ { //开启20个请求
wp.Do(func() error {
widthRegistry()
return nil
})
}
wp.Wait()
fmt.Println("down")
}()
go func() {
wp := workpool.New(200) //设置最大线程数
for i := 0; i < 2000; i++ { //开启20个请求
wp.Do(func() error {
widthIPAddr()
return nil
})
}
wp.Wait()
fmt.Println("down")
}()
}
wait()
}
// 注册发现模块
func widthRegistry() {
micro.SetClientServiceName(proto.GetHelloName(), "lp.srv.eg1") // set client group
say := proto.GetHelloClient()
var request proto.HelloRequest
r := rand.Intn(500)
request.Name = fmt.Sprintf("%v", r)
ctx := context.Background()
for i := 0; i < 10; i++ {
resp, err := say.SayHello(ctx, &request)
if err != nil {
mylog.Error(err)
fmt.Println("==========err:", err)
}
fmt.Println(resp)
time.Sleep(1 * time.Second)
}
}
// widthIPAddr 通过ip访问(非服务发现模式)
func widthIPAddr() {
micro.SetClientServiceAddr(proto.GetHelloName(), "127.0.0.1:50051")
// micro.SetClientServiceName(proto.GetHelloName(), "lp.srv.eg1") // set client group
hello := proto.GetHelloClient()
var request proto.HelloRequest
ctx := context.Background()
for i := 0; i < 10; i++ {
r := rand.Intn(500)
request.Name = fmt.Sprintf("%v", r)
resp, err := hello.SayHello(ctx, &request)
if err != nil {
mylog.Error(err)
fmt.Println("==========err:", err)
}
fmt.Println(resp)
time.Sleep(1 * time.Second)
}
}
func wait() {
// Go signal notification works by sending `os.Signal`
// values on a channel. We'll create a channel to
// receive these notifications (we'll also make one to
// notify us when the program can exit).
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
// `signal.Notify` registers the given channel to
// receive notifications of the specified signals.
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// This goroutine executes a blocking receive for
// signals. When it gets one it'll print it out
// and then notify the program that it can finish.
go func() {
sig := <-sigs
fmt.Println()
fmt.Println(sig)
done <- true
}()
// The program will wait here until it gets the
// expected signal (as indicated by the goroutine
// above sending a value on `done`) and then exit.
fmt.Println("awaiting signal")
<-done
fmt.Println("exiting")
fmt.Println("down")
}