RocketMQ

RocketMQ 消息队列部署

部署方式:

一、安装tar包(未安装git)

第一步:先安装jdk, maven等工具

vim /etc/profile 添加

复制代码
JAVA_HOME=/usr/java/jdk1.7.0_79
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
M2_HOME=/usr/local/apache-maven-3.3.9
PATH=$M2_HOME/bin:$JAVA_HOME/bin:$PATH
export JAVA_HOME
export CLASSPATH
export M2_HOME
export PATH
复制代码

 

设置生效:

source /etc/profile

 

测试: mvn -v

 

第二步:安装RocketMQ 

wget https://github.com/alibaba/RocketMQ/archive/v3.4.6.tar.gz
mv v3.4.6.tar.gz RocketMQ-3.4.6.tar.gz
tar -zxvf RocketMQ-3.4.6.tar.gz
执行install.sh:
sh install.sh

执行完成之后,如下:

RocketMQ_第1张图片

启动mq之前的准备工作:

给下列命令可执行权限:

cd /home/RocketMQ-3.4.6/bin;chmod +x mqadmin mqbroker mqfiltersrv mqshutdown  mqnamesrv

启动nameserver:

cd /home/RocketMQ-3.4.6/devenv/bin
nohup sh  mqnamesrv >/var/log/ns.log &

其中sh命令一定要加上!

验证nameserver是否启动:

tail -f /var/log/ns.log

 

启动broker

在启动borker之前需要指定nameserver地址

 

export NAMESRV_ADDR=127.0.0.1:9876
 nohup sh  mqbroker >/var/log/mq.log &

其中sh命令一定要加上!

验证mqbroker是否启动:

tail -f /var/log/mq.log

 

最后要配置一下防火墙:

nameserver端口为9876

broker端口为10911

 

关闭nameserver broker:

mqshutdown namesrv
mqshutdown broker

 

 最后:在idea中创建一个maven工程:

pom文件如下:

复制代码
xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ebways-mqartifactId>
        <groupId>com.ebwaysgroupId>
        <version>1.0.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>ebways-mq-providerartifactId>

    <properties>
        <ebways.mq.facade.version>1.0.0-SNAPSHOTebways.mq.facade.version>

    properties>

    <dependencies>

        <dependency>
            <groupId>com.alibaba.rocketmqgroupId>
            <artifactId>rocketmq-clientartifactId>
            <version>3.5.8version>
        dependency>
        <dependency>
            <groupId>com.alibaba.rocketmqgroupId>
            <artifactId>rocketmq-srvutilartifactId>
            <version>3.2.6version>
        dependency>
        <dependency>
            <groupId>com.alibaba.rocketmqgroupId>
            <artifactId>rocketmq-remotingartifactId>
            <version>3.4.6version>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
        dependency>
        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
        dependency>

        <dependency>
            <groupId>com.ebwaysgroupId>
            <artifactId>ebways-mq-facadeartifactId>
            <version>${ebways.mq.facade.version}version>
        dependency>
        <dependency>
            <groupId>io.nettygroupId>
            <artifactId>netty-allartifactId>
            <version>4.0.29.Finalversion>
        dependency>

    dependencies>


    <build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-pluginartifactId>
                <executions>
                    <execution>
                        <id>unpackid>
                        <phase>packagephase>
                        <goals>
                            <goal>unpack-dependenciesgoal>
                        goals>
                        <configuration>
                            <includeGroupIds>com.alibabaincludeGroupIds>
                            <includeArtifactIds>dubboincludeArtifactIds>
                            <outputDirectory>${project.build.directory}/dubbooutputDirectory>
                            <includes>META-INF/assembly/**includes>
                        configuration>
                    execution>
                executions>
            plugin>
            <plugin>
                <artifactId>maven-assembly-pluginartifactId>
            plugin>
        plugins>
    build>

project>
复制代码

 

消息生产者:

复制代码
package com.ebways.mq.test;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;

/**
 * Created by gmq on 2016/10/12 0012.
 */
public class RocketProductor {

