正在显示
4 个修改的文件
包含
85 行增加
和
2 行删除
@@ -14,7 +14,7 @@ require ( | @@ -14,7 +14,7 @@ require ( | ||
14 | //github.com/opentracing/opentracing-go v1.1.1-0.20190913142402-a7454ce5950e | 14 | //github.com/opentracing/opentracing-go v1.1.1-0.20190913142402-a7454ce5950e |
15 | //github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5 | 15 | //github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5 |
16 | //github.com/openzipkin/zipkin-go v0.2.5 | 16 | //github.com/openzipkin/zipkin-go v0.2.5 |
17 | - github.com/tiptok/gocomm v1.0.6 | 17 | + github.com/tiptok/gocomm v1.0.8 |
18 | ) | 18 | ) |
19 | 19 | ||
20 | -//replace github.com/tiptok/gocomm v1.0.5 => F:\go\src\learn_project\gocomm | 20 | +//replace github.com/tiptok/gocomm v1.0.8 => F:\go\src\learn_project\gocomm |
@@ -57,5 +57,8 @@ func main() { | @@ -57,5 +57,8 @@ func main() { | ||
57 | //} | 57 | //} |
58 | log.Debug(constant.REDIS_HOST, constant.REDIS_PORT) | 58 | log.Debug(constant.REDIS_HOST, constant.REDIS_PORT) |
59 | 59 | ||
60 | + //go message.RunConsumer() | ||
61 | + //go message.RunPublish() | ||
62 | + | ||
60 | beego.Run() | 63 | beego.Run() |
61 | } | 64 | } |
pkg/port/message/pub.go
0 → 100644
1 | +package message | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/tiptok/gocomm/pkg/broker" | ||
5 | + "github.com/tiptok/gocomm/pkg/broker/local" | ||
6 | + "github.com/tiptok/gocomm/pkg/broker/models" | ||
7 | + "github.com/tiptok/gocomm/pkg/log" | ||
8 | + "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain" | ||
9 | + "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg" | ||
10 | + "time" | ||
11 | +) | ||
12 | + | ||
13 | +func RunPublish() { | ||
14 | + var ( | ||
15 | + err error | ||
16 | + ) | ||
17 | + | ||
18 | + producer := broker.NewMessageProducer( | ||
19 | + models.WithKafkaHost(KAFKA_HOSTS), | ||
20 | + models.WithMessageProduceRepository(local.NewPgMessageProduceRepository(pg.DB, nil)), | ||
21 | + ) | ||
22 | + for { | ||
23 | + messages := []interface{}{ | ||
24 | + domain.Users{ | ||
25 | + Id: 20200129, | ||
26 | + Name: "user_2020", | ||
27 | + Phone: "18860183058", | ||
28 | + Status: 1, | ||
29 | + Roles: []int64{7, 8}, | ||
30 | + CreateTime: time.Now(), | ||
31 | + UpdateTime: time.Now(), | ||
32 | + }, | ||
33 | + } | ||
34 | + err = producer.Publish(TOPIC_BUSSINESS_ADMIN, messages, models.WithMessageProduceRepository(local.NewPgMessageProduceRepository(pg.DB, nil))) | ||
35 | + if err != nil { | ||
36 | + log.Error(err) | ||
37 | + } | ||
38 | + log.Debug("publish :", messages) | ||
39 | + time.Sleep(time.Hour * 24) | ||
40 | + } | ||
41 | +} |
pkg/port/message/sub.go
0 → 100644
1 | +package message | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/tiptok/gocomm/common" | ||
5 | + "github.com/tiptok/gocomm/pkg/broker/kafkax" | ||
6 | + "github.com/tiptok/gocomm/pkg/broker/local" | ||
7 | + "github.com/tiptok/gocomm/pkg/broker/models" | ||
8 | + "github.com/tiptok/gocomm/pkg/log" | ||
9 | + "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain" | ||
10 | + "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg" | ||
11 | +) | ||
12 | + | ||
13 | +const ( | ||
14 | + KAFKA_HOSTS = "106.52.15.41:9092" // "192.168.0.250:9092,192.168.0.251:9092,192.168.0.252:9092" //"106.52.15.41:9092" // | ||
15 | + GroupId = "godevp_test" // "calf_basic_test" //"godevp" | ||
16 | + TOPIC_BUSSINESS_ADMIN = "mmm_business_test" | ||
17 | +) | ||
18 | + | ||
19 | +func RunConsumer() { | ||
20 | + saramaConsumer := kafkax.NewSaramaConsumer(KAFKA_HOSTS, GroupId) // models.WithVersion("0.10.2.1") | ||
21 | + saramaConsumer.WithTopicHandler(TOPIC_BUSSINESS_ADMIN, UserLoginHandler) | ||
22 | + saramaConsumer.WithMessageReceiver(local.NewPgMessageReceiverRepository(pg.DB, nil)) // 持久化 | ||
23 | + | ||
24 | + err := saramaConsumer.StartConsume() | ||
25 | + if err != nil { | ||
26 | + log.Error(err) | ||
27 | + } | ||
28 | +} | ||
29 | + | ||
30 | +func UserLoginHandler(message interface{}) error { | ||
31 | + msg, ok := message.(*models.Message) | ||
32 | + if ok { | ||
33 | + var user = &domain.Users{} | ||
34 | + common.JsonUnmarshal(string(msg.Value), user) | ||
35 | + log.Info("消费消息:", msg.Id, msg.Topic, msg.Value) | ||
36 | + log.Info("登录用户信息:", user.Id, user.Name) | ||
37 | + } | ||
38 | + return nil | ||
39 | +} |
-
请 注册 或 登录 后发表评论