[root@localhost ~]# docker pull nsqio/nsq
[root@localhost ~]# docker run -d --name lookupd -p 4160:4160 -p 4161:4161 nsqio/nsq /nsqlookupd
2fa8d6d17df28454d57dc52a3c7f207dc6feb1c4c873b473432a10b8ef241d54
[root@localhost ~]#
[root@localhost ~]# docker run -d --name nsqd -p 4150:4150 -p 4151:4151 nsqio/nsq /nsqd --broadcast-address=192.168.174.130 --lookupd-tcp-address=192.168.174.130:4160
73ee118badae2166a8e3165a1aa02555e4efbf0c5e0d797ba8b0e43eac77180d
[root@localhost ~]#
[root@localhost ~]# docker run -d --name nsqadmin -p 4171:4171 nsqio/nsq /nsqadmin --lookupd-http-address=192.168.174.130:4161
898e85a4b65ff171543bb600cd122173e3b6916047d9ab3dbf1d745ddb4d796f
#关闭防火墙
[root@localhost ~]# yum install iptables
[root@localhost ~]# iptables -I INPUT -p tcp --dport 4150:4151 -j ACCEPT
[root@localhost ~]# iptables -I INPUT -p tcp --dport 4160:4161 -j ACCEPT
[root@localhost ~]# iptables -I INPUT -p tcp --dport 4171 -j ACCEPT
永久关闭
[root@localhost ~]# vim /etc/sysconfig/iptables-config
文件中添加
-A INPUT -p tcp -m state --state NEW -m tcp --dport 4150:4151 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 4160:4161 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 4171 -j ACCEPT
多个节点部署:
在另外一台机器上
[root@localhost ~]# docker pull nsqio/nsq
[root@localhost ~]# docker run --name nsqd -p 4150:4150 -p 4151:4151 nsqio/nsq /nsqd --broadcast-address=192.168.174.131 --lookupd-tcp-address=192.168.174.130:4160
往节点发送主题消息
package main
import (
"github.com/nsqio/go-nsq"
"log"
)
func main() {
config := nsq.NewConfig()
//nsqProducer, _ := nsq.NewProducer("192.168.174.130:4150", config)
nsqProducer, _ := nsq.NewProducer("192.168.174.131:4150", config)
err := nsqProducer.Ping()
if err != nil {
log.Fatal("ping error")
return
}
defer nsqProducer.Stop()
topicName := "publish_test"
count := 4
for i := 0; i < count; i++ {
//err = nsqProducer.DeferredPublish(topicName, time.Second * 10, []byte("hello world"))
err = nsqProducer.Publish(topicName, []byte("publish test"))
if err != nil {
log.Fatal("error")
}
}
}
package main
import (
"github.com/nsqio/go-nsq"
"log"
)
func main() {
config := nsq.NewConfig()
nsqProducer, _ := nsq.NewProducer("192.168.174.130:4150", config)
//nsqProducer, _ := nsq.NewProducer("192.168.174.131:4150", config)
err := nsqProducer.Ping()
if err != nil {
log.Fatal("ping error")
return
}
defer nsqProducer.Stop()
topicName := "publish_test_2"
count := 4
for i := 0; i < count; i++ {
err = nsqProducer.Publish(topicName, []byte("publish test 2"))
if err != nil {
log.Fatal("error")
}
}
}
不过现在还都没有接收的channel
package main
import (
"fmt"
"github.com/nsqio/go-nsq"
"os"
"os/signal"
"strconv"
"sync"
"time"
)
type ConsumerHandle struct {
consumer *nsq.Consumer
msgGood int
}
var consumers = make([]*nsq.Consumer, 0)
var mux = &sync.Mutex{}
func main() {
topicName := "publish_test"
count := 5
for i := 0; i < count; i++ {
time.Sleep(time.Second)
go readMessage(topicName, i)
}
cleanup := make(chan os.Signal)
signal.Notify(cleanup, os.Interrupt)
fmt.Println("server is running....")
exit := make(chan bool)
go func() {
select {
case <-cleanup:
fmt.Println("Receive an interrupt signal, stoping service ...")
for _, ele := range consumers {
ele.StopChan <- 1
ele.Stop()
}
exit <- true
}
}()
<-exit
fmt.Println("stop server....")
}
func (h *ConsumerHandle) HandleMessage(message *nsq.Message) error {
msg := string(message.Body) + " " + strconv.Itoa(h.msgGood)
fmt.Println(msg)
return nil
}
func readMessage(topicName string, count int) {
defer func() {
if err := recover(); err != nil {
fmt.Println("error: ", err)
}
}()
config := nsq.NewConfig()
config.MaxInFlight = 10000
config.MaxBackoffDuration = time.Minute
consumer, _ := nsq.NewConsumer(topicName, "ch"+strconv.Itoa(count), config)
consumerHandle := &ConsumerHandle{consumer: consumer, msgGood: count}
consumer.AddHandler(consumerHandle)
err := consumer.ConnectToNSQLookupd("192.168.174.130:4161")
//err := consumer.ConnectToNSQDs([]string{"192.168.174.130:4161"})
//err := consumer.ConnectToNSQD("192.168.174.130:4150")
//err := consumer.ConnectToNSQD("192.168.174.130:4151")
if err != nil {
fmt.Println("ConnectToNSQLookupd error")
}
mux.Lock()
consumers = append(consumers, consumer)
mux.Unlock()
<-consumer.StopChan
fmt.Println("stop....")
}
运行程序
2018/07/04 09:51:47 INF 1 [publish_test/ch0] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 09:51:47 INF 1 [publish_test/ch0] (192.168.174.131:4150) connecting to nsqd
publish test 0
publish test 0
publish test 0
publish test 0
2018/07/04 09:51:48 INF 2 [publish_test/ch1] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 09:51:48 INF 2 [publish_test/ch1] (192.168.174.131:4150) connecting to nsqd
2018/07/04 09:51:49 INF 3 [publish_test/ch2] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 09:51:49 INF 3 [publish_test/ch2] (192.168.174.131:4150) connecting to nsqd
2018/07/04 09:51:50 INF 4 [publish_test/ch3] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 09:51:50 INF 4 [publish_test/ch3] (192.168.174.131:4150) connecting to nsqd
server is running....
2018/07/04 09:51:51 INF 5 [publish_test/ch4] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 09:51:51 INF 5 [publish_test/ch4] (192.168.174.131:4150) connecting to nsqd
2018/07/04 09:52:53 INF 1 [publish_test/ch0] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 09:52:54 INF 3 [publish_test/ch2] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 09:52:57 INF 2 [publish_test/ch1] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
主题出现了接收的channel
上面4个channel已经就绪了,再发消息,4个channel都能接收到,我们重新发消息
2018/07/04 10:12:53 INF 1 [publish_test/ch0] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 10:12:53 INF 1 [publish_test/ch0] (192.168.174.131:4150) connecting to nsqd
2018/07/04 10:12:53 WRN 1 [publish_test/ch0] (192.168.174.131:4150) max RDY count 2500 < consumer max in flight 10000, truncation possible
2018/07/04 10:12:53 INF 1 [publish_test/ch0] (192.168.174.130:4150) connecting to nsqd
2018/07/04 10:12:53 WRN 1 [publish_test/ch0] (192.168.174.130:4150) max RDY count 2500 < consumer max in flight 10000, truncation possible
2018/07/04 10:12:54 INF 2 [publish_test/ch1] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 10:12:54 INF 2 [publish_test/ch1] (192.168.174.131:4150) connecting to nsqd
2018/07/04 10:12:54 WRN 2 [publish_test/ch1] (192.168.174.131:4150) max RDY count 2500 < consumer max in flight 10000, truncation possible
2018/07/04 10:12:54 INF 2 [publish_test/ch1] (192.168.174.130:4150) connecting to nsqd
2018/07/04 10:12:54 WRN 2 [publish_test/ch1] (192.168.174.130:4150) max RDY count 2500 < consumer max in flight 10000, truncation possible
2018/07/04 10:12:55 INF 3 [publish_test/ch2] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 10:12:55 INF 3 [publish_test/ch2] (192.168.174.131:4150) connecting to nsqd
2018/07/04 10:12:55 WRN 3 [publish_test/ch2] (192.168.174.131:4150) max RDY count 2500 < consumer max in flight 10000, truncation possible
2018/07/04 10:12:55 INF 3 [publish_test/ch2] (192.168.174.130:4150) connecting to nsqd
2018/07/04 10:12:55 WRN 3 [publish_test/ch2] (192.168.174.130:4150) max RDY count 2500 < consumer max in flight 10000, truncation possible
2018/07/04 10:12:56 INF 4 [publish_test/ch3] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 10:12:56 INF 4 [publish_test/ch3] (192.168.174.131:4150) connecting to nsqd
2018/07/04 10:12:56 WRN 4 [publish_test/ch3] (192.168.174.131:4150) max RDY count 2500 < consumer max in flight 10000, truncation possible
2018/07/04 10:12:56 INF 4 [publish_test/ch3] (192.168.174.130:4150) connecting to nsqd
2018/07/04 10:12:56 WRN 4 [publish_test/ch3] (192.168.174.130:4150) max RDY count 2500 < consumer max in flight 10000, truncation possible
server is running....
2018/07/04 10:12:57 INF 5 [publish_test/ch4] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 10:12:57 INF 5 [publish_test/ch4] (192.168.174.131:4150) connecting to nsqd
2018/07/04 10:12:57 WRN 5 [publish_test/ch4] (192.168.174.131:4150) max RDY count 2500 < consumer max in flight 10000, truncation possible
2018/07/04 10:12:57 INF 5 [publish_test/ch4] (192.168.174.130:4150) connecting to nsqd
2018/07/04 10:12:57 WRN 5 [publish_test/ch4] (192.168.174.130:4150) max RDY count 2500 < consumer max in flight 10000, truncation possible
publish test 1
publish test 2
publish test 3
publish test 4
publish test 0
publish test 0
publish test 0
publish test 0
publish test 1
publish test 1
publish test 1
publish test 2
publish test 2
publish test 2
publish test 3
publish test 3
publish test 3
publish test 4
publish test 4
publish test 4
2018/07/04 10:13:56 INF 3 [publish_test/ch2] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 10:14:05 INF 1 [publish_test/ch0] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 10:14:05 INF 4 [publish_test/ch3] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 10:14:06 INF 2 [publish_test/ch1] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
2018/07/04 10:14:08 INF 5 [publish_test/ch4] querying nsqlookupd http://192.168.174.130:4161/lookup?topic=publish_test
Receive an interrupt signal, stoping service ...
stop....
stop....
stop....
stop....
stop....
stop server....
2018/07/04 10:14:09 INF 1 [publish_test/ch0] stopping...
2018/07/04 10:14:09 INF 2 [publish_test/ch1] stopping...
2018/07/04 10:14:09 INF 3 [publish_test/ch2] stopping...
2018/07/04 10:14:09 INF 4 [publish_test/ch3] stopping...
2018/07/04 10:14:09 INF 5 [publish_test/ch4] stopping...
Process finished with exit code 0