    public static void main(String[] args) {

        DefaultMQProducer producer = new DefaultMQProducer("Producer");
        producer.setVipChannelEnabled(false);
        producer.setNamesrvAddr("192.168.100.190:9876");

        try {

            producer.start();

            Message message = new Message("PushTopic", "push", "1", "Just fot test.".getBytes());
            SendResult result = producer.send(message);
            System.out.println("id:" + result.getMsgId() + " result:" + result.getSendStatus());

            message = new Message("PushTopic", "push", "3", "Just fot test.".getBytes());

            result = producer.send(message);
            System.out.println("id:" + result.getMsgId() + " result:" + result.getSendStatus());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.shutdown();
        }

    }
}
复制代码

消费者:

复制代码
package com.ebways.mq.test;

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

import java.util.List;

/**
 * Created by gmq on 2016/10/12 0012.
 */
public class RocketConsumer {

    public static void main(String[] args) {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("PushConsumer");
        consumer.setVipChannelEnabled(false);
        consumer.setNamesrvAddr("192.168.100.190:9876");

        try {

            // 订阅PushTopic下的Tag未push的消息
            consumer.subscribe("PushTopic", "push");
            // 程序第一次启动从消息队列头取数据
            consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
            consumer.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List msgs, ConsumeConcurrentlyContext context) {
                    Message message = msgs.get(0);
                    System.err.println(message.toString());
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
            consumer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
复制代码

生产者控制台:

复制代码
"C:\Program Files\Java\jdk1.7.0_75\bin\java" -Didea.launcher.port=7539 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_75\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\rt.jar;D:\workspaces\workspace_git\ebways-mq\ebways-mq-provider\target\test-classes;D:\workspaces\workspace_git\ebways-mq\ebways-mq-provider\target\classes;D:\maven_repo\repository\com\alibaba\rocketmq\rocketmq-client\3.5.8\rocketmq-client-3.5.8.jar;D:\maven_repo\repository\org\slf4j\slf4j-api\1.6.1\slf4j-api-1.6.1.jar;D:\maven_repo\repository\com\alibaba\rocketmq\rocketmq-srvutil\3.2.6\rocketmq-srvutil-3.2.6.jar;D:\maven_repo\repository\com\alibaba\rocketmq\rocketmq-common\3.2.6\rocketmq-common-3.2.6.jar;D:\maven_repo\repository\commons-cli\commons-cli\1.2\commons-cli-1.2.jar;D:\maven_repo\repository\commons-io\commons-io\2.4\commons-io-2.4.jar;D:\maven_repo\repository\com\alibaba\rocketmq\rocketmq-remoting\3.4.6\rocketmq-remoting-3.4.6.jar;D:\maven_repo\repository\com\alibaba\fastjson\1.1.39\fastjson-1.1.39.jar;D:\maven_repo\repository\com\alibaba\druid\0.2.10\druid-0.2.10.jar;D:\maven_repo\repository\mysql\mysql-connector-java\5.1.14\mysql-connector-java-5.1.14.jar;D:\maven_repo\repository\com\alibaba\dubbo\2.5.3\dubbo-2.5.3.jar;D:\maven_repo\repository\org\javassist\javassist\3.15.0-GA\javassist-3.15.0-GA.jar;D:\maven_repo\repository\org\jboss\netty\netty\3.2.5.Final\netty-3.2.5.Final.jar;D:\maven_repo\repository\com\101tec\zkclient\0.4\zkclient-0.4.jar;D:\maven_repo\repository\org\apache\zookeeper\zookeeper\3.4.5\zookeeper-3.4.5.jar;D:\maven_repo\repository\org\slf4j\slf4j-log4j12\1.6.1\slf4j-log4j12-1.6.1.jar;D:\maven_repo\repository\jline\jline\0.9.94\jline-0.9.94.jar;D:\maven_repo\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;D:\workspaces\workspace_git\ebways-mq\ebways-mq-facade\target\classes;D:\maven_repo\repository\com\ebways\ebways-common\1.0.0-SNAPSHOT\ebways-common-1.0.0-20160817.113016-4.jar;D:\maven_repo\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\maven_repo\repository\com\fasterxml\jackson\core\jackson-core\2.3.0\jackson-core-2.3.0.jar;D:\maven_repo\repository\com\fasterxml\jackson\core\jackson-databind\2.3.0\jackson-databind-2.3.0.jar;D:\maven_repo\repository\com\fasterxml\jackson\core\jackson-annotations\2.3.0\jackson-annotations-2.3.0.jar;D:\maven_repo\repository\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar;D:\maven_repo\repository\commons-logging\commons-logging\1.0.4\commons-logging-1.0.4.jar;D:\maven_repo\repository\xalan\xalan\2.7.1\xalan-2.7.1.jar;D:\maven_repo\repository\xalan\serializer\2.7.1\serializer-2.7.1.jar;D:\maven_repo\repository\cglib\cglib\2.2.2\cglib-2.2.2.jar;D:\maven_repo\repository\asm\asm\3.3.1\asm-3.3.1.jar;D:\maven_repo\repository\net\sf\json-lib\json-lib\2.4\json-lib-2.4-jdk15.jar;D:\maven_repo\repository\net\sf\ezmorph\ezmorph\1.0.6\ezmorph-1.0.6.jar;D:\maven_repo\repository\commons-beanutils\commons-beanutils\1.8.0\commons-beanutils-1.8.0.jar;D:\maven_repo\repository\commons-codec\commons-codec\1.8\commons-codec-1.8.jar;D:\maven_repo\repository\commons-collections\commons-collections\3.2\commons-collections-3.2.jar;D:\maven_repo\repository\commons-digester\commons-digester\2.1\commons-digester-2.1.jar;D:\maven_repo\repository\commons-fileupload\commons-fileupload\1.2.2\commons-fileupload-1.2.2.jar;D:\maven_repo\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;D:\maven_repo\repository\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;D:\maven_repo\repository\redis\clients\jedis\2.6.2\jedis-2.6.2.jar;D:\maven_repo\repository\org\apache\commons\commons-pool2\2.0\commons-pool2-2.0.jar;D:\maven_repo\repository\com\ibm\icu\icu4j\3.4.4\icu4j-3.4.4.jar;D:\maven_repo\repository\com\jcraft\jsch\0.1.50\jsch-0.1.50.jar;D:\maven_repo\repository\commons-net\commons-net\3.3\commons-net-3.3.jar;D:\maven_repo\repository\org\springframework\spring-aop\3.2.8.RELEASE\spring-aop-3.2.8.RELEASE.jar;D:\maven_repo\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\maven_repo\repository\org\springframework\spring-aspects\3.2.8.RELEASE\spring-aspects-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\aspectj\aspectjweaver\1.7.4\aspectjweaver-1.7.4.jar;D:\maven_repo\repository\org\springframework\spring-beans\3.2.8.RELEASE\spring-beans-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-context\3.2.8.RELEASE\spring-context-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-expression\3.2.8.RELEASE\spring-expression-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-context-support\3.2.8.RELEASE\spring-context-support-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-core\3.2.8.RELEASE\spring-core-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-jms\3.2.8.RELEASE\spring-jms-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-tx\3.2.8.RELEASE\spring-tx-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-oxm\3.2.8.RELEASE\spring-oxm-3.2.8.RELEASE.jar;D:\maven_repo\repository\io\netty\netty-all\4.0.29.Final\netty-all-4.0.29.Final.jar;D:\maven_repo\repository\junit\junit\4.10\junit-4.10.jar;D:\maven_repo\repository\org\hamcrest\hamcrest-core\1.1\hamcrest-core-1.1.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.ebways.mq.test.RocketProductor
2016-10-12 16:25:16,818 DEBUG [Slf4JLogger.java:71] : Using SLF4J as the default logging framework
2016-10-12 16:25:16,838 DEBUG [Slf4JLogger.java:76] : -Dio.netty.eventLoopThreads: 8
2016-10-12 16:25:16,852 DEBUG [Slf4JLogger.java:76] : java.nio.Buffer.address: available
2016-10-12 16:25:16,853 DEBUG [Slf4JLogger.java:76] : sun.misc.Unsafe.theUnsafe: available
2016-10-12 16:25:16,854 DEBUG [Slf4JLogger.java:71] : sun.misc.Unsafe.copyMemory: available
2016-10-12 16:25:16,855 DEBUG [Slf4JLogger.java:76] : java.nio.Bits.unaligned: true
2016-10-12 16:25:16,856 DEBUG [Slf4JLogger.java:71] : Platform: Windows
2016-10-12 16:25:16,856 DEBUG [Slf4JLogger.java:76] : Java version: 7
2016-10-12 16:25:16,857 DEBUG [Slf4JLogger.java:76] : -Dio.netty.noUnsafe: false
2016-10-12 16:25:16,857 DEBUG [Slf4JLogger.java:76] : sun.misc.Unsafe: available
2016-10-12 16:25:16,858 DEBUG [Slf4JLogger.java:76] : -Dio.netty.noJavassist: false
2016-10-12 16:25:17,007 DEBUG [Slf4JLogger.java:71] : Javassist: available
2016-10-12 16:25:17,007 DEBUG [Slf4JLogger.java:76] : -Dio.netty.tmpdir: C:\Users\ADMINI~1\AppData\Local\Temp (java.io.tmpdir)
2016-10-12 16:25:17,007 DEBUG [Slf4JLogger.java:76] : -Dio.netty.bitMode: 64 (sun.arch.data.model)
2016-10-12 16:25:17,008 DEBUG [Slf4JLogger.java:76] : -Dio.netty.noPreferDirect: false
2016-10-12 16:25:17,074 DEBUG [Slf4JLogger.java:76] : -Dio.netty.noKeySetOptimization: false
2016-10-12 16:25:17,077 DEBUG [Slf4JLogger.java:76] : -Dio.netty.selectorAutoRebuildThreshold: 512
2016-10-12 16:25:18,041 DEBUG [Slf4JLogger.java:71] : -Dio.netty.initialSeedUniquifier: 0x204d19d796b8da28 (took 16 ms)
2016-10-12 16:25:18,101 DEBUG [Slf4JLogger.java:76] : -Dio.netty.allocator.type: unpooled
2016-10-12 16:25:18,102 DEBUG [Slf4JLogger.java:76] : -Dio.netty.threadLocalDirectBufferSize: 65536
2016-10-12 16:25:18,102 DEBUG [Slf4JLogger.java:76] : -Dio.netty.maxThreadLocalCharBufferSize: 16384
2016-10-12 16:25:18,125 DEBUG [Slf4JLogger.java:76] : Generated: com.alibaba.rocketmq.shade.io.netty.util.internal.__matchers__.com.alibaba.rocketmq.remoting.protocol.RemotingCommandMatcher
2016-10-12 16:25:18,151 DEBUG [Slf4JLogger.java:76] : -Dio.netty.recycler.maxCapacity.default: 262144
2016-10-12 16:25:18,158 DEBUG [Slf4JLogger.java:81] : -Dcom.alibaba.rocketmq.shade.io.netty.buffer.bytebuf.checkAccessible: true
2016-10-12 16:25:18,161 DEBUG [Slf4JLogger.java:81] : -Dcom.alibaba.rocketmq.shade.io.netty.leakDetection.level: simple
2016-10-12 16:25:18,161 DEBUG [Slf4JLogger.java:81] : -Dcom.alibaba.rocketmq.shade.io.netty.leakDetection.maxRecords: 4
2016-10-12 16:25:18,341 DEBUG [Slf4JLogger.java:76] : java.nio.ByteBuffer.cleaner(): available
id:C0A864592C74592FA6173998D9A40000 result:SEND_OK
id:C0A864592C74592FA6173998D9C50001 result:SEND_OK

Process finished with exit code 0
复制代码

消费者控制台:

复制代码
"C:\Program Files\Java\jdk1.7.0_75\bin\java" -Didea.launcher.port=7538 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_75\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_75\jre\lib\rt.jar;D:\workspaces\workspace_git\ebways-mq\ebways-mq-provider\target\test-classes;D:\workspaces\workspace_git\ebways-mq\ebways-mq-provider\target\classes;D:\maven_repo\repository\com\alibaba\rocketmq\rocketmq-client\3.5.8\rocketmq-client-3.5.8.jar;D:\maven_repo\repository\org\slf4j\slf4j-api\1.6.1\slf4j-api-1.6.1.jar;D:\maven_repo\repository\com\alibaba\rocketmq\rocketmq-srvutil\3.2.6\rocketmq-srvutil-3.2.6.jar;D:\maven_repo\repository\com\alibaba\rocketmq\rocketmq-common\3.2.6\rocketmq-common-3.2.6.jar;D:\maven_repo\repository\commons-cli\commons-cli\1.2\commons-cli-1.2.jar;D:\maven_repo\repository\commons-io\commons-io\2.4\commons-io-2.4.jar;D:\maven_repo\repository\com\alibaba\rocketmq\rocketmq-remoting\3.4.6\rocketmq-remoting-3.4.6.jar;D:\maven_repo\repository\com\alibaba\fastjson\1.1.39\fastjson-1.1.39.jar;D:\maven_repo\repository\com\alibaba\druid\0.2.10\druid-0.2.10.jar;D:\maven_repo\repository\mysql\mysql-connector-java\5.1.14\mysql-connector-java-5.1.14.jar;D:\maven_repo\repository\com\alibaba\dubbo\2.5.3\dubbo-2.5.3.jar;D:\maven_repo\repository\org\javassist\javassist\3.15.0-GA\javassist-3.15.0-GA.jar;D:\maven_repo\repository\org\jboss\netty\netty\3.2.5.Final\netty-3.2.5.Final.jar;D:\maven_repo\repository\com\101tec\zkclient\0.4\zkclient-0.4.jar;D:\maven_repo\repository\org\apache\zookeeper\zookeeper\3.4.5\zookeeper-3.4.5.jar;D:\maven_repo\repository\org\slf4j\slf4j-log4j12\1.6.1\slf4j-log4j12-1.6.1.jar;D:\maven_repo\repository\jline\jline\0.9.94\jline-0.9.94.jar;D:\maven_repo\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;D:\workspaces\workspace_git\ebways-mq\ebways-mq-facade\target\classes;D:\maven_repo\repository\com\ebways\ebways-common\1.0.0-SNAPSHOT\ebways-common-1.0.0-20160817.113016-4.jar;D:\maven_repo\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\maven_repo\repository\com\fasterxml\jackson\core\jackson-core\2.3.0\jackson-core-2.3.0.jar;D:\maven_repo\repository\com\fasterxml\jackson\core\jackson-databind\2.3.0\jackson-databind-2.3.0.jar;D:\maven_repo\repository\com\fasterxml\jackson\core\jackson-annotations\2.3.0\jackson-annotations-2.3.0.jar;D:\maven_repo\repository\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar;D:\maven_repo\repository\commons-logging\commons-logging\1.0.4\commons-logging-1.0.4.jar;D:\maven_repo\repository\xalan\xalan\2.7.1\xalan-2.7.1.jar;D:\maven_repo\repository\xalan\serializer\2.7.1\serializer-2.7.1.jar;D:\maven_repo\repository\cglib\cglib\2.2.2\cglib-2.2.2.jar;D:\maven_repo\repository\asm\asm\3.3.1\asm-3.3.1.jar;D:\maven_repo\repository\net\sf\json-lib\json-lib\2.4\json-lib-2.4-jdk15.jar;D:\maven_repo\repository\net\sf\ezmorph\ezmorph\1.0.6\ezmorph-1.0.6.jar;D:\maven_repo\repository\commons-beanutils\commons-beanutils\1.8.0\commons-beanutils-1.8.0.jar;D:\maven_repo\repository\commons-codec\commons-codec\1.8\commons-codec-1.8.jar;D:\maven_repo\repository\commons-collections\commons-collections\3.2\commons-collections-3.2.jar;D:\maven_repo\repository\commons-digester\commons-digester\2.1\commons-digester-2.1.jar;D:\maven_repo\repository\commons-fileupload\commons-fileupload\1.2.2\commons-fileupload-1.2.2.jar;D:\maven_repo\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;D:\maven_repo\repository\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;D:\maven_repo\repository\redis\clients\jedis\2.6.2\jedis-2.6.2.jar;D:\maven_repo\repository\org\apache\commons\commons-pool2\2.0\commons-pool2-2.0.jar;D:\maven_repo\repository\com\ibm\icu\icu4j\3.4.4\icu4j-3.4.4.jar;D:\maven_repo\repository\com\jcraft\jsch\0.1.50\jsch-0.1.50.jar;D:\maven_repo\repository\commons-net\commons-net\3.3\commons-net-3.3.jar;D:\maven_repo\repository\org\springframework\spring-aop\3.2.8.RELEASE\spring-aop-3.2.8.RELEASE.jar;D:\maven_repo\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\maven_repo\repository\org\springframework\spring-aspects\3.2.8.RELEASE\spring-aspects-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\aspectj\aspectjweaver\1.7.4\aspectjweaver-1.7.4.jar;D:\maven_repo\repository\org\springframework\spring-beans\3.2.8.RELEASE\spring-beans-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-context\3.2.8.RELEASE\spring-context-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-expression\3.2.8.RELEASE\spring-expression-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-context-support\3.2.8.RELEASE\spring-context-support-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-core\3.2.8.RELEASE\spring-core-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-jms\3.2.8.RELEASE\spring-jms-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-tx\3.2.8.RELEASE\spring-tx-3.2.8.RELEASE.jar;D:\maven_repo\repository\org\springframework\spring-oxm\3.2.8.RELEASE\spring-oxm-3.2.8.RELEASE.jar;D:\maven_repo\repository\io\netty\netty-all\4.0.29.Final\netty-all-4.0.29.Final.jar;D:\maven_repo\repository\junit\junit\4.10\junit-4.10.jar;D:\maven_repo\repository\org\hamcrest\hamcrest-core\1.1\hamcrest-core-1.1.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.ebways.mq.test.RocketConsumer
2016-10-12 16:25:05,432 DEBUG [Slf4JLogger.java:71] : Using SLF4J as the default logging framework
2016-10-12 16:25:05,443 DEBUG [Slf4JLogger.java:76] : -Dio.netty.eventLoopThreads: 8
2016-10-12 16:25:05,458 DEBUG [Slf4JLogger.java:76] : java.nio.Buffer.address: available
2016-10-12 16:25:05,459 DEBUG [Slf4JLogger.java:76] : sun.misc.Unsafe.theUnsafe: available
2016-10-12 16:25:05,459 DEBUG [Slf4JLogger.java:71] : sun.misc.Unsafe.copyMemory: available
2016-10-12 16:25:05,460 DEBUG [Slf4JLogger.java:76] : java.nio.Bits.unaligned: true
2016-10-12 16:25:05,461 DEBUG [Slf4JLogger.java:71] : Platform: Windows
2016-10-12 16:25:05,461 DEBUG [Slf4JLogger.java:76] : Java version: 7
2016-10-12 16:25:05,462 DEBUG [Slf4JLogger.java:76] : -Dio.netty.noUnsafe: false
2016-10-12 16:25:05,462 DEBUG [Slf4JLogger.java:76] : sun.misc.Unsafe: available
2016-10-12 16:25:05,462 DEBUG [Slf4JLogger.java:76] : -Dio.netty.noJavassist: false
2016-10-12 16:25:05,621 DEBUG [Slf4JLogger.java:71] : Javassist: available
2016-10-12 16:25:05,622 DEBUG [Slf4JLogger.java:76] : -Dio.netty.tmpdir: C:\Users\ADMINI~1\AppData\Local\Temp (java.io.tmpdir)
2016-10-12 16:25:05,622 DEBUG [Slf4JLogger.java:76] : -Dio.netty.bitMode: 64 (sun.arch.data.model)
2016-10-12 16:25:05,622 DEBUG [Slf4JLogger.java:76] : -Dio.netty.noPreferDirect: false
2016-10-12 16:25:05,664 DEBUG [Slf4JLogger.java:76] : -Dio.netty.noKeySetOptimization: false
2016-10-12 16:25:05,664 DEBUG [Slf4JLogger.java:76] : -Dio.netty.selectorAutoRebuildThreshold: 512
2016-10-12 16:25:05,964 DEBUG [Slf4JLogger.java:71] : -Dio.netty.initialSeedUniquifier: 0x90c3e98f2f23855e (took 49 ms)
2016-10-12 16:25:06,011 DEBUG [Slf4JLogger.java:76] : -Dio.netty.allocator.type: unpooled
2016-10-12 16:25:06,012 DEBUG [Slf4JLogger.java:76] : -Dio.netty.threadLocalDirectBufferSize: 65536
2016-10-12 16:25:06,012 DEBUG [Slf4JLogger.java:76] : -Dio.netty.maxThreadLocalCharBufferSize: 16384
2016-10-12 16:25:06,103 DEBUG [Slf4JLogger.java:76] : Generated: com.alibaba.rocketmq.shade.io.netty.util.internal.__matchers__.com.alibaba.rocketmq.remoting.protocol.RemotingCommandMatcher
2016-10-12 16:25:06,543 DEBUG [Slf4JLogger.java:76] : -Dio.netty.recycler.maxCapacity.default: 262144
2016-10-12 16:25:06,551 DEBUG [Slf4JLogger.java:81] : -Dcom.alibaba.rocketmq.shade.io.netty.buffer.bytebuf.checkAccessible: true
2016-10-12 16:25:06,554 DEBUG [Slf4JLogger.java:81] : -Dcom.alibaba.rocketmq.shade.io.netty.leakDetection.level: simple
2016-10-12 16:25:06,554 DEBUG [Slf4JLogger.java:81] : -Dcom.alibaba.rocketmq.shade.io.netty.leakDetection.maxRecords: 4
2016-10-12 16:25:06,743 DEBUG [Slf4JLogger.java:76] : java.nio.ByteBuffer.cleaner(): available
Exception in thread "RebalanceService" java.lang.NoSuchMethodError: java.util.concurrent.ConcurrentHashMap.keySet()Ljava/util/concurrent/ConcurrentHashMap$KeySetView;
    at com.alibaba.rocketmq.client.impl.consumer.RebalanceImpl.truncateMessageQueueNotMyTopic(RebalanceImpl.java:315)
    at com.alibaba.rocketmq.client.impl.consumer.RebalanceImpl.doRebalance(RebalanceImpl.java:230)
    at com.alibaba.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl.doRebalance(DefaultMQPushConsumerImpl.java:913)
    at com.alibaba.rocketmq.client.impl.factory.MQClientInstance.doRebalance(MQClientInstance.java:889)
    at com.alibaba.rocketmq.client.impl.consumer.RebalanceService.run(RebalanceService.java:47)
    at java.lang.Thread.run(Thread.java:745)
MessageExt [queueId=3, storeSize=183, queueOffset=2, sysFlag=0, bornTimestamp=1476260718501, bornHost=/192.168.100.89:53902, storeTimestamp=1476260717911, storeHost=/192.168.100.190:10911, msgId=C0A864BE00002A9F000000000000044A, commitLogOffset=1098, bodyCRC=1329428386, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=PushTopic, flag=0, properties={TAGS=push, KEYS=1, WAIT=true, UNIQ_KEY=C0A864592C74592FA6173998D9A40000, MAX_OFFSET=3, CONSUME_START_TIME=1476260718542, MIN_OFFSET=0}, body=14]]
MessageExt [queueId=0, storeSize=183, queueOffset=2, sysFlag=0, bornTimestamp=1476260718533, bornHost=/192.168.100.89:53902, storeTimestamp=1476260717916, storeHost=/192.168.100.190:10911, msgId=C0A864BE00002A9F0000000000000501, commitLogOffset=1281, bodyCRC=1329428386, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=PushTopic, flag=0, properties={TAGS=push, KEYS=3, WAIT=true, UNIQ_KEY=C0A864592C74592FA6173998D9C50001, MAX_OFFSET=3, CONSUME_START_TIME=1476260718564, MIN_OFFSET=0}, body=14]]
复制代码

 

