-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
63 lines (53 loc) · 1 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
package main
import (
"fmt"
"github.com/nsqio/go-nsq"
"io/ioutil"
"log"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
//读取ip.conf
data, err := ioutil.ReadFile("ip.conf")
if err != nil {
log.Fatalf("readfile error:%v", err)
}
//连接
config := nsq.NewConfig()
w, err := nsq.NewProducer(string(data), config)
if err != nil {
log.Fatalf("nsq.NewProducer:%v", err)
}
defer w.Stop()
//发送消息
i := 0
for {
message := fmt.Sprintf("hello %d", i)
if i%2 == 0 {
if err := w.Publish("test1", []byte(message)); err != nil {
log.Println("发送失败1")
} else {
log.Println("发送消息1", message)
}
} else {
if err := w.Publish("test2", []byte(message)); err != nil {
log.Println("发送失败2")
} else {
log.Println("发送消息2", message)
}
}
i++
if i == 10 {
break
}
time.Sleep(time.Second * 3)
}
//结束
<-sigs
log.Println("end")
}