Docker搭建RocketMQ问题及解决方案

本文为Docker搭建RocketMQ问题及解决方案

转载请注明:http://blog.csdn.net/sinat_28434649/article/details/79291988

1、环境

OS:Linux
Docker:1.6.2
RocketMQ:4.2
搭建参考 rocketmq-docker,请下载该工程

1.2、构建mqnamesrv镜像

下载rocketmq-docker

#构架镜像
sh docker_build.sh

构建失败

1.3、修改mqnamesrv\Dockerfile内容

查看Dockerfile内容

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Start from a Java image.
FROM java:8

# Rocketmq version
ENV ROCKETMQ_VERSION 4.0.0-incubating

# Rocketmq home
ENV ROCKETMQ_HOME  /opt/rocketmq-${ROCKETMQ_VERSION}



WORKDIR  ${ROCKETMQ_HOME}

RUN mkdir -p \
		/opt/logs \
	    /opt/store

RUN curl https://dist.apache.org/repos/dist/release/incubator/rocketmq/${ROCKETMQ_VERSION}/rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip -o rocketmq.zip \
          && unzip rocketmq.zip \
          && mv apache-rocketmq-all/* . \
          && rmdir apache-rocketmq-all  \
          && rm rocketmq.zip


RUN chmod +x bin/mqbroker

CMD cd ${ROCKETMQ_HOME}/bin && export JAVA_OPT=" -Duser.home=/opt" && sh mqbroker


EXPOSE 10909 10911
VOLUME /opt/logs \
		/opt/store

发现以上链接已失效。上官网查找下载链接更新Dockerfile文件

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


# Start from a Java image.
FROM java:8


# Rocketmq version
ENV ROCKETMQ_VERSION 4.2.0


# Rocketmq home
ENV ROCKETMQ_HOME  /opt/rocketmq-${ROCKETMQ_VERSION}


WORKDIR  ${ROCKETMQ_HOME}


RUN mkdir -p \
		/opt/logs \
		/opt/store


RUN curl http://mirror.bit.edu.cn/apache/rocketmq/${ROCKETMQ_VERSION}/rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip -o rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip \
          && unzip rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip \
          && rm rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip




RUN chmod +x bin/mqnamesrv


CMD cd ${ROCKETMQ_HOME}/bin && export JAVA_OPT=" -Duser.home=/opt"  && sh mqnamesrv -c /opt/rocketmq-4.2.0/conf/broker.conf




EXPOSE 9876
VOLUME /opt/logs \
		/opt/store
加载默认配置文件,实际使用是需修改配置文件。

1.4、再次构建mqnamesrv镜像

#构架镜像
sh docker_build.sh

1.5、创建容器

#创建容器
docker run -d -p 9876:9876 --name rmqnamesrv -e "JAVA_OPT_EXT=-server -Xms128m -Xmx256m -Xmn128m" apache/incubator-rocketmq-namesrv:4.2.0-incubating

2.1、构建broker镜像

同理修改mqnamesrv\Dockerfile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


# Start from a Java image.
FROM java:8


# Rocketmq version
ENV ROCKETMQ_VERSION 4.2.0


# Rocketmq home
ENV ROCKETMQ_HOME  /opt/rocketmq-${ROCKETMQ_VERSION}






WORKDIR  ${ROCKETMQ_HOME}


RUN mkdir -p \
		/opt/logs \
	    /opt/store


RUN curl http://mirror.bit.edu.cn/apache/rocketmq/${ROCKETMQ_VERSION}/rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip -o rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip \
          && unzip rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip \
          && rm rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip




RUN chmod +x bin/mqbroker


CMD cd ${ROCKETMQ_HOME}/bin && export && export JAVA_OPT=" -Duser.home=/opt"  && sh mqbroker -c /opt/rocketmq-4.2.0/conf/broker.conf





EXPOSE 10909 10911
VOLUME /opt/logs \
		/opt/store

默认加载配置文件,实际使用是需修改配置文件。

#构架镜像
sh docker_build.sh

2.2、创建容器

docker run -d -p 10911:10911 -p 10909:10909 --name rmqbroker --link rmqnamesrv:rmqnamesrv -e "NAMESRV_ADDR=rmqnamesrv:9876"  -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m"  apache/incubator-rocketmq-broker:4.2.0-incubating


3、测试RocketMQ

root> docker exec -it rmqnamesrv /bin/bash
ERRO[0000] Unable to connect to local syslog daemon     
root@84b097b311e9:/opt/rocketmq-4.2.0# cd /opt/rocketmq-4.2.0/
root@84b097b311e9:/opt/rocketmq-4.2.0# export NAMESRV_ADDR=localhost:9876
root@84b097b311e9:/opt/rocketmq-4.2.0# sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
....
SendResult [sendStatus=SEND_OK, msgId=AC11004101216E0BE85827526C4A0263, offsetMsgId=AC11004200002A9F000000000002BE44, messageQueue=MessageQueue [topic=TopicTest, brokerName=a2e2cadee871, queueId=0], queueOffset=250]
SendResult [sendStatus=SEND_OK, msgId=AC11004101216E0BE85827526C500264, offsetMsgId=AC11004200002A9F000000000002BEF8, messageQueue=MessageQueue [topic=TopicTest, brokerName=a2e2cadee871, queueId=1], queueOffset=250]
....

生产消息成功

root>sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
....
ConsumeMessageThread_18 Receive New Messages: [MessageExt [queueId=1, storeSize=179, queueOffset=100, sysFlag=0, bornTimestamp=1518102906647, bornHost=/172.17.0.65:50605, storeTimestamp=1518102906650, storeHost=/172.17.0.66:10911, msgId=AC11004200002A9F0000000000011970, commitLogOffset=72048, bodyCRC=1703501626, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=252, CONSUME_START_TIME=1518103025749, UNIQ_KEY=AC11004101216E0BE85827525317000C, WAIT=true, TAGS=TagA}, body=17]]] 
ConsumeMessageThread_14 Receive New Messages: [MessageExt [queueId=1, storeSize=180, queueOffset=92, sysFlag=0, bornTimestamp=1518100775617, bornHost=/172.17.0.65:48328, storeTimestamp=1518100775618, storeHost=/172.17.0.66:10911, msgId=AC11004200002A9F0000000000010306, commitLogOffset=66310, bodyCRC=738600450, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=252, CONSUME_START_TIME=1518103025736, UNIQ_KEY=AC11004100566E0BE8582731CEC10171, WAIT=true, TAGS=TagA}, body=18]]] 
ConsumeMessageThread_16 Receive New Messages: [MessageExt [queueId=1, storeSize=180, queueOffset=90, sysFlag=0, bornTimestamp=1518100775585, bornHost=/172.17.0.65:48328, storeTimestamp=1518100775587, storeHost=/172.17.0.66:10911, msgId=AC11004200002A9F000000000000FD66, commitLogOffset=64870, bodyCRC=584953392, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=252, CONSUME_START_TIME=1518103025735, UNIQ_KEY=AC11004100566E0BE8582731CEA10169, WAIT=true, TAGS=TagA}, body=18]]] 
....

消费消息成功


4、问题总结

1】构建失败,查看一下官网看一下连接是否修改了;查看最新包,文件夹目录是否调整

2】out of memory问题,修改一下容器启动参数即可。

3】在docker下运行RocketMQ时,通过java客户端远程调用,namesrv返回的是docker容器的地址,java客户端无法访问到。

可以通过配置brokerIP1 = 实际ip 解决。(本机ip地址,默认系统自动识别,但是某些多网卡机器会存在识别错误的情况,这种情况下可以人工配置 解决。

具体需要加载配置文件,需要在构建的时候 追加 -c /opt/rocketmq-4.2.0/conf/broker.conf,再进入容器修改配置文件)

4】CODE: 1  DESC: java.lang.NullPointerException, org.apache.rocketmq.common.message.MessageExt.socketAddress2ByteBuffer(MessageExt.java:69)

尝试关闭再开启防火墙(PS:我的是群晖的机器,每次都要重启一下防火墙,原因未知)



5、结束语

Docker下按照官方文档搭建RocketMQ,主要遇到的问题是,原本的构建脚本不能用了,研究一下Dockerfile,按目前的版本修改即可。遇到out of memory,修改JVM启动参数即可。


6、参考文档:

http://rocketmq.apache.org/docs/quick-start/

https://github.com/apache/rocketmq-externals/tree/master/rocketmq-docker

http://blog.csdn.net/u013256816/article/details/54743551



你可能感兴趣的:(Docker,MQ,RocketMQ)