RocketMQ (二) RocketMQ 4.1.0 安装与入门实例

一、安装

环境:

Linux version 2.6.32-573.el6.x86_64
RocketMQ 4.1.0
java 1.8
maven 3.3.9 

步骤:

1.下载源代码并编译

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

在编译过程中需要下载很多的maven依赖包,建议还是更改自己自己的maven setting.xml 为国内镜像服务器地址。
oschina mirror url: http://maven.oschina.net/

这里我保存了自己已经编译完成的zip包,需要的同学可以直接下载,绿色版。
http://download.csdn.net/detail/chenfanglincfl/9821697

2.启动nameserver

在使用之前先简单了解一下这个到底是什么?

在分布式协作系统中,比较常用的是zookeeper,用来保证整个分布式系统的服务之间的选举,数据的发布和订阅,分布式锁的控制等。而rocketmq是没有接入zookeeper,而是通过自己内部的nameserver来维护活跃的broker列表,这样broker仅仅只维护数据的消息转发、存储以及master slave之间的同步。

进入到编译完成之后的target文件,会存在一个apache-rocketmq-all文件夹,这是编译完成之后的文件也是需要得到的最终文件。

  • a)配置ROCKETMQ_HOME
    在启动之前需要配置ROCKETMQ_HOME环境变量,如下
vim /etc/profile
export ROCKETMQ_HOME=/usr/local/rocketmq/incubator-rocketmq/target/apache-rocketmq-all
PATH=$JAVA_HOME/bin:$M2_HOME/bin:$ROCKETMQ_HOME/bin:$PATH
source /etc/profile

通过echo $ROCKETMQ_HOME ,可以查看ROCKETMQ_HOME 是否配置完成。

  • b)授权指定sh脚本文件权限
    进入$ROCKETMQ_HOME/bin 文件夹 执行
chmod +x mqadmin mqbroker mqfiltersrv mqshutdown  mqnamesrv
  • c)启动nameserver
nohup sh bin/mqnamesrv &

启动之后查看位于bin同级目录下的nohup.out文件

tailf nohup.out

这里写图片描述

如图表示启动成功

3.启动broker

  • a)配置NAMESRV_ADDR
 vim /etc/profile
export NAMESRV_ADDR=192.168.2.233:9876

保存

  • b)nohup mqbroker >/var/log/mq.log &

RocketMQ (二) RocketMQ 4.1.0 安装与入门实例_第1张图片

如上图表示broker启动成功

至此,rocketmq 安装完毕。

二、入门实例

producer

package test.mq;

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;

/**
 * @Author chenfanglin 【[email protected]】
 * @Date 2017/4/2118:00
 */
public class TestProducerRocketMQ {

    public static void main(String[] args) throws MQClientException{

        DefaultMQProducer producer = new DefaultMQProducer("producerGroupName");
        producer.setNamesrvAddr("192.168.2.235:9876");
        producer.start();
        try {
            {
                //topic:主题 testTopic.tags:主题下的tag tagA.keys:keyA.body:具体业务消息体
                Message msg = new Message("testTopic","tagA","keyA",("Hello World tagA!").getBytes());
                for(int i=100;i>0;i--){
                    if(i%2==0){
                        SendResult sendResult = producer.send(msg);
                        Thread.sleep(1000);
                        System.out.println("tagA send result:"+sendResult);
                    }else{
                        msg = new Message("testTopic","tagB","keyB",("Hello World tagB!").getBytes());
                        SendResult sendResult = producer.send(msg);
                        Thread.sleep(1000);
                        System.out.println("tagB send result:"+sendResult);
                    }
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        //注销producer
        Runtime.getRuntime().addShutdownHook(new Thread(() -> producer.shutdown()));
        System.exit(0);
    }
}

consumer

package test.mq;

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.consumer.ConsumeFromWhere;
import com.alibaba.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Author chenfanglin 【[email protected]】
 * @Date 2017/4/2118:04
 */
public class TestConsumerRocketMQ {
    public static void main(String[] args) throws InterruptedException, MQClientException {

        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("testProducerGroupName");
        consumer.setNamesrvAddr("192.168.2.235:9876");

        //订阅testTopic主题下,标签为tagA或者tagB的消息
        consumer.subscribe("testTopic", "tagA || tagB");
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List msgs,
                                                            ConsumeConcurrentlyContext context) {
                System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);
                MessageExt msg = msgs.get(0);
                if (msg.getTopic().equals("testTopic")) {
                    if (msg.getTags() != null && msg.getTags().equals("tagA")) {
                        // 执行tagA的消费
                        String message = new String(msg.getBody());
                        System.out.println("receive tagA message:"+message);
                    }
                    else if (msg.getTags() != null && msg.getTags().equals("tagB")) {
                        // 执行tagB的消费
                        String message = new String(msg.getBody());
                        System.out.println("receive tagB message:"+message);
                    }

                }
                //回执消息确认
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        System.out.println("Consumer Started.");
    }
}

资料:

NameServer的作用: http://blog.csdn.net/manzhizhen/article/details/52606733
官方文档:http://rocketmq.incubator.apache.org/docs/quick-start/
mq选型:http://www.cnblogs.com/mantu/p/6108645.html
问题解答:https://my.oschina.net/caiyunick/blog/806844
集群安装:http://blog.csdn.net/lovesomnus/article/details/51769977

你可能感兴趣的:(【RocketMQ】)