【消息队列】windows安装NSQ

简介: 【消息队列】windows安装NSQ

nsq 是基于 Go语言开发出来的消息队列中间件,今天在windows上来安装一下基础环境。




一、下载可执行文件


点击下载

下载完成之后解压:


image.png


二、执行 nsqlookup


nsqlookupd是管理拓扑信息的守护进程。客户端查询nsqlookupd以发现特定主题的nsqd生产者,nsqd节点广播主题和通道信息。


H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>nsqlookupd
[nsqlookupd] 2022/12/21 18:02:36.729343 INFO: nsqlookupd v1.2.1 (built w/go1.16.6)
[nsqlookupd] 2022/12/21 18:02:36.739842 INFO: HTTP: listening on [::]:4161
[nsqlookupd] 2022/12/21 18:02:36.739842 INFO: TCP: listening on [::]:4160




三、执行 nsqd


nsqd 是接收消息、排队并将消息传递给客户机的守护进程。


H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>nsqd --lookupd-tcp-address=127.0.0.1:4160
[nsqd] 2022/12/21 18:06:30.371132 INFO: nsqd v1.2.1 (built w/go1.16.6)
[nsqd] 2022/12/21 18:06:30.380208 INFO: ID: 94
[nsqd] 2022/12/21 18:06:30.381257 INFO: NSQ: persisting topic/channel metadata to nsqd.dat
[nsqd] 2022/12/21 18:06:30.508955 INFO: TCP: listening on [::]:4150
[nsqd] 2022/12/21 18:06:30.508955 INFO: LOOKUP(127.0.0.1:4160): adding peer
[nsqd] 2022/12/21 18:06:30.508955 INFO: HTTP: listening on [::]:4151
[nsqd] 2022/12/21 18:06:30.510027 INFO: LOOKUP connecting to 127.0.0.1:4160
[nsqd] 2022/12/21 18:06:30.512820 INFO: LOOKUPD(127.0.0.1:4160): peer info {TCPPort:4160 HTTPPort:4161 Version:1.2.1 BroadcastAddress:FENG}



四、执行 nsqadmin


nsqadmin是一个Web UI,用于实时查看聚合的集群统计信息并执行各种管理任务。

H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>nsqadmin --lookupd-http-address=127.0.0.1:4161
[nsqadmin] 2022/12/21 18:07:45.818217 INFO: nsqadmin v1.2.1 (built w/go1.16.6)
[nsqadmin] 2022/12/21 18:07:45.827762 INFO: HTTP: listening on [::]:4171



五、测试


  1. 发布一个初始消息


H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>curl -d "hello world 1" "http://127.0.0.1:4151/pub?topic=test"
OK
H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>




六、启动 nsq_to_file


nsq_to_file 使用指定的主题/通道并写入以换行符分隔的文件,可选地滚动和/或压缩该文件。

H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>nsq_to_file --topic=test --output-dir=./tmp --lookupd-http-address=127.0.0.1:4161
2022/12/21 18:13:14 INF    1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test
2022/12/21 18:13:14 INF    1 [test/nsq_to_file] (FENG:4150) connecting to nsqd
[nsq_to_file] 2022/12/21 18:13:33.800381 INFO: [test/nsq_to_file] opening tmp/test.FENG.2022-12-21_18.log
[nsq_to_file] 2022/12/21 18:13:33.800381 INFO: [test/nsq_to_file] syncing 1 records to disk
2022/12/21 18:14:22 INF    1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test
2022/12/21 18:15:22 INF    1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test
2022/12/21 18:16:22 INF    1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test
2022/12/21 18:17:22 INF    1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test



七、推送更多消息

H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>curl -d "hello world 1" "http://127.0.0.1:4151/pub?topic=test"
OK
H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>curl -d "hello world 1" "http://127.0.0.1:4151/pub?topic=test"
OK
H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>curl -d "hello world 1" "http://127.0.0.1:4151/pub?topic=test"
OK
H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>curl -d "hello world 1" "http://127.0.0.1:4151/pub?topic=test"
OK
H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>curl -d "hello world 1" "http://127.0.0.1:4151/pub?topic=test"
OK
H:\我的开发环境\消息队列\nsq-1.2.1.windows-amd64.go1.16.6\bin>



八、通过界面查看消息统计


浏览器打开:http://127.0.0.1:4171/

image.png

  • 可以看到,刚刚发布了11条消息




九、Go 语言验证


  1. 消费者(Consumer)程序段


