1.概述

RocketMQ是alibaba公司开源的一个纯java的开源消息中间件。

2.开发测试环境搭建

到github上面rocketMQ,我选择的是alibaba-rocketmq-3.2.6.tar.gz,

rocketMQ在github上的网址:https://github.com/alibaba/RocketMQ/releases

下载之后解压到本地目录,我解压之后的目录结构是在:F:\work\alibaba-rocketmq

2.1 启动服务:

2.1.1 启动mqnamesrv

打开cmd,转到rocketmq的bin目录下:

cd F:\work\alibaba-rocketmq\bin

然后在bin目录下运行mqnamesrv.exe

mqnamesrv.exe

2.1.2 启动broker

在rocketmq的bin目录下执行下面命令:

mqbroker.exe -n "192.168.1.102:9876"

如果启动不成功,可以到当前用户目录下面查看报错日志,分析原因,如果没有对配置文件进行修改,日志默认是保存在当前用户目录下,如我的是保存在C:\Users\lenovo\logs\rocketmqlogs下面。

2.2 项目实例

1. 创建maven项目

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfoAAAC+CAIAAABSyVrQAAAgAElEQVR4nO2d/3sUx53n+0+427vdHLfPmmdD9lkefxnWICfrC9HjZ/fOPNwvwAj5kI3WrIz5YhIjaSIbDLbskeSQgNYSKNjrGFneWBPzRZE9km1sCELIgNApHomAJcMFUDD2gFCQwaAWIun7oWe6q6qrqnukGXXP9Hue18Ojmamurq6ueXX1p5v+KBrx6o3Hpo6mabNLdi6u3K1p2uLK3bNLdnJr1oula6UAAOBnNN7r4oWBr744M3r1go6SId3PLtm5bseH+h+apvW3bOQWy7Tu64sVpbjB9T0BAAAZxU3dL3phtz7H152uaVrfvg1Mm7JF963hgKIoSl5Fq2gVihKM0J9HChTiFQhH3R0KAIDcxk3d7z782eySnb/+ZEB3+sXO5z+Plv38pS2api16Ybe3dd8QpPXdGg4oeYGAEgh1Wwp3VwSUQCCPLB8N5SkKVbghKD5aAADA1Mmg7qM97c17m52vVfv6SIKk5bNM90pBkFdVazigFBeQ5fXC9bw6EVYCAGSITOk+2tMeeGDOXbNmigqUl+3Qhtv4aNreIwN6TP/Nj/tFK60vVpS8inomitJd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/u537L6L4mNH/osjp0Rf3w5Bcfnvxi6Io6dEW9cEUduqwOXRmr/vH8SemednTTEnMG3Vm22FAhISmJ7pOBFzKk07J+XuIte0s+deVWrHtJM7jNTkH3liZ1lt2bXNCie+oaL637ZA2JUxDiKNW0RFHuu3eeYKvp8tA9yDTQvZtsfX3bgVOHRN8yun+pbP75+Ni5uHo+PnY+8e/Y+fjYufjYufhYuGySuqfuC3y0znqLi6JQBpfq3nSodUHTy5yvZLN7bjNsmu1M92xrLTdiEj1GtZC+M4e445MM5hgHCbrnReWhe5BpoHs3OXymS/Ito/vK0h9sWp+/uTR/c2n+JuLfzevzN6/Pryz9gUT3IL1k4sFB0D3INNC9d8EDkL2Kg/9QljrQPcg00L13ge49SmaeFQHdg0wD3XsX6N5rGGH3TDzmE7oHmQa69y7Qva+A7kGmge69S7SnHfgK14ccyG2gewAA8AXQPQAA+ALoHgAAfAF0DwAAvgC6BwAAXwDdu0nZ/G+53gYAgE+A7qeVjvD9nc/dZ7CjcCb5tiN8v+stBADkKtD9tNL57D1nOxovnXjr0om3Pt27tbF0Gfm289l7pIs3BBUlGJlKA6ZeAwAgW4Hup0rn2U+cF+6ouPvSibfGvz4w0h8Z2P9qY+ky8m1Hxd3SxaF7AMDkge6nytZf1B483eGw8IGyuy+deGukPzLSHxn46DVd98bbA2VC3beGA2R600A4mf2qu8L8Iq+i1VykIZj8WPe7sAYAgD9wovv/DxZtOgk80mv4AAAAAElFTkSuQmCC" alt="" />

Pom.xml文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba.rocketmq</groupId>
<artifactId>rocketmqTest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>rocketmqTest Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>com.alibaba.rocketmq</groupId>
<artifactId>rocketmq-all</artifactId>
<version>3.2.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>rocketmqTest</finalName>
</build>
</project>

2. 编写消息产生者Producer

package com.pcitc.rocketmq.example;

