1.下载并安装RocketMQ,本文测试版本:rocketmq-all-4.6.0-bin-release
2.下载并安装RocketMQ可视化工具(原地址:https://blog.csdn.net/so_geili/article/details/90142461)
2.1 git clone 源码,建议使用码云
2.2 修改配置文件
2.3 打包:mvn clean package -Dmaven.test.skip=true
2.4 运行jar:java -jar target/rocketmq-console-ng-1.0.0.jar
3.启动RocketMQ(原地址:https://blog.csdn.net/w8253497062015/article/details/94458893,第二步只要设置了自动创建topic,第三步一般不用动)
3.1 mqnamesrv.cmd -n localhost:9876
3.2 mqbroker.cmd -n localhost:9876 autoCreateTopicEnable=true &4.编写代码测试(原地址:https://guozh.net/rocketmqshiyongyizhiwindowdajianbushurocketmq/)
4.0 依赖
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.2.0</version>
</dependency>
4.1 编写消费者
public class Consumer {
public static void main(String[] args) throws MQClientException {
//这里填写group名字
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("my-group-name-A");
//NameServer地址
consumer.setNamesrvAddr("localhost:9876");
//1:topic名字 2:tag名字
consumer.subscribe("topic-name-A", "tag-name-A");
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(
List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
for (MessageExt msg : msgs) {
System.out.println(new String(msg.getBody()));
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.out.println("Consumer Started!");
}
}
4.2 编写生产者
public class Producer {
public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
DefaultMQProducer producer = new DefaultMQProducer("my-group-name-A");
producer.setNamesrvAddr("localhost:9876");
producer.start();
Message message = new Message("topic-name-A","tag-name-A","Message : My blog address guozh.net".getBytes());
producer.send(message);
System.out.println("Message sended");
producer.shutdown();
}
}
5.启动程序
5.1 启动消费者
5.2 启动生产者
6.测试成功