[Message] goalng subscribe messaging middleware deployment using NSQ

Copyright: https://blog.csdn.net/qq_21852449/article/details/88629106

nsq is golang written in a very mature support distributed high availability of messaging middleware. It is very good specific integrated much better integrated, in the case of three docker command to get a complete example, never used before docker, not much contact, but really with them before they would feel about such a headache configuration environment things can be omitted.
First, there are three components in the NSQ you need to know nsqd nsqlookupd nsqadmin

nsqd nsqd is a daemon nsq services, he lined up to receive and send messages to the client.

nsqlookupd this component from the literal meaning to know and view, nsqd a daemon to manage the entire messaging topology, the client queries nsqlookupd to find a specific topic nsqd producer.

nsqadmin This is a management console is a simple web ui, so that you can clearly see all kinds of information.

After that it should understand our thoughts became clear that if I want to use nsq then you need to start up and let it go nsqlookupd manage the entire network topology structure, at the same time we also need to create a nsqd to do things, and then start nsqadmin view task details, status.
First, make sure your computer successfully installed docker, and then load the image from the command, this part of the code is not particularity and machine, the environment does not matter.

docker pull nsqio/nsq
docker run --name lookupd -p 4160:4160 -p 4161:4161 nsqio/nsq /nsqlookupd

Next, re-open a command line, or let lookupd running in the background, and then run a nsqd container, this section requires you to fill in the host and port, host is your ip address, port service is listening.

docker pull nsqio/nsq
docker run --name nsqd -p 4150:4150 -p 4151:4151 \
    nsqio/nsq /nsqd \
    --broadcast-address=<host> \
    --lookupd-tcp-address=<host>:<port>

This step has been to set up a super simple messaging middleware, it contains all the features you need, and now we want to give him a ui user-friendly installation, remember to modify the host and port they really

docker run -d --name nsqadmin -p 4171:4171 nsqio/nsq /nsqadmin  --lookupd-http-address=<host>:<port>

Here Insert Picture Description
The above is installed, super simple.

Next we understand the message subscription transceiver mechanism, known to have a very vivid example of speaking on almost roughly know the meaning of the expression it may differ, saying that the first month of the amount of time Xiao Ming's sister Alice always follow her mother's instructions his book to his brother to let him see, and so watching his brother in him, so one month down, Xiao Ming saw two books, red to go check every day to see Xiao Ming did not read a book and then report to the mother, so small red flowers a lot of time on things that are not your own; second month straight red think I put the book on his brother got it on the shelf, and after she said yes each time his brother and brother read the book on the shelves will go and see my sister there did not give her the book, one month down, Xiao Ming was reading two books, but my sister all the time to be saved down to do something else, and this time there will be new problems, there is no way in time to the red mom Xiao Ming's progress report, but know that will eventually read, so this will give rise to a delay of the message that is to be expected but the result is there when the result is not known; the third Month, Xiao Ming to invite him to read and junior partner together, my sister told them that the first layer is to bookshelf Xiaoming, the second layer is a friend of Xiao Ming, Xiao Ming so each time to take the book that started for the first layer, Xiao Ming went to a friend take the second layer. This is the history of the progress of a message mechanism, the first month of messaging is equivalent to the code of our previous, sent a request to process the request and then return the second month advanced a little, sent a request to return directly, and has always been the I would say this place doing something but I had to wait time to do it again. The first three months of more advanced support for several people simultaneously.
In this way we can be extracted from some of the concepts, and the topic is the concept of the channel in nsq, a topic that is equivalent to the bookshelf example, correspond to the first channel layers on the shelves. By such a topic and channel will be able to uniquely identify a user a. Do not talk nonsense directly on the following code in the code, we can achieve a simple function of the chat room, to see the effect when we need to open three terminals, the first run the main function Xiaoming second run main function the red, then we run the third Publish function to send a message, remember to fill their host.

package main

// 消息收发机制的例子
import (
	"fmt"
	"sync"

	"github.com/nsqio/go-nsq"
)

//nsqd 节点地址和端口
var (
	//nsqd的地址,使用了tcp监听的端口
	tcpNsqdAddrr = "<host>:4150"
)

func main() {
	// 新建消费者   订阅all 主题  使用通道 c1
	// NewCustomer("小明", "all", "xiaoming")
	// NewCustomer("小红", "private", "xiaohong")

	// 新建生产者  生产一条问候消息  主题为all
	// Publish("private", "小红你好, 我是小A")

}

// Publish 发布函数   发布消息到指定的topic
// topic 主题
// content 内容
func Publish(topic, content string) {
	//初始化NSQ配置
	config := nsq.NewConfig()
	tPro, err := nsq.NewProducer(tcpNsqdAddrr, config)
	if err != nil {
		fmt.Println(err)
	}
	// 发布消息
	err = tPro.Publish(topic, []byte(content))
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("发送成功")
	}

}

// NsqHandler 声明一个结构体,实现HandleMessage接口方法
type NsqHandler struct {
	name string
	//标识ID
	// nsqHandlerID string
}

// HandleMessage 实现HandleMessage方法
// message是接收到的消息
func (s *NsqHandler) HandleMessage(message *nsq.Message) error {
	// 打印消息的一些基本信息
	fmt.Printf(s.name, "  收到了 ----->", string(message.Body))
	return nil
}

// NewCustomer 新建一个消费者
func NewCustomer(name, topic, channel string) {

	// 初始化配置
	config := nsq.NewConfig()
	// 创造消费者,参数一时订阅的主题,参数二是使用的通道
	com, err := nsq.NewConsumer(topic, channel, config)
	if err != nil {
		fmt.Println(err)
	}
	// 添加处理回调
	com.AddHandler(&NsqHandler{name: name})
	// 连接对应的nsqd
	err = com.ConnectToNSQD(tcpNsqdAddrr)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("消费者", name)
	}
	// 只是为了不结束此进程,这里没有意义
	var wg = &sync.WaitGroup{}
	wg.Add(1)
	wg.Wait()

}

Guess you like

Origin blog.csdn.net/qq_21852449/article/details/88629106