import java.util.concurrent.TimeUnit;

import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message; public class Producer {
public static void main(String[] args) throws MQClientException,
InterruptedException {
/**
* 一个应用创建一个Producer,由应用来维护此对象,可以设置为全局对象或者单例<br>
* 注意:ProducerGroupName需要由应用来保证唯一<br>
* ProducerGroup这个概念发送普通的消息时,作用不大,但是发送分布式事务消息时,比较关键,
* 因为服务器会回查这个Group下的任意一个Producer
*/
DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
producer.setNamesrvAddr("192.168.1.102:9876");
producer.setInstanceName("Producer"); /**
* Producer对象在使用之前必须要调用start初始化,初始化一次即可<br>
* 注意:切记不可以在每次发送消息时,都调用start方法
*/
producer.start(); /**
* 下面这段代码表明一个Producer对象可以发送多个topic,多个tag的消息。
* 注意:send方法是同步调用,只要不抛异常就标识成功。但是发送成功也可会有多种状态,<br>
* 例如消息写入Master成功,但是Slave不成功,这种情况消息属于成功,但是对于个别应用如果对消息可靠性要求极高,<br>
* 需要对这种情况做处理。另外,消息可能会存在发送失败的情况,失败重试由应用来处理。
*/
for (int i = 0; i < 100; i++) {
try {
{
Message msg = new Message("TopicTest1",// topic
"TagA",// tag
"OrderID001",// key
("Hello MetaQ").getBytes());// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
} {
Message msg = new Message("TopicTest2",// topic
"TagB",// tag
"OrderID0034",// key
("Hello MetaQ").getBytes());// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
} {
Message msg = new Message("TopicTest3",// topic
"TagC",// tag
"OrderID061",// key
("Hello MetaQ").getBytes());// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
}
} catch (Exception e) {
e.printStackTrace();
}
TimeUnit.MILLISECONDS.sleep(1000);
} /**
* 应用退出时,要调用shutdown来清理资源,关闭网络连接,从MetaQ服务器上注销自己
* 注意:我们建议应用在JBOSS、Tomcat等容器的退出钩子里调用shutdown方法
*/
producer.shutdown();
}
}

3. 编写消息消费者Consumer

package com.pcitc.rocketmq.example;

import java.util.List;

import com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.common.message.MessageExt; public class PushConsumer { /**
* 当前例子是PushConsumer用法,使用方式给用户感觉是消息从RocketMQ服务器推到了应用客户端。<br>
* 但是实际PushConsumer内部是使用长轮询Pull方式从MetaQ服务器拉消息,然后再回调用户Listener方法<br>
*/
public static void main(String[] args) throws InterruptedException,
MQClientException {
/**
* 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例<br>
* 注意:ConsumerGroupName需要由应用来保证唯一
*/
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(
"ConsumerGroupName");
consumer.setNamesrvAddr("192.168.1.102:9876");
consumer.setInstanceName("Consumber"); /**
* 订阅指定topic下tags分别等于TagA或TagC或TagD
*/
consumer.subscribe("TopicTest1", "TagA || TagC || TagD");
/**
* 订阅指定topic下所有消息<br>
* 注意:一个consumer对象可以订阅多个topic
*/
consumer.subscribe("TopicTest2", "*"); consumer.registerMessageListener(new MessageListenerConcurrently() { /**
* 默认msgs里只有一条消息,可以通过设置consumeMessageBatchMaxSize参数来批量接收消息
*/
public ConsumeConcurrentlyStatus consumeMessage(
List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
System.out.println(Thread.currentThread().getName()
+ " Receive New Messages: " + msgs.size()); MessageExt msg = msgs.get(0);
if (msg.getTopic().equals("TopicTest1")) {
// 执行TopicTest1的消费逻辑
if (msg.getTags() != null && msg.getTags().equals("TagA")) {
// 执行TagA的消费
System.out.println(new String(msg.getBody()));
} else if (msg.getTags() != null
&& msg.getTags().equals("TagC")) {
// 执行TagC的消费
} else if (msg.getTags() != null
&& msg.getTags().equals("TagD")) {
// 执行TagD的消费
}
} else if (msg.getTopic().equals("TopicTest2")) {
System.out.println(new String(msg.getBody()));
} return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
} }); /**
* Consumer对象在使用之前必须要调用start初始化,初始化一次即可<br>
*/
consumer.start(); System.out.println("Consumer Started.");
}
}

先运行消息订阅者PushConsumer代码,然后运行消息生产者Producer代码,就会看到控制台打印刚刚发送的消息。

