作者 唐旭辉

更新

@@ -24,12 +24,12 @@ func main() { @@ -24,12 +24,12 @@ func main() {
24 beego.Run() 24 beego.Run()
25 }() 25 }()
26 consumerRun := consumer.NewRuner() 26 consumerRun := consumer.NewRuner()
27 - if err := consumerRun.InitConsumer(); err != nil {  
28 - logs.Error("启动kafka消息消费者失败:%s", err)  
29 - }  
30 wg := &sync.WaitGroup{} 27 wg := &sync.WaitGroup{}
31 wg.Add(1) 28 wg.Add(1)
32 go func() { 29 go func() {
  30 + if err := consumerRun.InitConsumer(); err != nil {
  31 + logs.Error("启动kafka消息消费者失败:%s", err)
  32 + }
33 consumerRun.Start(ctx) 33 consumerRun.Start(ctx)
34 wg.Done() 34 wg.Done()
35 }() 35 }()
@@ -14,7 +14,7 @@ var KafkaCfg KafkaConfig @@ -14,7 +14,7 @@ var KafkaCfg KafkaConfig
14 14
15 func init() { 15 func init() {
16 KafkaCfg = KafkaConfig{ 16 KafkaCfg = KafkaConfig{
17 - Servers: []string{"106.52.15.41:9092"}, 17 + Servers: []string{"192.168.190.136:9092"},
18 ConsumerId: "partnermg_local", 18 ConsumerId: "partnermg_local",
19 } 19 }
20 if os.Getenv("KAFKA_HOST") != "" { 20 if os.Getenv("KAFKA_HOST") != "" {
@@ -95,6 +95,9 @@ func (good OrderGoodBestShop) CopyToOrderGood(g *OrderGood) { @@ -95,6 +95,9 @@ func (good OrderGoodBestShop) CopyToOrderGood(g *OrderGood) {
95 g.Price = good.Price 95 g.Price = good.Price
96 g.PlanGoodNumber = good.Nums 96 g.PlanGoodNumber = good.Nums
97 g.GoodCompute.PlanAmount = good.Amount 97 g.GoodCompute.PlanAmount = good.Amount
  98 + g.BonusStatus = OrderGoodWaitPay
  99 + g.CurrentBonusStatus = OrderGoodBonusWaitPay{}
  100 + g.CurrentBonusStatus.WartPayPartnerBonus(g)
98 return 101 return
99 } 102 }
100 103
@@ -22,20 +22,6 @@ type MessageConsumer struct { @@ -22,20 +22,6 @@ type MessageConsumer struct {
22 afterHandles []TopicHandle 22 afterHandles []TopicHandle
23 } 23 }
24 24
25 -// func NewMessageConsumer() *MessageConsumer {  
26 -// topics := []string{}  
27 -// for key := range TopicHandleRouters {  
28 -// topics = append(topics, key)  
29 -// }  
30 -// return &MessageConsumer{  
31 -// ready: make(chan bool),  
32 -// kafkaHosts: configs.Cfg.Servers,  
33 -// groupId: configs.Cfg.ConsumerId,  
34 -// topicsHandles: TopicHandleRouters,  
35 -// topics: topics,  
36 -// }  
37 -// }  
38 -  
39 //实现对应的接口 25 //实现对应的接口
40 var _ sarama.ConsumerGroupHandler = (*MessageConsumer)(nil) 26 var _ sarama.ConsumerGroupHandler = (*MessageConsumer)(nil)
41 27
@@ -128,10 +114,12 @@ func (r *Runer) Start(ctx context.Context) { @@ -128,10 +114,12 @@ func (r *Runer) Start(ctx context.Context) {
128 for { 114 for {
129 select { 115 select {
130 case <-ctx.Done(): 116 case <-ctx.Done():
131 - err := r.consumerGroup.Close()  
132 - logs.Warning("ctx cancel;consumerGroup.Close();err:%s", err) 117 + logs.Warning("ctx cancel;consumerGroup.Close()")
  118 + r.consumerGroup.Close()
133 return 119 return
134 default: 120 default:
  121 +
  122 + }
135 if err := r.consumerGroup.Consume(ctx, r.msgConsumer.topics, r.msgConsumer); err != nil { 123 if err := r.consumerGroup.Consume(ctx, r.msgConsumer.topics, r.msgConsumer); err != nil {
136 logs.Error("consumerGroup err:%s \n", err) 124 logs.Error("consumerGroup err:%s \n", err)
137 //等待重试 125 //等待重试
@@ -140,47 +128,7 @@ func (r *Runer) Start(ctx context.Context) { @@ -140,47 +128,7 @@ func (r *Runer) Start(ctx context.Context) {
140 } 128 }
141 r.msgConsumer.ready = make(chan struct{}) 129 r.msgConsumer.ready = make(chan struct{})
142 } 130 }
143 - }  
144 } 131 }
145 func (r *Runer) IsReady() <-chan struct{} { 132 func (r *Runer) IsReady() <-chan struct{} {
146 return r.msgConsumer.ready 133 return r.msgConsumer.ready
147 } 134 }
148 -  
149 -//StartConsumer 启动  
150 -//返回 Consumer关闭方法 和 error  
151 -// func StartConsumer(ctx context.Context) (func(), error) {  
152 -// consumer := NewMessageConsumer()  
153 -// config := sarama.NewConfig()  
154 -// config.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategyRoundRobin  
155 -// config.Consumer.Offsets.Initial = sarama.OffsetNewest  
156 -// config.Version = sarama.V0_11_0_2  
157 -// consumerGroup, err := sarama.NewConsumerGroup(consumer.kafkaHosts, consumer.groupId, config)  
158 -// if err != nil {  
159 -// return func() {}, err  
160 -// }  
161 -// wg := &sync.WaitGroup{}  
162 -// wg.Add(1)  
163 -// go func() {  
164 -// defer wg.Done()  
165 -// for {  
166 -// if err := ctx.Err(); err != nil {  
167 -// logs.Error("ctx err:%s \n", err)  
168 -// return  
169 -// }  
170 -// if err := consumerGroup.Consume(ctx, consumer.topics, consumer); err != nil {  
171 -// logs.Error("consumerGroup err:%s \n", err)  
172 -// }  
173 -// consumer.ready = make(chan bool)  
174 -// }  
175 -// }()  
176 -// //等待 consumerGroup 设置完成  
177 -// <-consumer.ready  
178 -// logs.Info("Sarama consumer up and running!...")  
179 -// return func() {  
180 -// wg.Wait()  
181 -// if err := consumerGroup.Close(); err != nil {  
182 -// logs.Error("consumerGroup.Close err %s", err)  
183 -// }  
184 -// logs.Info("consumerGroup.Close")  
185 -// }, nil  
186 -// }