Centos7 Apache-RocketMq4.+搭建及rocketmq demo

版权声明:人工智能大佬群号:467123855 。 www.6aiq.com AIQ-机器学习大数据技术社区 全国最专业的机器学习大数据技术社区。 https://blog.csdn.net/lsgqjh/article/details/69808665

阿里云官方已经提供了云消息队列服务
官方文档地址:https://help.aliyun.com/product/29530.html?spm=5176.doc44712.3.1.DvSXmn

Apache的quick start 链接:
http://rocketmq.apache.org/docs/quick-start/

完整代码见Github:

https://github.com/cbamls/Apache-RocketMq-Demo
更多关于阿里云产品的文档 请加入本博客推荐的QQ群

环境要求:

64bit OS, Linux/Unix/Mac is recommended;
64bit JDK 1.7+;
Maven 3.2.x
Git

服务端安装

jdk自行安装
安装maven:

cd /usr/local/src
wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bin.tar.gz

配置环境变量, JAVA_HOME 因人而异啊。

# jdk7 settings
JAVA_HOME=/opt/jdk1.7.0_79
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH
export M2_HOME=/usr/local/src/apache-maven-3.5.0
export PATH=$ZOOKEEPER_HOME/bin:$M2_HOME/bin:$PATH
export PATH

检测maven:


[root@localhost ~]# mvn -v
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: /usr/local/src/apache-maven-3.5.0
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: /opt/jdk1.7.0_79/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-642.4.2.el6.x86_64", arch: "amd64", family: "unix"
[root@localhost ~]# 

clone Apache-RocketMq源码并maven build:

> git clone https://github.com/apache/incubator-rocketmq.git
> cd incubator-rocketmq
> mvn clean package install -Prelease-all assembly:assembly -U
> cd target/apache-rocketmq-all/

完成 build的结果:

To refactor, move this assembly into a child project and use the flag <useAllReactorProjects>true</useAllReactorProjects> in each moduleSet.
[INFO] Building tar: /usr/local/src/incubator-rocketmq/target/apache-rocketmq-all.tar.gz
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] Apache RocketMQ 4.1.0-incubating-SNAPSHOT .......... SUCCESS [ 17.218 s]
[INFO] rocketmq-remoting 4.1.0-incubating-SNAPSHOT ........ SUCCESS [ 26.766 s]
[INFO] rocketmq-common 4.1.0-incubating-SNAPSHOT .......... SUCCESS [  9.472 s]
[INFO] rocketmq-client 4.1.0-incubating-SNAPSHOT .......... SUCCESS [ 16.182 s]
[INFO] rocketmq-store 4.1.0-incubating-SNAPSHOT ........... SUCCESS [ 16.367 s]
[INFO] rocketmq-srvutil 4.1.0-incubating-SNAPSHOT ......... SUCCESS [  0.977 s]
[INFO] rocketmq-broker 4.1.0-incubating-SNAPSHOT .......... SUCCESS [ 28.364 s]
[INFO] rocketmq-tools 4.1.0-incubating-SNAPSHOT ........... SUCCESS [  4.477 s]
[INFO] rocketmq-namesrv 4.1.0-incubating-SNAPSHOT ......... SUCCESS [ 32.307 s]
[INFO] rocketmq-example 4.1.0-incubating-SNAPSHOT ......... SUCCESS [  1.684 s]
[INFO] rocketmq-filtersrv 4.1.0-incubating-SNAPSHOT ....... SUCCESS [  1.585 s]
[INFO] rocketmq-test 4.1.0-incubating-SNAPSHOT ............ SUCCESS [  7.249 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 06:41 min
[INFO] Finished at: 2017-04-09T10:47:18+08:00
[INFO] Final Memory: 54M/653M
[INFO] ------------------------------------------------------------------------

启动namesrv和broker

nohup sh mqnamesrv &
nohup sh mqbroker -n localhost:9876 &

关闭服务

> sh bin/mqshutdown broker
The mqbroker(36695) is running...
Send shutdown request to mqbroker(36695) OK

> sh bin/mqshutdown namesrv
The mqnamesrv(36664) is running...
Send shutdown request to mqnamesrv(36664) OK

Provider 和Consumer代码:

package cn.m1c.rocket;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.util.concurrent.TimeUnit;


public class Producer {
   public static void main(String[] args) throws MQClientException,
           InterruptedException, MQClientException {
      /**
       * 一个应用创建一个Producer,由应用来维护此对象,可以设置为全局对象或者单例<br>
       * 注意:ProducerGroupName需要由应用来保证唯一<br>
       * ProducerGroup这个概念发送普通的消息时,作用不大,但是发送分布式事务消息时,比较关键,
       * 因为服务器会回查这个Group下的任意一个Producer
       */
      final DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
      producer.setNamesrvAddr("222.27.196.8:9876");
      producer.setInstanceName("Producer");
      /**
       * Producer对象在使用之前必须要调用start初始化,初始化一次即可<br>
       * 注意:切记不可以在每次发送消息时,都调用start方法
       */
      producer.start();

      /**
       * 下面这段代码表明一个Producer对象可以发送多个topic,多个tag的消息。
       * 注意:send方法是同步调用,只要不抛异常就标识成功。但是发送成功也可会有多种状态,<br>
       * 例如消息写入Master成功,但是Slave不成功,这种情况消息属于成功,但是对于个别应用如果对消息可靠性要求极高,<br>
       * 需要对这种情况做处理。另外,消息可能会存在发送失败的情况,失败重试由应用来处理。
       */
      for(int m =0;m<1;m++){
          for (int i = 0; i < 100000; i++){
                 try {
                    {
                        Message msg = new Message("TopicTest2",// topic
                              "push",// tag
                              "OrderID00"+i,// key
                              ("Hello MetaQ"+i).getBytes());// body
                        SendResult sendResult = producer.send(msg);
                        System.out.print(i);
                        System.out.print(sendResult);
                        System.out.print(",");
                        System.out.print(StringUtils.bytesToString(msg.getBody()));
                        System.out.println(",");
                    }
//                  {
//                      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);
//                  }
                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                 }catch(Exception e) {
                    e.printStackTrace();
                 }
//               TimeUnit.MILLISECONDS.sleep(10);
              }
      }
      /**
       * 应用退出时,要调用shutdown来清理资源,关闭网络连接,从MetaQ服务器上注销自己
       * 注意:我们建议应用在JBOSS、Tomcat等容器的退出钩子里调用shutdown方法
       */
//    producer.shutdown();
      Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
         public void run() {
            producer.shutdown();
         }
      }));
      System.exit(0);
   }
}