 以上。

 

 

二、官方安装:(未实验)

 

1、简介

    RocketMQ的前身是Metaq,当Metaq3.0发布时,产品名称改为RocketMQ

    MetaQ2.x版本由于依赖了alibaba公司内部其他系统,对于公司外部用户使用不够友好,推荐使用3.0版本。

     项目地址:https://github.com/alibaba/RocketMQ

2、安装RocketMQ

     安装RocketMQ需要jdk1.6, maven,git环境

     如果本机没有安装git,请使用如下命令安装

     yum install git

     具体安装步骤可以参考RocketMQ项目组给出的步骤,参见:

     https://github.com/alibaba/RocketMQ/wiki/Quick-Start

git clone https://github.com/alibaba/RocketMQ.git

cd RocketMQ

sh install.sh

cd devenv

      安装完成后,因为install.sh脚本中创建devenv 符号链接写错了目录,需要在RocketMQ目录下执行如下命令:

       rm -rf devenv

       ln -s target/alibaba-rocketmq-3.0.7/alibaba-rocketmq devenv

       启动RocketMQ

       cd devenv/bin

       nohup sh mqnamesrv &

       nohup sh mqbroker -n "192.168.230.128:9876" &

       more nohup.out

       如果显示:

       The Name Server boot success.
       The broker[vdata.kt, 192.168.230.128:10911] boot success.

