我将它用作样板文件,除了在同一个程序中我还有一些 goroutines 作为工作程序并连接到后端端点 tcp://127.0.0.1:5560。
我想做的是让它通过更有效的方式连接,例如 ipc://、inproc://,甚至是 unix 套接字。我已经尝试过这些,但没有奏效。ZeroMQ 不能使用 Channels 吗?
那么如何在没有 tcp 的情况下将不同的 goroutine 与 ZeroMQ 上下文连接起来呢?有更好的选择吗?
更新:代码:
// Simple message queuing broker
// Same as request-reply broker but using QUEUE device
//
// Author: Brendan Mc.
// Requires: http://github.com/alecthomas/gozmq
package main
import (
zmq "github.com/alecthomas/gozmq"
)
func startWorker() {
context, _ := zmq.NewContext()
defer context.Close()
worker, _ := context.NewSocket(zmq.REP)
//err := worker.Connect("ipc:///backend") // Tried it, but nothing
//err := worker.Connect("inproc:///backend") // Tried it, but nothing
err := worker.Connect("tcp://127.0.0.1:5560") // this works
if err != nil {
fmt.Println(err)
}
for {
data, err := worker.Recv(0)
fmt.Println(string(data))
worker.Send([]byte("I got your data"), 0)
}
}
func main() {
context, _ := zmq.NewContext()
defer context.Close()
// Socket facing clients
frontend, _ := context.NewSocket(zmq.ROUTER)
defer frontend.Close()
frontend.Bind("tcp://*:5559")
// Socket facing services
backend, _ := context.NewSocket(zmq.DEALER)
defer backend.Close()
//backend.Bind("ipc:///backend") // Tried it, but nothing
//backend.Bind("inproc:///backend") // Tried it, but nothing
backend.Bind("tcp://*:5560") // this works
for i := 0; i < 4; i++ {
go startWorker() // Start workers in a separate goroutine
}
// Start built-in device
zmq.Device(zmq.QUEUE, frontend, backend)
// We never get here…
}