RocketMQ在windows上安装和开发使用的更多相关文章

  1. 在windows上安装scikit-learn开发环境

    操作系统:Windows 10 64位 1.安装python 前往https://www.python.org/downloads/下载对应操作系统的版本,笔者下载了32位的python 2.7.13 ...

  2. 【python安装】Windows上安装和创建python开发环境

    1. 在 windows10 上安装python开发环境 Linux和Mac OS都自带python环境,但是Windows没有,所以需要自行安装. 第1步:访问 python官网,下载Windows ...

  3. # 【ARM-Linux开发】在Win7的电脑上直接运行安装Ubuntu14.04发生的问题 标签(空格分隔): 【Linux开发】 --- > 一段时间以来,一直是在Windows上安装虚拟机

    [ARM-Linux开发]在Win7的电脑上直接运行安装Ubuntu14.04发生的问题 标签(空格分隔): [Linux开发] 一段时间以来,一直是在Windows上安装虚拟机,然后安装Ubuntu ...

  4. 在Windows上安装Elasticsearch 5.0

    在windows上安装Elasticsearch Elasticsearch可以使用.zip软件包安装在Windows上. elasticsearch-service.bat命令,它将设置Elasti ...

  5. 在windows上安装ASP.NET 5(译文)

    本文将介绍如何在windows上安装ASP.NET5,包括单独安装和通过Visual Studio 2015 安装. 本文包括: 通过Visual Studio安装ASP.NET 单独安装ASP.NE ...

  6. Redis简介以及如何在Windows上安装Redis

    Redis简介 Redis是一个速度非常快的非关系型内存数据库. Redis提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erla ...

  7. 如何在Windows上搭建Android开发环境

    Android开发越来越火,许多小伙伴们纷纷学习Android开发,学习Android开发首要任务是搭建Android开发环境,由于大家 主要实在Windows 上开发Android,下面就详细给大家 ...

  8. NodeJs在windows上安装配置测试

    Node.js简介简单的说 Node.js 就是运行在服务端的 JavaScript.Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.Node.js 使用了一个 ...

  9. Git学习系列之Windows上安装Git详细步骤(图文详解)

    前言 最初,Git是用于Linux下的内核代码管理.因为其非常好用,目前,已经被成功移植到Mac和Windows操作系统下. 鉴于大部分使用者使用的是Windows操作系统,故,这里详细讲解Windo ...

随机推荐

  1. linux 软连接方式实现上传文件存储目录的无缝迁移

    背景: 由于前期的磁盘空间规划与后期的业务要求不符合.原先/home被用于用户上传文件的存储目录,但是由于上传文件的逐渐增多,而原来的/home目录的空间不足,需要给/home目录进行扩容.同时各个应 ...

  2. CentOS 6.3 安装ATI显卡驱动

    环境: centos 6.3  内核(GNU貌似大家都知道) 显卡:ATI Mobility Radeon HD 3470 安装显卡流程: 1.官网下载最新驱动 http://support.amd. ...

  3. 企业级应用架构(三)三层架构之数据访问层的改进以及测试DOM的发布

    在上一篇我们在宏观概要上对DAL层进行了封装与抽象.我们的目的主要有两个:第一,解除BLL层对DAL层的依赖,这一点我们通过定义接口做到了:第二,使我们的DAL层能够支持一切数据访问技术,如Ado.n ...

  4. YII中引用自定义类

    如果通过actions方法引用其他自定义类时 <?php class LoginController extends Controller { public function actionInd ...

  5. javascript-01

    1.JavaScript:浏览器脚本语言 2.JavaScript的作用  |-1.进行前端验证  |-2.实现页面的动态效果 3.JavaScript的特点  |-1.和java没有任何关系,官方标 ...

  6. Android Activity的生命周期详解

    应用程序中,一个Activity通常就是一个单独的屏幕,它上面可以显示一些控件也可以监听并处理用户的事件做出响应. Activity之间通过Intent进行通信.在Intent 的描述结构中,有两个最 ...

  7. Find Minimum in Rotated Sorted Array问题的困惑

    今天做了两题,第二题没解出来,发现太麻烦了,放弃了……明天脑子清楚的时候再做. 第一题就是标题中的这个问题.在一个旋转排序数组中找出最小的值. 针对该问题出了两道不同要求的题目,分别是不考虑重复元素的 ...

  8. JDBC——架构层、驱动

    JDBC(java Datebase Connector) jdbc驱动程序 四种类型: jdbc-odbc桥接驱动程序 Native-API JDBC-Net Native-Protocol (常见 ...

  9. 时间处理总结(一).net

    不断整理中... 获取当前时间DateTime.Now.ToString("d") ;//获取到的日期格式为eg :2013/11/14 时间格式只取年月日DateTime.Now ...

  10. 安装"MySQLdb"一波三折.

    在慕课网学习课程"Python操作MySQL数据库",安装"MySQLdb"时遇到问题. 先是找错地方: 百度搜索"Mysql for Python& ...