package main
import (
    "github.com/nsqio/go-nsq"
    "log"
    "os"
    "os/signal"
    "syscall"
)
type myMessageHandler struct{}
// HandleMessage implements the Handler interface
func (h *myMessageHandler) HandleMessage(m *nsq.Message) error {
    if len(m.Body) == 0 {
        // Returning nil will automatically send a FIN command to NSQ to mark the message as processed.
        // In the case, a message with an empty body is simply ignored/discarded.
        return nil
    }
    // do whatever actual message processing is desired.
    //err := processMessage(m.Body)
    err := func(message []byte) error {
        log.Println("接收到消息: " + string(message))
        return nil
    }(m.Body)
    // Returning a non-nil error will automatically send a REQ command to NSQ to re-queue the message.
    return err
}
func main() {
    // Instantiate a consumer that will subscribe to the provided channel.
    config := nsq.NewConfig()
    consumer, err := nsq.NewConsumer("topic", "channel", config)
    if err != nil {
        log.Fatal(err)
    }
    // Set the Handler for messages received by this Consumer. Can be called multiple times.
    // See also AddConcurrentHandlers.
    consumer.AddHandler(&myMessageHandler{})
    // Use nsqlookupd to discover nsqd instances.
    // See also ConnectToNSQD,ConnectToNSQDs, ConnectToNSQLookupds.
    err = consumer.ConnectToNSQLookupd("localhost:4161")
    if err != nil {
        log.Fatal(err)
    }
    // wait for signal to exit
    sigChan := make(chan os.Signal, 1)
    signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
    <-sigChan
    // Gracefully stop the consumer
    consumer.Stop()
}


生产者(Producer)程序段

package main
import (
    "github.com/nsqio/go-nsq"
    "log"
)
func main() {
    config := nsq.NewConfig()
    producer, err := nsq.NewProducer("127.0.0.1:4150", config)
    if err != nil {
        log.Fatal(err)
    }
    messageBody := []byte("你好呀,七镜")
    topicName := "topic"
    // Synchronously publish a single message to the specified topic.
    // Messages can also be sent asynchronously and/or in batches.
    err = producer.Publish(topicName, messageBody)
    if err != nil {
        log.Fatal(err)
    }
    // Gracefully stop the producer when appropriate (e.g. before shutting down the service)
    producer.Stop()
}


生产者程序段执行结果

image.png


消费者程序段执行结果

image.png


算是走了遍消息队列的入门程序,  后面还需要好好研究。





目录
相关文章
|
2天前
|
并行计算 TensorFlow 算法框架/工具
Windows10下CUDA9.0+CUDNN7.0.5的完美安装教程
该文介绍了如何在Windows 10上安装CUDA 9.0和cuDNN 7.0.5以支持Tensorflow-gpu 1.10.0。首先,解释了安装CUDA的原因,然后详细步骤包括:从NVIDIA官网下载CUDA 9.0,选择自定义安装并关闭不必要的组件,检查显卡驱动版本以决定是否安装Display Driver,最后确认安装成功。接着,下载cuDNN需要注册NVIDIA账户,解压后将文件复制到CUDA安装目录。整个过程旨在确保与Tensorflow-gpu 1.10.0的兼容性。
16 2
|
3天前
|
Windows
Windows 系统下安装
在Windows上安装Julia,从官网下载安装程序。32位版本兼容32/64位系统,但64位仅用于64位Windows。运行安装向导,简单点击Next,建议选中添加到PATH选项。完成后,Julia将可在终端使用,默认路径如C:\Users\BAIDU\AppData\Local\Programs\Julia 1.7.2。
|
4天前
|
并行计算 Windows
23.10.02更新 Windows下CUDA和CUDNN的安装和配置(图多详细)
23.10.02更新 Windows下CUDA和CUDNN的安装和配置(图多详细)
13 1
|
4天前
|
并行计算 Ubuntu TensorFlow
23.10.02更新 windows系统下的Tensorflow安装(图多详细)
23.10.02更新 windows系统下的Tensorflow安装(图多详细)
11 0
23.10.02更新 windows系统下的Tensorflow安装(图多详细)
|
5天前
|
安全 测试技术 数据库
达梦数据库Windows安装教程:从准备到完成
达梦数据库Windows安装教程:从准备到完成
|
5天前
|
安全 虚拟化 Windows
手把手教你如何在虚拟机上安装Windows 10
手把手教你如何在虚拟机上安装Windows 10
|
5天前
|
NoSQL Linux Redis
Redis的介绍,以及Redis的安装(本机windows版,虚拟机Linux版)和Redis常用命令的介绍
Redis的介绍,以及Redis的安装(本机windows版,虚拟机Linux版)和Redis常用命令的介绍
18 0
|
5天前
|
编解码 安全 关系型数据库
祝福CSDN的小伙伴2024年快乐!Windows7安装MySQL
祝福CSDN的小伙伴2024年快乐!Windows7安装MySQL
|
5天前
|
SQL Windows
保姆级:Windows Server 2012上安装.NET Framework 3.5
保姆级:Windows Server 2012上安装.NET Framework 3.5
|
9天前
|
Oracle Java 关系型数据库
windows 下 win11 JDK17安装与环境变量的配置(配置简单详细,包含IJ中java文件如何使用命令运行)
本文介绍了Windows 11中安装JDK 17的步骤,包括从官方网站下载JDK、配置环境变量以及验证安装是否成功。首先,下载JDK 17的安装文件,如果没有Oracle账户,可以直接解压缩文件到指定目录。接着,配置系统环境变量,新建`JAVA_HOME`变量指向JDK安装路径,并在`Path`变量中添加。然后,通过命令行(cmd)验证安装,分别输入`java -version`和`javac -version`检查版本信息。最后,作者分享了如何在任意位置运行Java代码,包括在IntelliJ IDEA(IJ)中创建的Java文件,只需去掉包声明,就可以通过命令行直接运行。