forked from EnvelopeRainGroup10/Consumer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (124 loc) · 3.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
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
package main
import (
"Consumer/logger"
"Consumer/sqlmodule"
"fmt"
"github.com/nsqio/go-nsq"
"log"
"strconv"
"strings"
)
func NewCreateEnvelopConsumer(t string, c string, addr string) error {
conf := nsq.NewConfig()
nc, err := nsq.NewConsumer(t, c, conf)
if err != nil {
logger.Logger.Error(err.Error())
return err
}
nc.AddHandler(nsq.HandlerFunc(func(msg *nsq.Message) error {
if string(msg.Body) == "hello" {
return nil
}
parametersList := strings.Split(string(msg.Body), ",")
envelopeId, _ := strconv.ParseInt(parametersList[0], 10, 64)
uid, _ := strconv.ParseInt(parametersList[1], 10, 64)
value, _ := strconv.ParseInt(parametersList[2], 10, 64)
timeStamp, _ := strconv.ParseInt(parametersList[3], 10, 64)
sqlmodule.CreateEnvelopeDetail(envelopeId, uid, value, timeStamp)
return nil
}))
// 连接nsqlookupd
if err:= nc.ConnectToNSQLookupd(addr);err!=nil{
fmt.Println("connect nsqlookupd failed ", err)
return err
}
return nil
}
func NewUpdateCountConsumer(t string, c string, addr string) error {
conf := nsq.NewConfig()
nc, err := nsq.NewConsumer(t, c, conf)
if err != nil {
fmt.Println("create consumer failed err ", err)
return err
}
nc.AddHandler(nsq.HandlerFunc(func(msg *nsq.Message) error {
if string(msg.Body) == "hello" {
return nil
}
uid, err := strconv.ParseInt(string(msg.Body), 10, 64)
if err != nil {
log.Println(err)
}
sqlmodule.GetUser(uid)
sqlmodule.UpdateCountByUid(uid)
return nil
}))
// 连接nsqlookupd
if err:= nc.ConnectToNSQLookupd(addr);err!=nil{
fmt.Println("connect nsqlookupd failed ", err)
return err
}
return nil
}
func NewUpdateStateConsumer(t string, c string, addr string) error {
conf := nsq.NewConfig()
nc, err := nsq.NewConsumer(t, c, conf)
if err != nil {
fmt.Println("create consumer failed err ", err)
return err
}
nc.AddHandler(nsq.HandlerFunc(func(msg *nsq.Message) error {
if string(msg.Body) == "hello" {
return nil
}
parameterList := strings.Split(string(msg.Body), ",")
envelopeId, _ := strconv.ParseInt(parameterList[0], 10, 64)
uid, _ := strconv.ParseInt(parameterList[1], 10, 64)
if err != nil {
log.Println(err)
}
sqlmodule.UpdateStateByEidAndUid(envelopeId, uid)
return nil
}))
// 连接nsqlookupd
if err:= nc.ConnectToNSQLookupd(addr);err!=nil{
fmt.Println("connect nsqlookupd failed ", err)
return err
}
return nil
}
func main() {
_, err := sqlmodule.InitDB()
if err != nil {
return
}
logger.InitLogger()
go func() {
addr := "111.62.107.178:4161"
err := NewCreateEnvelopConsumer("CreateEnvelopeDetail", "channel-1", addr)
if err != nil {
fmt.Println("new nsq consumer failed", err)
return
}
select {}
}()
go func() {
addr := "111.62.107.178:4161"
err := NewUpdateCountConsumer("UpdateCountByUid", "channel-1", addr)
if err != nil {
fmt.Println("new nsq consumer failed", err)
return
}
select {}
}()
go func() {
addr := "111.62.107.178:4161"
err := NewUpdateStateConsumer("UpdateStateByEidAndUid", "channel-1", addr)
if err != nil {
fmt.Println("new nsq consumer failed", err)
return
}
select {}
}()
select {}
}