RocketMQ在Windows平台下环境搭建

时间:2024-05-04 12:06:37

一.  环境搭建

需要jdk1.6(以上) 64bit, maven, eclipse

二.  RocketMQ项目下载

项目地址:https://github.com/alibaba/RocketMQ,将下载的RocketMQ-master放到eclipse工作空间中

三. 将RocketMQ-master导入到eclipse中

  1. 将项目导入eclipse,如下图
RocketMQ在Windows平台下环境搭建


2. 在我下载的RocketMQ-master的pom.xml文件的parent有个问题,默认如下:
    <parent>
<groupId>com.taobao</groupId>
<artifactId>parent</artifactId>
<version>1.0.2</version>
</parent> <!-- <parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent> -->

但是编译时总报错parent找不到,而用下面的parent,则编译通过。


    <!--<parent>
<groupId>com.taobao</groupId>
<artifactId>parent</artifactId>
<version>1.0.2</version>
</parent>--> <parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>

3.  由于我用的是jdk1.7,故修改RocketMQ-master.pom的jdk版本


 <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--maven properties -->
<maven.test.skip>true</maven.test.skip>
<maven.jdoc.skip>true</maven.jdoc.skip>
<downloadSources>true</downloadSources>
<!-- compiler settings properties -->
<java_source_version>1.7</java_source_version>
<java_target_version>1.7</java_target_version>
<file_encoding>UTF-8</file_encoding>
</properties>

四.  编译RocketMQ项目

1.  在命令行执行在RocketMQ-master文件夹下的install.bat批处理

RocketMQ在Windows平台下环境搭建

2.  该命令会编译整个项目,并在RocketMQ-master目录下生成一个target文件夹

3.  进入刚生成的target文件夹下的bin目录,在命令行中执行:start mqnamesrv.exe,会弹出一个信息窗口,显示The
name Server boot success 说明启动成功了,接着启动borker,在命令行中执行:start mqbroker.exe -n 127.0.0.1:9876 同样的弹出一个窗口,看到success表示成功了。 
   RocketMQ在Windows平台下环境搭建

  RocketMQ在Windows平台下环境搭建

五.   启动Producer和Customer

        1.   在RocketMQ-example项目中加入Producer.java

public class Producer {
public static void main(String[] args) throws MQClientException,
InterruptedException{
/**
* 一个应用创建一个Producer,由应用来维护此对象,可以设置为全局对象或者单例<br>
* 注意:ProducerGroupName需要由应用来保证唯一<br>
* ProducerGroup这个概念发送普通的消息时,作用不大,但是发送分布式事务消息时,比较关键,
* 因为服务器会回查这个Group下的任意一个Producer
*/
final DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
producer.setNamesrvAddr("127.0.0.1:9876");
producer.setInstanceName("Producer"); /**
* Producer对象在使用之前必须要调用start初始化,初始化一次即可<br>
* 注意:切记不可以在每次发送消息时,都调用start方法
*/
producer.start(); /**
* 下面这段代码表明一个Producer对象可以发送多个topic,多个tag的消息。
* 注意:send方法是同步调用,只要不抛异常就标识成功。但是发送成功也可会有多种状态,<br>
* 例如消息写入Master成功,但是Slave不成功,这种情况消息属于成功,但是对于个别应用如果对消息可靠性要求极高,<br>
* 需要对这种情况做处理。另外,消息可能会存在发送失败的情况,失败重试由应用来处理。
*/
for (int i = 0; i < 10; i++){
try {
{
Message msg = new Message("TopicTest1",// topic
"TagA",// tag
"OrderID001",// key
("Hello MetaQA").getBytes());// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
} {
Message msg = new Message("TopicTest2",// topic
"TagB",// tag
"OrderID0034",// key
("Hello MetaQB").getBytes());// body
SendResult sendResult = producer.send(msg);
System.out.println(sendResult);
} {
Message msg = new Message("TopicTest3",// topic
"TagC",// tag
"OrderID061",// key
("Hello MetaQC").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();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
producer.shutdown();
}
}));
System.exit(0);
}
}

