我将其用作样板,除了在同一程序中还有一些goroutines是工作程序之外,它们还连接到后端端点tcp://127.0.0.1:5560。
我想做的是让它通过更有效的方式连接,例如ipc://,inproc://或unix套接字。我已经尝试过了,但是没有用。带有ZeroMQ的通道是不行的吗?
那么,如何在不使用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…
}
阿波罗的战车
相关分类