首先是 java 环境的配置
在centos里,首先下载jdk
配置相关信息如下:
export JAVA_HOME=/home/zhangyong/java/jdk1.7.0_67 export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar expor tPATH=$JAVA_HOME/bin:$PATH
然后配置rocketmq
我使用的版本是:
alibaba-rocketmq-3.2.6.tar
解压到如下的目录:
/home/zhangyong/server/alibaba-rocketmq
但是目前该目录没有执行权限
那么执行如下shell
chmod -R 777 /home/zhangyong/server/alibaba-rocketmq
好的。
开始启动相关的服务
首先启动的是名字服务
./mqnamesrv &
然后 需要启动broker
这里 我使用了最简单的方式
如下shell
#export NAMESRV_ADDR=Your_name_IP:9876 #export NAMESRV_ADDR=127.0.0.1:9876 # nohup mqbroker
那么我们通过程序来运行
我们使用如下包的内容:
在producer里设置nameserver
如下所示:
public class Producer { public static void main(String[] args) throws MQClientException, InterruptedException { DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name"); //在客户端使用需要导入到 环境变量里 // producer.setNamesrvAddr("192.168.3.109:9876"); producer.start(); for (int i = 0; i < 1000; i++) { try { Message msg = new Message("TopicTest",// topic "TagA",// tag ("Hello RocketMQ " + i).getBytes()// body ); SendResult sendResult = producer.send(msg); System.out.println(sendResult); } catch (Exception e) { e.printStackTrace(); Thread.sleep(1000); } } producer.shutdown(); } }
当执行后发生什么呢?
发现报如下的错:
com.alibaba.rocketmq.client.exception.MQClientException: No route info of this topic, TopicTest See https://github.com/alibaba/RocketMQ/issues/44 for further details. at com.alibaba.rocketmq.client.impl.producer.DefaultMQProducerImpl.sendDefaultImpl(DefaultMQProducerImpl.java:587) at com.alibaba.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1031) at com.alibaba.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1025) at com.alibaba.rocketmq.client.producer.DefaultMQProducer.send(DefaultMQProducer.java:95) at com.alibaba.rocketmq.example.quickstart.Producer.main(Producer.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
为什么呢? 我突然想到了 在linux centos 里可能是防火墙的问题
ok 想到这里,马上查看iptables 果然有防火墙拦住了
nameserver端口为9876
broker端口为10911
lokkit -p 9876:tcp -p 10911:tcp
然后重新运行producer, 我们发现打印如下的内容:
SendResult [sendStatus=SEND_OK, msgId=C0A8036D00002A9F0000000000062100, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost, queueId=1], queueOffset=728] SendResult [sendStatus=SEND_OK, msgId=C0A8036D00002A9F000000000006218A, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost, queueId=2], queueOffset=728] SendResult [sendStatus=SEND_OK, msgId=C0A8036D00002A9F0000000000062214, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost, queueId=3], queueOffset=728]
ok ,既然生产者已经发送数据了,那么我们来接收消息。
public class Consumer { public static void main(String[] args) throws InterruptedException, MQClientException { DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4"); consumer.setNamesrvAddr("192.168.3.109:9876"); /** * 设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费<br> * 如果非第一次启动,那么按照上次消费的位置继续消费 */ consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); consumer.subscribe("TopicTest", "*"); consumer.registerMessageListener(new MessageListenerConcurrently() { @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.println("Consumer Started."); } }
执行结果如下:
ConsumeMessageThread_12 Receive New Messages: [MessageExt [queueId=0, storeSize=138, queueOffset=728, sysFlag=0, bornTimestamp=1432364508153, bornHost=/192.168.3.105:59304, storeTimestamp=1432364487933, storeHost=/192.168.3.109:10911, msgId=C0A8036D00002A9F0000000000062076, commitLogOffset=401526, bodyCRC=917938826, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={TAGS=TagA, WAIT=true, MAX_OFFSET=729, MIN_OFFSET=0}, body=18]]]