2.  加入Customer.java


public class Consumer {
/**
* 当前例子是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("127.0.0.1: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() { 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的消费
System.out.println(new String(msg.getBody()));
}else if (msg.getTags() != null
&&msg.getTags().equals("TagD")) {
//执行TagD的消费
System.out.println(new String(msg.getBody()));
}
}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("ConsumerStarted.");
}
}
3.  运行Producer

19:50:57.233 [main] DEBUG i.n.u.i.l.InternalLoggerFactory - Using SLF4J as the default logging framework
19:50:57.259 [main] DEBUG i.n.c.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 4
19:50:57.274 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
19:50:57.274 [main] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
19:50:57.274 [main] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
19:50:57.275 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: true
19:50:57.275 [main] DEBUG i.n.util.internal.PlatformDependent - Platform: Windows
19:50:57.276 [main] DEBUG i.n.util.internal.PlatformDependent - Java version: 7
19:50:57.276 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noUnsafe: false
19:50:57.276 [main] DEBUG i.n.util.internal.PlatformDependent - sun.misc.Unsafe: available
19:50:57.277 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noJavassist: false
19:50:57.506 [main] DEBUG i.n.util.internal.PlatformDependent - Javassist: available
19:50:57.507 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\tannj\AppData\Local\Temp (java.io.tmpdir)
19:50:57.507 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
19:50:57.507 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
19:50:57.544 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
19:50:57.544 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
19:50:57.875 [main] DEBUG i.n.util.internal.ThreadLocalRandom - -Dio.netty.initialSeedUniquifier: 0xfaf4f653b3d8be50 (took 52 ms)
19:50:57.927 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: unpooled
19:50:57.927 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 65536
19:50:57.958 [NettyClientSelector_1] DEBUG i.n.u.i.JavassistTypeParameterMatcherGenerator - Generated: io.netty.util.internal.__matchers__.com.alibaba.rocketmq.remoting.protocol.RemotingCommandMatcher
19:50:58.006 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacity.default: 262144
19:50:58.027 [NettyClientWorkerThread_1] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetectionLevel: simple
19:50:58.255 [NettyClientSelector_1] DEBUG io.netty.util.internal.Cleaner0 - java.nio.ByteBuffer.cleaner(): available
SendResult [sendStatus=SEND_OK, msgId=0A016F9600002A9F000000000272A36A, messageQueue=MessageQueue [topic=TopicTest, brokerName=tannj-PC, queueId=0], queueOffset=73578]
SendResult [sendStatus=SEND_OK, msgId=0A016F9600002A9F000000000272A3F2, messageQueue=MessageQueue [topic=TopicTest, brokerName=tannj-PC, queueId=1], queueOffset=73577]
SendResult [sendStatus=SEND_OK, msgId=0A016F9600002A9F000000000272A47A, messageQueue=MessageQueue [topic=TopicTest, brokerName=tannj-PC, queueId=2], queueOffset=73577]
SendResult [sendStatus=SEND_OK, msgId=0A016F9600002A9F000000000272A502, messageQueue=MessageQueue [topic=TopicTest, brokerName=tannj-PC, queueId=3], queueOffset=73576]
SendResult [sendStatus=SEND_OK, msgId=0A016F9600002A9F000000000272A58A, messageQueue=MessageQueue [topic=TopicTest, brokerName=tannj-PC, queueId=0], queueOffset=73579]
SendResult [sendStatus=SEND_OK, msgId=0A016F9600002A9F000000000272A612, messageQueue=MessageQueue [topic=TopicTest, brokerName=tannj-PC, queueId=1], queueOffset=73578]
SendResult [sendStatus=SEND_OK, msgId=0A016F9600002A9F000000000272A69A, messageQueue=MessageQueue [topic=TopicTest, brokerName=tannj-PC, queueId=2], queueOffset=73578]
SendResult [sendStatus=SEND_OK, msgId=0A016F9600002A9F000000000272A722, messageQueue=MessageQueue [topic=TopicTest, brokerName=tannj-PC, queueId=3], queueOffset=73577]
SendResult [sendStatus=SEND_OK, msgId=0A016F9600002A9F000000000272A7AA, messageQueue=MessageQueue [topic=TopicTest, brokerName=tannj-PC, queueId=0], queueOffset=73580]
SendResult [sendStatus=SEND_OK, msgId=0A016F9600002A9F000000000272A832, messageQueue=MessageQueue [topic=TopicTest, brokerName=tannj-PC, queueId=1], queueOffset=73579]
4.  运行Customer

19:51:49.059 [main] DEBUG i.n.u.i.l.InternalLoggerFactory - Using SLF4J as the default logging framework
19:51:49.069 [main] DEBUG i.n.c.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 4
19:51:49.083 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
19:51:49.084 [main] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
19:51:49.084 [main] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
19:51:49.085 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: true
19:51:49.085 [main] DEBUG i.n.util.internal.PlatformDependent - Platform: Windows
19:51:49.086 [main] DEBUG i.n.util.internal.PlatformDependent - Java version: 7
19:51:49.086 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noUnsafe: false
19:51:49.086 [main] DEBUG i.n.util.internal.PlatformDependent - sun.misc.Unsafe: available
19:51:49.086 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noJavassist: false
19:51:49.299 [main] DEBUG i.n.util.internal.PlatformDependent - Javassist: available
19:51:49.300 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\tannj\AppData\Local\Temp (java.io.tmpdir)
19:51:49.300 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
19:51:49.300 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
19:51:49.338 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
19:51:49.338 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
19:51:49.617 [main] DEBUG i.n.util.internal.ThreadLocalRandom - -Dio.netty.initialSeedUniquifier: 0x51d30e47b2203417 (took 19 ms)
19:51:49.685 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: unpooled
19:51:49.685 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 65536
19:51:49.728 [NettyClientSelector_1] DEBUG i.n.u.i.JavassistTypeParameterMatcherGenerator - Generated: io.netty.util.internal.__matchers__.com.alibaba.rocketmq.remoting.protocol.RemotingCommandMatcher
19:51:49.779 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacity.default: 262144
19:51:49.811 [NettyClientWorkerThread_1] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetectionLevel: simple
19:51:50.034 [NettyClientSelector_1] DEBUG io.netty.util.internal.Cleaner0 - java.nio.ByteBuffer.cleaner(): available
Consumer Started.
ConsumeMessageThread_2 Receive New Messages: [MessageExt [queueId=0, storeSize=136, queueOffset=73578, sysFlag=0, bornTimestamp=1421236258344, bornHost=/10.1.111.150:53985, storeTimestamp=1421236258374, storeHost=/10.1.111.150:10911, msgId=0A016F9600002A9F000000000272A36A, commitLogOffset=41067370, bodyCRC=613185359, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={TAGS=TagA, WAIT=true, MAX_OFFSET=73581, MIN_OFFSET=0}, body=16]]]
ConsumeMessageThread_1 Receive New Messages: [MessageExt [queueId=1, storeSize=136, queueOffset=73577, sysFlag=0, bornTimestamp=1421236258376, bornHost=/10.1.111.150:53985, storeTimestamp=1421236258377, storeHost=/10.1.111.150:10911, msgId=0A016F9600002A9F000000000272A3F2, commitLogOffset=41067506, bodyCRC=1401636825, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={TAGS=TagA, WAIT=true, MAX_OFFSET=73580, MIN_OFFSET=0}, body=16]]]
ConsumeMessageThread_4 Receive New Messages: [MessageExt [queueId=1, storeSize=136, queueOffset=73578, sysFlag=0, bornTimestamp=1421236258387, bornHost=/10.1.111.150:53985, storeTimestamp=1421236258387, storeHost=/10.1.111.150:10911, msgId=0A016F9600002A9F000000000272A612, commitLogOffset=41068050, bodyCRC=1424393152, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={TAGS=TagA, WAIT=true, MAX_OFFSET=73580, MIN_OFFSET=0}, body=16]]]
ConsumeMessageThread_7 Receive New Messages: [MessageExt [queueId=3, storeSize=136, queueOffset=73576, sysFlag=0, bornTimestamp=1421236258382, bornHost=/10.1.111.150:53985, storeTimestamp=1421236258383, storeHost=/10.1.111.150:10911, msgId=0A016F9600002A9F000000000272A502, commitLogOffset=41067778, bodyCRC=1032136437, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={TAGS=TagA, WAIT=true, MAX_OFFSET=73578, MIN_OFFSET=0}, body=16]]]
ConsumeMessageThread_9 Receive New Messages: [MessageExt [queueId=2, storeSize=136, queueOffset=73577, sysFlag=0, bornTimestamp=1421236258379, bornHost=/10.1.111.150:53985, storeTimestamp=1421236258381, storeHost=/10.1.111.150:10911, msgId=0A016F9600002A9F000000000272A47A, commitLogOffset=41067642, bodyCRC=1250039395, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={TAGS=TagA, WAIT=true, MAX_OFFSET=73579, MIN_OFFSET=0}, body=16]]]
ConsumeMessageThread_3 Receive New Messages: [MessageExt [queueId=0, storeSize=136, queueOffset=73579, sysFlag=0, bornTimestamp=1421236258384, bornHost=/10.1.111.150:53985, storeTimestamp=1421236258385, storeHost=/10.1.111.150:10911, msgId=0A016F9600002A9F000000000272A58A, commitLogOffset=41067914, bodyCRC=601994070, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={TAGS=TagA, WAIT=true, MAX_OFFSET=73581, MIN_OFFSET=0}, body=16]]]
ConsumeMessageThread_5 Receive New Messages: [MessageExt [queueId=0, storeSize=136, queueOffset=73580, sysFlag=0, bornTimestamp=1421236258394, bornHost=/10.1.111.150:53985, storeTimestamp=1421236258395, storeHost=/10.1.111.150:10911, msgId=0A016F9600002A9F000000000272A7AA, commitLogOffset=41068458, bodyCRC=710410109, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={TAGS=TagA, WAIT=true, MAX_OFFSET=73581, MIN_OFFSET=0}, body=16]]]
ConsumeMessageThread_8 Receive New Messages: [MessageExt [queueId=3, storeSize=136, queueOffset=73577, sysFlag=0, bornTimestamp=1421236258392, bornHost=/10.1.111.150:53985, storeTimestamp=1421236258393, storeHost=/10.1.111.150:10911, msgId=0A016F9600002A9F000000000272A722, commitLogOffset=41068322, bodyCRC=988340972, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={TAGS=TagA, WAIT=true, MAX_OFFSET=73578, MIN_OFFSET=0}, body=16]]]
ConsumeMessageThread_10 Receive New Messages: [MessageExt [queueId=2, storeSize=136, queueOffset=73578, sysFlag=0, bornTimestamp=1421236258390, bornHost=/10.1.111.150:53985, storeTimestamp=1421236258391, storeHost=/10.1.111.150:10911, msgId=0A016F9600002A9F000000000272A69A, commitLogOffset=41068186, bodyCRC=1307562618, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={TAGS=TagA, WAIT=true, MAX_OFFSET=73579, MIN_OFFSET=0}, body=16]]]
ConsumeMessageThread_6 Receive New Messages: [MessageExt [queueId=1, storeSize=136, queueOffset=73579, sysFlag=0, bornTimestamp=1421236258398, bornHost=/10.1.111.150:53985, storeTimestamp=1421236258397, storeHost=/10.1.111.150:10911, msgId=0A016F9600002A9F000000000272A832, commitLogOffset=41068594, bodyCRC=1565577195, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={TAGS=TagA, WAIT=true, MAX_OFFSET=73580, MIN_OFFSET=0}, body=16]]]

至此,已把RocketMQ在window平台下运行起来。