作者 唐旭辉

调试

@@ -6,7 +6,6 @@ import ( @@ -6,7 +6,6 @@ import (
6 "os/signal" 6 "os/signal"
7 "sync" 7 "sync"
8 "syscall" 8 "syscall"
9 - "time"  
10 9
11 "github.com/astaxie/beego" 10 "github.com/astaxie/beego"
12 "github.com/astaxie/beego/logs" 11 "github.com/astaxie/beego/logs"
@@ -14,7 +13,6 @@ import ( @@ -14,7 +13,6 @@ import (
14 _ "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/log" 13 _ "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/log"
15 _ "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego" 14 _ "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/beego"
16 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/consumer" 15 "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/consumer"
17 - "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/consumer/produce"  
18 ) 16 )
19 17
20 func main() { 18 func main() {
@@ -26,7 +24,7 @@ func main() { @@ -26,7 +24,7 @@ func main() {
26 beego.Run() 24 beego.Run()
27 }() 25 }()
28 consumerRun := consumer.NewRuner() 26 consumerRun := consumer.NewRuner()
29 - logs.Debug("%+v", consumerRun) 27 +
30 if err := consumerRun.InitConsumer(); err != nil { 28 if err := consumerRun.InitConsumer(); err != nil {
31 logs.Error("启动kafka消息消费者失败:%s", err) 29 logs.Error("启动kafka消息消费者失败:%s", err)
32 } 30 }
@@ -40,12 +38,11 @@ func main() { @@ -40,12 +38,11 @@ func main() {
40 <-consumerRun.IsReady() 38 <-consumerRun.IsReady()
41 logs.Info("Sarama consumer up and running!...") 39 logs.Info("Sarama consumer up and running!...")
42 }() 40 }()
43 - go func() {  
44 - t := time.NewTimer(7 * time.Second)  
45 - <-t.C  
46 - err := produce.Producer()  
47 - logs.Info("err:%s", err)  
48 - }() 41 + // go func() {
  42 + // t := time.NewTimer(7 * time.Second)
  43 + // <-t.C
  44 + // produce.Producer()
  45 + // }()
49 for { 46 for {
50 select { 47 select {
51 case <-sigs: 48 case <-sigs:
@@ -18,8 +18,8 @@ type MessageConsumer struct { @@ -18,8 +18,8 @@ type MessageConsumer struct {
18 groupId string 18 groupId string
19 topics []string 19 topics []string
20 topicsHandles map[string]TopicHandle 20 topicsHandles map[string]TopicHandle
21 - beforeHandles []TopicHandle  
22 - afterHandles []TopicHandle 21 + // beforeHandles []TopicHandle
  22 + // afterHandles []TopicHandle
23 } 23 }
24 24
25 //实现对应的接口 25 //实现对应的接口
@@ -41,13 +41,8 @@ func (c *MessageConsumer) ConsumeClaim(groupSession sarama.ConsumerGroupSession, @@ -41,13 +41,8 @@ func (c *MessageConsumer) ConsumeClaim(groupSession sarama.ConsumerGroupSession,
41 err error 41 err error
42 ) 42 )
43 for message := range groupClaim.Messages() { 43 for message := range groupClaim.Messages() {
44 -  
45 logs.Debug("Done Message claimed: timestamp = %v, topic = %s offset = %v value = %v \n", 44 logs.Debug("Done Message claimed: timestamp = %v, topic = %s offset = %v value = %v \n",
46 message.Timestamp, message.Topic, message.Offset, string(message.Value)) 45 message.Timestamp, message.Topic, message.Offset, string(message.Value))
47 - for i := range c.beforeHandles {  
48 - c.beforeHandles[i](message)  
49 - }  
50 - groupSession.MarkMessage(message, "")  
51 if topicHandle, err = c.FindTopichandle(groupClaim.Topic()); err != nil { 46 if topicHandle, err = c.FindTopichandle(groupClaim.Topic()); err != nil {
52 logs.Error("FindTopichandle err:%s \n", err) 47 logs.Error("FindTopichandle err:%s \n", err)
53 continue 48 continue
@@ -55,9 +50,7 @@ func (c *MessageConsumer) ConsumeClaim(groupSession sarama.ConsumerGroupSession, @@ -55,9 +50,7 @@ func (c *MessageConsumer) ConsumeClaim(groupSession sarama.ConsumerGroupSession,
55 if err = topicHandle(message); err != nil { 50 if err = topicHandle(message); err != nil {
56 logs.Error("Message claimed: kafka消息处理错误 topic =", message.Topic, message.Offset, err) 51 logs.Error("Message claimed: kafka消息处理错误 topic =", message.Topic, message.Offset, err)
57 } 52 }
58 - for i := range c.beforeHandles {  
59 - c.afterHandles[i](message)  
60 - } 53 + groupSession.MarkMessage(message, "")
61 } 54 }
62 return nil 55 return nil
63 } 56 }
@@ -79,18 +72,20 @@ func NewRuner() *Runer { @@ -79,18 +72,20 @@ func NewRuner() *Runer {
79 for key := range TopicHandleRouters { 72 for key := range TopicHandleRouters {
80 topics = append(topics, key) 73 topics = append(topics, key)
81 } 74 }
82 -  
83 - return &Runer{ 75 + r := &Runer{
84 msgConsumer: &MessageConsumer{ 76 msgConsumer: &MessageConsumer{
85 ready: make(chan struct{}), 77 ready: make(chan struct{}),
86 kafkaHosts: configs.Cfg.Servers, 78 kafkaHosts: configs.Cfg.Servers,
87 groupId: configs.Cfg.ConsumerId, 79 groupId: configs.Cfg.ConsumerId,
88 topicsHandles: TopicHandleRouters, 80 topicsHandles: TopicHandleRouters,
89 topics: topics, 81 topics: topics,
90 - beforeHandles: BeforeHandles,  
91 - afterHandles: AfterHandles, 82 + // beforeHandles: BeforeHandles,
  83 + // afterHandles: AfterHandles,
92 }, 84 },
93 } 85 }
  86 + logs.Debug("kafka_host=%v; topic=%v;groupid=%s ", r.msgConsumer.kafkaHosts,
  87 + r.msgConsumer.topics, r.msgConsumer.groupId)
  88 + return r
94 } 89 }
95 90
96 func (r *Runer) InitConsumer() error { 91 func (r *Runer) InitConsumer() error {
  1 +package handles
  2 +
  3 +import "encoding/json"
  4 +
  5 +type DataFromMessage struct {
  6 + Module string `json:"module"`
  7 + Action string `json:"action"`
  8 + Data json.RawMessage `json:"data"`
  9 +}
@@ -10,12 +10,6 @@ import ( @@ -10,12 +10,6 @@ import (
10 syncOrderSrv "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/syncOrder/service" 10 syncOrderSrv "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/syncOrder/service"
11 ) 11 )
12 12
13 -type DataFromMessage struct {  
14 - Module string `json:"module"`  
15 - Action string `json:"action"`  
16 - Data json.RawMessage `json:"data"`  
17 -}  
18 -  
19 func DataFromXiangMi(message *sarama.ConsumerMessage) error { 13 func DataFromXiangMi(message *sarama.ConsumerMessage) error {
20 logs.Info("Done Message claimed: timestamp = %v, topic = %s offset = %v value = %v \n", 14 logs.Info("Done Message claimed: timestamp = %v, topic = %s offset = %v value = %v \n",
21 message.Timestamp, message.Topic, message.Offset, string(message.Value)) 15 message.Timestamp, message.Topic, message.Offset, string(message.Value))
@@ -27,33 +21,38 @@ func DataFromXiangMi(message *sarama.ConsumerMessage) error { @@ -27,33 +21,38 @@ func DataFromXiangMi(message *sarama.ConsumerMessage) error {
27 if err != nil { 21 if err != nil {
28 return fmt.Errorf("[Consumer][SyncBestshopOrder] 解析kafka数据失败;%s", err) 22 return fmt.Errorf("[Consumer][SyncBestshopOrder] 解析kafka数据失败;%s", err)
29 } 23 }
30 - err = SyncBestshopOrder(msgData.Data) 24 + dataAction := msgData.Module + "/" + msgData.Action
  25 + switch dataAction {
  26 + case "xiangmi.order/ship":
  27 + err = syncBestshopOrder(msgData.Data)
31 if err != nil { 28 if err != nil {
32 e := fmt.Errorf("[Consumer][SyncBestshopOrder] %s", err) 29 e := fmt.Errorf("[Consumer][SyncBestshopOrder] %s", err)
33 return e 30 return e
34 } 31 }
  32 + default:
  33 + logs.Error("未找到执行动作:Module=%s,Action=%s", msgData.Module, msgData.Action)
  34 + }
35 return nil 35 return nil
36 } 36 }
37 37
38 //SyncBestshopOrder 同步 38 //SyncBestshopOrder 同步
39 -func SyncBestshopOrder(data []byte) error {  
40 - 39 +func syncBestshopOrder(data []byte) error {
41 var ( 40 var (
42 cmd syncOrderCmd.CreateOrderFromBestshop 41 cmd syncOrderCmd.CreateOrderFromBestshop
43 err error 42 err error
44 ) 43 )
45 err = json.Unmarshal(data, &cmd) 44 err = json.Unmarshal(data, &cmd)
46 if err != nil { 45 if err != nil {
47 - return fmt.Errorf("[Consumer][SyncBestshopOrder] 解析kafka数据失败;%s", err) 46 + return fmt.Errorf("[Consumer][syncBestshopOrder] 解析kafka数据失败;%s", err)
48 } 47 }
49 if cmd.PartnerId <= 0 { 48 if cmd.PartnerId <= 0 {
50 - logs.Info("[Consumer][SyncBestshopOrder] PartnerId<=0 ,不处理消息") 49 + logs.Info("[Consumer][syncBestshopOrder] PartnerId<=0 ,不处理消息")
51 return nil 50 return nil
52 } 51 }
53 srv := syncOrderSrv.NewOrderInfoService(nil) 52 srv := syncOrderSrv.NewOrderInfoService(nil)
54 err = srv.SyncOrderFromBestshop(cmd) 53 err = srv.SyncOrderFromBestshop(cmd)
55 if err != nil { 54 if err != nil {
56 - e := fmt.Errorf("[Consumer][SyncBestshopOrder] %s", err) 55 + e := fmt.Errorf("[Consumer][syncBestshopOrder] %s", err)
57 return e 56 return e
58 } 57 }
59 return err 58 return err
1 package consumer 1 package consumer
2 2
3 -import (  
4 - "encoding/json"  
5 - "fmt" 3 +// import (
  4 +// "encoding/json"
  5 +// "fmt"
6 6
7 - "github.com/Shopify/sarama"  
8 - "github.com/astaxie/beego/logs"  
9 - syncOrderCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/syncOrder/command"  
10 - syncOrderSrv "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/syncOrder/service"  
11 -) 7 +// "github.com/Shopify/sarama"
  8 +// "github.com/astaxie/beego/logs"
  9 +// syncOrderCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/syncOrder/command"
  10 +// syncOrderSrv "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/syncOrder/service"
  11 +// )
12 12
13 -//SyncBestshopOrder 同步  
14 -func SyncBestshopOrder(message *sarama.ConsumerMessage) error {  
15 - logs.Info("Done Message claimed: timestamp = %v, topic = %s offset = %v value = %v \n",  
16 - message.Timestamp, message.Topic, message.Offset, string(message.Value))  
17 - var (  
18 - cmd syncOrderCmd.CreateOrderFromBestshop  
19 - err error  
20 - )  
21 - err = json.Unmarshal(message.Value, &cmd)  
22 - if err != nil {  
23 - return fmt.Errorf("[Consumer][SyncBestshopOrder] 解析kafka数据失败;%s", err)  
24 - }  
25 - if cmd.PartnerId <= 0 {  
26 - logs.Info("[Consumer][SyncBestshopOrder] PartnerId<=0 ,不处理消息")  
27 - return nil  
28 - }  
29 - srv := syncOrderSrv.NewOrderInfoService(nil)  
30 - err = srv.SyncOrderFromBestshop(cmd)  
31 - if err != nil {  
32 - e := fmt.Errorf("[Consumer][SyncBestshopOrder] %s", err)  
33 - return e  
34 - }  
35 - return err  
36 -} 13 +// //SyncBestshopOrder 同步
  14 +// func SyncBestshopOrder(message *sarama.ConsumerMessage) error {
  15 +// logs.Info("Done Message claimed: timestamp = %v, topic = %s offset = %v value = %v \n",
  16 +// message.Timestamp, message.Topic, message.Offset, string(message.Value))
  17 +// var (
  18 +// cmd syncOrderCmd.CreateOrderFromBestshop
  19 +// err error
  20 +// )
  21 +// err = json.Unmarshal(message.Value, &cmd)
  22 +// if err != nil {
  23 +// return fmt.Errorf("[Consumer][SyncBestshopOrder] 解析kafka数据失败;%s", err)
  24 +// }
  25 +// if cmd.PartnerId <= 0 {
  26 +// logs.Info("[Consumer][SyncBestshopOrder] PartnerId<=0 ,不处理消息")
  27 +// return nil
  28 +// }
  29 +// srv := syncOrderSrv.NewOrderInfoService(nil)
  30 +// err = srv.SyncOrderFromBestshop(cmd)
  31 +// if err != nil {
  32 +// e := fmt.Errorf("[Consumer][SyncBestshopOrder] %s", err)
  33 +// return e
  34 +// }
  35 +// return err
  36 +// }