topic_handle_router.go
1.0 KB
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
package consumer
import (
"os"
"github.com/Shopify/sarama"
"github.com/astaxie/beego/logs"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/port/consumer/handles"
)
//TopicHandle 处理kafka中得消息
type TopicHandle func(*sarama.ConsumerMessage) error
//TopicHandleRouters 根据topic区分消息并进行处理
var TopicHandleRouters = map[string]TopicHandle{
"topic_test": func(message *sarama.ConsumerMessage) error {
logs.Info("Done Message claimed: timestamp = %v, topic = %s offset = %v value = %v \n",
message.Timestamp, message.Topic, message.Offset, string(message.Value))
return nil
},
}
func init() {
var runEnv string
if os.Getenv("KAFKA_CONSUMER_ID") != "" {
runEnv = os.Getenv("KAFKA_CONSUMER_ID")
}
if runEnv == "partnermg_test" {
initHandleRoutersTest()
}
if runEnv == "partnermg_prd" {
initHandleRoutersProd()
}
}
func initHandleRoutersTest() {
TopicHandleRouters["xiangmi_project_test"] = handles.DataFromXiangMi
}
func initHandleRoutersProd() {
TopicHandleRouters["xiangmi_project"] = handles.DataFromXiangMi
}