消费端:

package cn.m1c.rocket;


import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

public class PushConsumer {
         /**
          * 当前例子是PushConsumer用法,使用方式给用户感觉是消息从RocketMQ服务器推到了应用客户端。<br>
          * 但是实际PushConsumer内部是使用长轮询Pull方式从MetaQ服务器拉消息,然后再回调用户Listener方法<br>
          */
         public static void main(String[] args) throws InterruptedException, MQClientException, MQClientException {
                   /**
                    * 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例<br>
                    * 注意:ConsumerGroupName需要由应用来保证唯一
                    */
                   DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(
                                     "ConsumerGroupName");
                   consumer.setNamesrvAddr("222.27.196.8:9876");
                   consumer.setInstanceName("Consumber");
                   consumer.setMessageModel(MessageModel.CLUSTERING);
//                   consumer.setConsumeThreadMin(1);
//                   consumer.setConsumeThreadMax(1);

                   /**
                    * 订阅指定topic下tags分别等于TagA或TagC或TagD
                    */
                   consumer.subscribe("TopicTest1","TagA || TagC || TagD");
                   /**
                    * 订阅指定topic下所有消息<br>
                    * 注意:一个consumer对象可以订阅多个tag
                    */
                   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(StringUtils.bytesToString(msg.getBody()));
                                               }else if (msg.getTags() != null
                                                                 &&msg.getTags().equals("TagC")) {
                                                        //执行TagC的消费
                                                        System.out.println(StringUtils.bytesToString(msg.getBody()));
                                               }else if (msg.getTags() != null
                                                                 &&msg.getTags().equals("TagD")) {
                                                        //执行TagD的消费
                                                        System.out.println(StringUtils.bytesToString(msg.getBody()));
                                               }
                                     }else if (msg.getTopic().equals("TopicTest2")) {
                                               System.out.println(StringUtils.bytesToString(msg.getBody()));
                                     }

                                     return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

                            }
                   });

                   /**
                    * Consumer对象在使用之前必须要调用start初始化,初始化一次即可<br>
                    */
                   consumer.start();

                   System.out.println("ConsumerStarted.");
         }
}
展开阅读全文

没有更多推荐了,返回首页