/
main.go
94 lines (75 loc) · 1.92 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
package main
import (
"context"
"encoding/json"
"os"
"os/signal"
"syscall"
"github.com/go-eagle/eagle/pkg/queue/rabbitmq/options"
"github.com/rabbitmq/amqp091-go"
eagle "github.com/go-eagle/eagle/pkg/app"
"github.com/go-eagle/eagle/pkg/config"
"github.com/spf13/pflag"
logger "github.com/go-eagle/eagle/pkg/log"
"github.com/go-eagle/eagle/pkg/queue/rabbitmq"
)
var (
cfgDir = pflag.StringP("config dir", "c", "config", "config path.")
env = pflag.StringP("env name", "e", "", "env var name.")
)
// cd examples/queue/rabbitmq/consumer/
// go run main.go
func main() {
pflag.Parse()
// init config
c := config.New(*cfgDir, config.WithEnv(*env))
var cfg eagle.Config
if err := c.Load("app", &cfg); err != nil {
panic(err)
}
// set global
eagle.Conf = &cfg
logger.Init()
rabbitmq.Load()
defer rabbitmq.Close()
stopSig := make(chan os.Signal, 1)
signal.Notify(stopSig, syscall.SIGINT, syscall.SIGTERM)
done := make(chan struct{})
stop := make(chan struct{}, 1)
// 自定义消息处理函数
handler := func(ctx context.Context, body amqp091.Delivery) (action rabbitmq.Action) {
msg := make(map[string]interface{})
err := json.Unmarshal(body.Body, &msg)
if err != nil {
logger.Errorf("consumer handler unmarshal msg err: %s", err.Error())
return rabbitmq.NackDiscard
}
logger.Infof("consumer handler receive msg: %s", msg)
return rabbitmq.Ack
}
// rabbitmq consume message
ctx := context.Background()
opts := []options.ConsumerOption{
options.WithConsumerOptionConcurrency(1),
}
go func() {
err := rabbitmq.Consume(ctx, "test-demo", handler, opts...)
if err != nil {
logger.Errorf("rabbitmq consume err: %s", err.Error())
}
}()
for {
select {
case <-stopSig:
logger.Info("received stop signal")
stop <- struct{}{}
case <-stop:
logger.Info("stopping service")
close(done)
return
case <-done:
logger.Info("stopped service gracefully")
return
}
}
}