nsq

mac本地使用nsq

Posted by JYH on June 17, 2019

配置

macOS版本 10.14.5

nsq

参考 https://blog.csdn.net/chinawangfei/article/details/80341688

操作

执行 brew install nsq

1.第一个shell中 $ nsqlookupd

2.第二个shell中,启动nsqd $ nsqd –lookupd-tcp-address=127.0.0.1:4160

3.第三个shell中,启动nsqadmin $ nsqadmin –lookupd-http-address=127.0.0.1:4161

4.第四个shell中,发布第一个消息(同时创建topic) $ curl -d ‘hello world 1’ ‘http://127.0.0.1:4151/pub?topic=test’

5.第五个shell中,使用nsq_to_file启动一个client来接收消息 $ nsq_to_file –topic=test –output-dir=/tmp –lookupd-http-address=127.0.0.1:4161

查看

1.在浏览器中输入网址http://127.0.0.1:4171,打开nsqadmin的UI界面,查看统计数据

2.查看*.log 的文件内容

JAVA代码

生产NSQ

package com.jyh.jiangboot.command;

import com.github.brainlag.nsq.NSQProducer; import com.github.brainlag.nsq.exceptions.NSQException; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;

import java.util.concurrent.TimeoutException;

@Component public class NsqProduce {

@Value("${NSQ_ADDRESS}")
private String address;

@Value("${NSQ_DAEMON_PORT}")
private Integer nsqDaemonPort;

@Value("${NSQ_TOPIC}")
private String nsqTopic;

private NSQProducer getProducer() {
    NSQProducer producer = new NSQProducer();
    producer.addAddress(address, nsqDaemonPort).start();
    return producer;
}

private void produce(String msg) throws NSQException, TimeoutException {
    this.getProducer().produce(nsqTopic, msg.getBytes());
}

}