全局事件总线(Global EventBus)在Go语言中的实现

huangapple go评论113阅读模式
英文:

Global EventBus in GoLang

问题

我正在尝试学习GO,并尝试不同的概念。现在我正在尝试使用PubSub方法,但是在应用程序内部进行。我有一个EventBus,并且我正在尝试通过依赖注入传递实例。然而,当我运行应用程序时,什么都没有发生。

主要代码如下:

  1. package main
  2. import (
  3. "github.com/asaskevich/EventBus"
  4. modelA "interfaces/internal/modelA"
  5. modelB "interfaces/internal/modelB"
  6. )
  7. func main() {
  8. bus := EventBus.New()
  9. a := &modelA.Bus{EventBus: bus}
  10. a.Send()
  11. b := &modelB.Bus{
  12. EventBus: bus,
  13. }
  14. b.Receive()
  15. }

internal/modelA代码如下:

  1. package modelA
  2. import (
  3. "fmt"
  4. "github.com/asaskevich/EventBus"
  5. )
  6. type Bus struct {
  7. EventBus EventBus.Bus
  8. }
  9. type ModelAService interface {
  10. Run()
  11. Send()
  12. }
  13. func calculator(a int, b int) {
  14. fmt.Printf("ModelA %d\n", a+b)
  15. }
  16. func (bus *Bus) Receive() {
  17. err := bus.EventBus.Subscribe("testMessageFromB", calculator)
  18. if err != nil {
  19. fmt.Printf("Error Receiving message...")
  20. }
  21. }
  22. func (bus *Bus) Send() {
  23. bus.EventBus.Publish("testMessageFromA", 33, 33)
  24. }

internal/modelB代码如下:

  1. package modelB
  2. import (
  3. "fmt"
  4. "github.com/asaskevich/EventBus"
  5. )
  6. type Bus struct {
  7. EventBus EventBus.Bus
  8. }
  9. type ModelBService interface {
  10. Run()
  11. Send()
  12. }
  13. func calculator(a int, b int) {
  14. fmt.Printf("ModelB %d\n", a+b)
  15. }
  16. func (bus *Bus) Receive() {
  17. err := bus.EventBus.Subscribe("testMessageFromA", calculator)
  18. if err != nil {
  19. fmt.Printf("Error Receiving message...")
  20. }
  21. }
  22. func (bus *Bus) Send() {
  23. bus.EventBus.Publish("testMessageFromB", 33, 60)
  24. }
英文:

I am trying to learn GO and in doing so trying different concepts. Right now I am trying a PubSub approach, but within the application. I have an EventBus and I amd trying to pass the instance via Dependency Injection. However when I run the application nothing happens.

main

  1. package main
  2. import (
  3. "github.com/asaskevich/EventBus"
  4. modelA "interfaces/internal/modelA"
  5. modelB "interfaces/internal/modelB"
  6. )
  7. func main() {
  8. bus := EventBus.New()
  9. a := &modelA.Bus{EventBus: bus}
  10. a.Send()
  11. b := &modelB.Bus{
  12. EventBus: bus,
  13. }
  14. b.Receive()
  15. }

internal/modelA

  1. package modelA
  2. import (
  3. "fmt"
  4. "github.com/asaskevich/EventBus"
  5. )
  6. type Bus struct {
  7. EventBus EventBus.Bus
  8. }
  9. type ModelAService interface {
  10. Run()
  11. Send()
  12. }
  13. func calculator(a int, b int) {
  14. fmt.Printf("ModelA "+"%d\n", a+b)
  15. }
  16. func (bus *Bus) Receive() {
  17. err := bus.EventBus.Subscribe("testMessageFromB", calculator)
  18. if err != nil {
  19. fmt.Printf("Error Receiving message...")
  20. }
  21. }
  22. func (bus *Bus) Send() {
  23. bus.EventBus.Publish("testMessageFromA", 33, 33)
  24. }

internal/modelB

  1. package modelB
  2. import (
  3. "fmt"
  4. "github.com/asaskevich/EventBus"
  5. )
  6. type Bus struct {
  7. EventBus EventBus.Bus
  8. }
  9. type ModelBService interface {
  10. Run()
  11. Send()
  12. }
  13. func calculator(a int, b int) {
  14. fmt.Printf("ModelB "+"%d\n", a+b)
  15. }
  16. func (bus *Bus) Receive() {
  17. err := bus.EventBus.Subscribe("testMessageFromA", calculator)
  18. if err != nil {
  19. fmt.Printf("Error Receiving message...")
  20. }
  21. }
  22. func (bus *Bus) Send() {
  23. bus.EventBus.Publish("testMessageFromB", 33, 60)
  24. }

答案1

得分: 2

你需要首先订阅一个主题,然后发布(执行为主题定义的回调函数)。

可以尝试以下代码:

  1. func main() {
  2. bus := EventBus.New()
  3. a := &modelA.Bus{EventBus: bus}
  4. b := &modelB.Bus{EventBus: bus}
  5. b.Receive() // 订阅
  6. a.Send() // 发布
  7. // 取消订阅
  8. }

还可以参考示例

  1. func calculator(a int, b int) {
  2. fmt.Printf("%d\n", a + b)
  3. }
  4. func main() {
  5. bus := EventBus.New()
  6. bus.Subscribe("main:calculator", calculator)
  7. bus.Publish("main:calculator", 20, 40)
  8. bus.Unsubscribe("main:calculator", calculator)
  9. }

我的调试结构和输出如下:

全局事件总线(Global EventBus)在Go语言中的实现


注释:
为了清晰起见,你可以将b.Receive()重命名为b.Subscribe(),将a.Send()重命名为a.Publish()

另请参阅gRPC

全局事件总线(Global EventBus)在Go语言中的实现

英文:

You need to first Subscribe to a topic
then Publish (executes callback defined for a topic).

Try something like this:

  1. func main() {
  2. bus := EventBus.New()
  3. a := &modelA.Bus{EventBus: bus}
  4. b := &modelB.Bus{EventBus: bus}
  5. b.Receive() // Subscribe
  6. a.Send() // Publish
  7. // Unsubscribe
  8. }

Also see the example:

  1. func calculator(a int, b int) {
  2. fmt.Printf("%d\n", a + b)
  3. }
  4. func main() {
  5. bus := EventBus.New();
  6. bus.Subscribe("main:calculator", calculator);
  7. bus.Publish("main:calculator", 20, 40);
  8. bus.Unsubscribe("main:calculator", calculator);
  9. }

My debugging structure and output:
全局事件总线(Global EventBus)在Go语言中的实现


Footnotes:
You may rename b.Receive() to b.Subscribe()
and a.Send() to a.Publish() for clarity.

See also gRPC:

全局事件总线(Global EventBus)在Go语言中的实现

huangapple
  • 本文由 发表于 2022年4月24日 18:29:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/71987522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定