       则NameServer,Broker启动成功

3、在eclipse中开发测试

       1.创建一个maven项目,其pom.xml的内容见pom.xml

       2. 编写消息产生者Producer,见Producer.java

       3.编写消息消费者Consumer,见PushConsumer.java

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
 
    <groupId>com.cattgroupId>
    <artifactId>RocketMQTestartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
 
    <name>RocketMQTestname>
    <url>http://maven.apache.orgurl>
 
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
 
    <dependencies>
        <dependency>
            <groupId>com.alibaba.rocketmqgroupId>
            <artifactId>rocketmq-clientartifactId>
            <version>3.0.7version>
        dependency>
        <dependency>
            <groupId>com.alibaba.rocketmqgroupId>
            <artifactId>rocketmq-allartifactId>
            <version>3.0.7version>
            <type>pomtype>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
            <version>1.1.1version>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-coreartifactId>
            <version>1.1.1version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.10version>
            <scope>testscope>
        dependency>
    dependencies>
project>

Producer.java

默认情况下,一台服务器只能启动一个Producer或Consumer实例,所以如果需要在一台服务器启动多个实例,需要设置实例的名称

producer.setNamesrvAddr("192.168.230.128:9876");
producer.setInstanceName("Producer");

 
package  com.catt.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,由应用来维护此对象,可以设置为全局对象或者单例
         * 注意:ProducerGroupName需要由应用来保证唯一
         * ProducerGroup这个概念发送普通的消息时,作用不大,但是发送分布式事务消息时,比较关键,
         * 因为服务器会回查这个Group下的任意一个Producer
         */
        DefaultMQProducer producer = new  DefaultMQProducer("ProducerGroupName");
        producer.setNamesrvAddr("192.168.230.128:9876");
        producer.setInstanceName("Producer");
 
