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

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

Global EventBus in GoLang

问题

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

主要代码如下:

package main

import (
	"github.com/asaskevich/EventBus"
	modelA "interfaces/internal/modelA"
	modelB "interfaces/internal/modelB"
)

func main() {
	bus := EventBus.New()

	a := &modelA.Bus{EventBus: bus}
	a.Send()

	b := &modelB.Bus{
		EventBus: bus,
	}
	b.Receive()

}

internal/modelA代码如下:

package modelA

import (
	"fmt"
	"github.com/asaskevich/EventBus"
)

type Bus struct {
	EventBus EventBus.Bus
}

type ModelAService interface {
	Run()
	Send()
}

func calculator(a int, b int) {
	fmt.Printf("ModelA %d\n", a+b)
}

func (bus *Bus) Receive() {
	err := bus.EventBus.Subscribe("testMessageFromB", calculator)
	if err != nil {
		fmt.Printf("Error Receiving message...")
	}
}

func (bus *Bus) Send() {
	bus.EventBus.Publish("testMessageFromA", 33, 33)
}

internal/modelB代码如下:

package modelB

import (
	"fmt"
	"github.com/asaskevich/EventBus"
)

type Bus struct {
	EventBus EventBus.Bus
}

type ModelBService interface {
	Run()
	Send()
}

func calculator(a int, b int) {
	fmt.Printf("ModelB %d\n", a+b)
}

func (bus *Bus) Receive() {
	err := bus.EventBus.Subscribe("testMessageFromA", calculator)
	if err != nil {
		fmt.Printf("Error Receiving message...")
	}
}

func (bus *Bus) Send() {
	bus.EventBus.Publish("testMessageFromB", 33, 60)
}
英文:

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

package main

import (
	"github.com/asaskevich/EventBus"
	modelA "interfaces/internal/modelA"
	modelB "interfaces/internal/modelB"
)

func main() {
	bus := EventBus.New()

	a := &modelA.Bus{EventBus: bus}
	a.Send()

	b := &modelB.Bus{
		EventBus: bus,
	}
	b.Receive()

}

internal/modelA

package modelA

import (
	"fmt"
	"github.com/asaskevich/EventBus"
)

type Bus struct {
	EventBus EventBus.Bus
}

type ModelAService interface {
	Run()
	Send()
}

func calculator(a int, b int) {
	fmt.Printf("ModelA "+"%d\n", a+b)
}

func (bus *Bus) Receive() {
	err := bus.EventBus.Subscribe("testMessageFromB", calculator)
	if err != nil {
		fmt.Printf("Error Receiving message...")
	}
}

func (bus *Bus) Send() {
	bus.EventBus.Publish("testMessageFromA", 33, 33)
}

internal/modelB

package modelB

import (
	"fmt"
	"github.com/asaskevich/EventBus"
)

type Bus struct {
	EventBus EventBus.Bus
}

type ModelBService interface {
	Run()
	Send()
}

func calculator(a int, b int) {
	fmt.Printf("ModelB "+"%d\n", a+b)
}

func (bus *Bus) Receive() {
	err := bus.EventBus.Subscribe("testMessageFromA", calculator)
	if err != nil {
		fmt.Printf("Error Receiving message...")
	}
}

func (bus *Bus) Send() {
	bus.EventBus.Publish("testMessageFromB", 33, 60)
}

答案1

得分: 2

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

可以尝试以下代码:

func main() {
    bus := EventBus.New()

    a := &modelA.Bus{EventBus: bus}
    b := &modelB.Bus{EventBus: bus}

    b.Receive() // 订阅
    a.Send()    // 发布
    // 取消订阅
}

还可以参考示例

func calculator(a int, b int) {
    fmt.Printf("%d\n", a + b)
}

func main() {
    bus := EventBus.New()
    bus.Subscribe("main:calculator", calculator)
    bus.Publish("main:calculator", 20, 40)
    bus.Unsubscribe("main:calculator", calculator)
}

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

全局事件总线(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:

func main() {
    bus := EventBus.New()

    a := &modelA.Bus{EventBus: bus}
    b := &modelB.Bus{EventBus: bus}

	b.Receive() // Subscribe
	a.Send()    // Publish
    // Unsubscribe
}

Also see the example:

func calculator(a int, b int) {
	fmt.Printf("%d\n", a + b)
}

func main() {
	bus := EventBus.New();
	bus.Subscribe("main:calculator", calculator);
	bus.Publish("main:calculator", 20, 40);
	bus.Unsubscribe("main:calculator", calculator);
}

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:

确定