是否可以将ZMQ代理用作“切换”?

时间:2017-04-20 14:50:06

标签: go zeromq

我将实现以下图表: ZMQ-proxy

让所有“数据生成器”将数据发布到所有“数据接收器”。 数据接收者仍应具有设置子核心的能力。

// Proxy
package main

import zmq "github.com/pebbe/zmq4"

func main() {
    publisher, _ := zmq.NewSocket(zmq.XPUB)
    defer publisher.Close()
    publisher.Bind("tcp://*:8080")

    subscriber, _ := zmq.NewSocket(zmq.XSUB)
    defer subscriber.Close()
    subscriber.Bind("tcp://*:8081")

    zmq.Proxy(subscriber, publisher, nil)
}

和“数据生成器”:

// Data generator
package main

import (
    zmq "github.com/pebbe/zmq4"
    "time"
)

func main() {
    publisher, _ := zmq.NewSocket(zmq.PUB)
    defer publisher.Close()
    publisher.Connect("tcp://127.0.0.1:8081")

    for {
        publisher.Send("Some msg", 0)
        time.Sleep(time.Second)
    }
}

和“数据接收者”:

// Data receiver
package main

import (
    "fmt"
    zmq "github.com/pebbe/zmq4"
)

func main() {
    subscriber, _ := zmq.NewSocket(zmq.SUB)
    defer subscriber.Close()
    subscriber.Connect("tcp://127.0.0.1:8080")

    for {
        msg, _ := subscriber.Recv(0)
        fmt.Println(msg)
    }
}

在我看来它应该有效,但事实并非如此。我做错了什么?

1 个答案:

答案 0 :(得分:1)

要接收任何数据,订阅者套接字应使用某些过滤器指定SetSubscibe。见INFORMATION_SCHEMA.TABLES