        /**
         * Producer对象在使用之前必须要调用start初始化,初始化一次即可
         * 注意:切记不可以在每次发送消息时,都调用start方法
         */
        producer.start();
 
        /**
         * 下面这段代码表明一个Producer对象可以发送多个topic,多个tag的消息。
         * 注意:send方法是同步调用,只要不抛异常就标识成功。但是发送成功也可会有多种状态,
         * 例如消息写入Master成功,但是Slave不成功,这种情况消息属于成功,但是对于个别应用如果对消息可靠性要求极高,
         * 需要对这种情况做处理。另外,消息可能会存在发送失败的情况,失败重试由应用来处理。
         */
        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();
    }
}

PushConsumer.java

 
package  com.catt.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服务器推到了应用客户端。
     * 但是实际PushConsumer内部是使用长轮询Pull方式从MetaQ服务器拉消息,然后再回调用户Listener方法
     */
    public  static  void  main(String[] args) throws  InterruptedException,
            MQClientException {
        /**
         * 一个应用创建一个Consumer,由应用来维护此对象,可以设置为全局对象或者单例
         * 注意:ConsumerGroupName需要由应用来保证唯一
         */
        DefaultMQPushConsumer consumer = new  DefaultMQPushConsumer(
                "ConsumerGroupName");
        consumer.setNamesrvAddr("192.168.230.128:9876");
        consumer.setInstanceName("Consumber");
 
        /**
         * 订阅指定topic下tags分别等于TagA或TagC或TagD
         */
        consumer.subscribe("TopicTest1""TagA || TagC || TagD");
        /**
         * 订阅指定topic下所有消息
         * 注意:一个consumer对象可以订阅多个topic
         */
        consumer.subscribe("TopicTest2""*");
 
        consumer.registerMessageListener(new  MessageListenerConcurrently() {
 
            /**
             * 默认msgs里只有一条消息,可以通过设置consumeMessageBatchMaxSize参数来批量接收消息
             */
            @Override
            public  ConsumeConcurrentlyStatus consumeMessage(
                    List 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初始化,初始化一次即可
         */
        consumer.start();
 
        System.out.println("Consumer Started.");
    }
}
标签:  mq,  rocketmq

你可能感兴趣的:(java,rocketmq)