Docker安装RocketMQ(坑有点多)

1 Docker安装RocketMQ

1.1 安装流程

#拉取镜像
docker pull foxiswho/rocketmq:server-4.3.2
docker pull foxiswho/rocketmq:broker-4.3.2
#创建nameserver容器
docker create -p 9876:9876 --name rmqserver \
-e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" \
-e "JAVA_OPTS=-Duser.home=/opt" \
-v /haoke/rmq/rmqserver/logs:/opt/logs \
-v /haoke/rmq/rmqserver/store:/opt/store \
foxiswho/rocketmq:server-4.3.2
#创建broker容器
docker create -p 10911:10911 -p 10909:10909 --name rmqbroker \
-e "JAVA_OPTS=-Duser.home=/opt" \
-e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" \
-v /haoke/rmq/rmqbroker/conf/broker.conf:/etc/rocketmq/broker.conf \
-v /haoke/rmq/rmqbroker/logs:/opt/logs \
-v /haoke/rmq/rmqbroker/store:/opt/store \
foxiswho/rocketmq:broker-4.3.2
#启动容器
docker start rmqserver rmqbroker

1.2 测试代码

package cn.itcast.rocketmq.topic;

import org.apache.rocketmq.client.producer.DefaultMQProducer;

public class TopicDemo {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("haoke");

        //设置nameserver的地址
        producer.setNamesrvAddr("172.16.55.185:9876");

        // 启动生产者
        producer.start();

        /**
         * 创建topic,参数分别是:broker的名称,topic的名称,queue的数量
         *
         */
        producer.createTopic("broker_haoke_im", "my-topic", 8);

        System.out.println("topic创建成功!");

        producer.shutdown();
    }
}

1.3 运行结果

Docker安装RocketMQ(坑有点多)_第1张图片

2 异常

2.1 MQClientException: No route info of this topic, my-topic

异常说明:

在客户端的 Producer 运行起来准备发送消息时抛异常如下 “ No route info of this topic ” 异常产生的原因可能是:

  1. Broker 禁止自动创建 Topic,且用户没有通过手工方式创建 Topic
  2. Broker 没有正确连接到 Name Server
  3. Producer 没有正确连接到 Name Server 

解决办法:


排查1:Broker 禁止自动创建 Topic,且用户没有通过手工方式创建 Topic:可以在 Rocket_HOME/distribution/target/apache-rocketmq 下执行 " sh bin/mqbroker -m " 来查看 broker 的配置参数如下所示,autoCreateTopicEnable=true 证明是没有问题的。

root@localhost apache-rocketmq]# sh ./bin/mqbroker -m
2018-08-12 01\:27\:28 INFO main - namesrvAddr=
2018-08-12 01\:27\:28 INFO main - brokerIP1=192.168.58.129
2018-08-12 01\:27\:28 INFO main - brokerName=localhost.localdomain
2018-08-12 01\:27\:28 INFO main - brokerClusterName=DefaultCluster
2018-08-12 01\:27\:28 INFO main - brokerId=0
2018-08-12 01\:27\:28 INFO main - autoCreateTopicEnable=true
2018-08-12 01\:27\:28 INFO main - autoCreateSubscriptionGroup=true
2018-08-12 01\:27\:28 INFO main - rejectTransactionMessage=false
2018-08-12 01\:27\:28 INFO main - fetchNamesrvAddrByAddressServer=false
2018-08-12 01\:27\:28 INFO main - transactionTimeOut=3000
2018-08-12 01\:27\:28 INFO main - transactionCheckMax=5
2018-08-12 01\:27\:28 INFO main - transactionCheckInterval=60000
2018-08-12 01\:27\:28 INFO main - storePathRootDir=/root/store
2018-08-12 01\:27\:28 INFO main - storePathCommitLog=/root/store/commitlog
2018-08-12 01\:27\:28 INFO main - flushIntervalCommitLog=500
2018-08-12 01\:27\:28 INFO main - commitIntervalCommitLog=200
2018-08-12 01\:27\:28 INFO main - flushCommitLogTimed=false
2018-08-12 01\:27\:28 INFO main - deleteWhen=04
2018-08-12 01\:27\:28 INFO main - fileReservedTime=72
2018-08-12 01\:27\:28 INFO main - maxTransferBytesOnMessageInMemory=262144
2018-08-12 01\:27\:28 INFO main - maxTransferCountOnMessageInMemory=32
2018-08-12 01\:27\:28 INFO main - maxTransferBytesOnMessageInDisk=65536
2018-08-12 01\:27\:28 INFO main - maxTransferCountOnMessageInDisk=8
2018-08-12 01\:27\:28 INFO main - accessMessageInMemoryMaxRatio=40
2018-08-12 01\:27\:28 INFO main - messageIndexEnable=true
2018-08-12 01\:27\:28 INFO main - messageIndexSafe=false
2018-08-12 01\:27\:28 INFO main - haMasterAddress=
2018-08-12 01\:27\:28 INFO main - brokerRole=ASYNC_MASTER
2018-08-12 01\:27\:28 INFO main - flushDiskType=ASYNC_FLUSH
2018-08-12 01\:27\:28 INFO main - cleanFileForciblyEnable=true
2018-08-12 01\:27\:28 INFO main - transientStorePoolEnable=false
[root@localhost apache-rocketmq]# 

排查2:Broker 没有正确连接到 Name Server

我们创建配置文件broker.conf,broker.conf配置了Broker、NameServer的地址。

broker.conf文件:

#创建broker配置文件
vim /haoke/rmq/rmqbroker/conf/broker.conf
brokerIP1=172.16.55.185
namesrvAddr=172.16.55.185:9876
brokerName=broker_haoke_im

创建broker容器的时候,读取broker.conf文件:

#创建broker容器
docker create -p 10911:10911 -p 10909:10909 --name rmqbroker \
-e "JAVA_OPTS=-Duser.home=/opt" \
-e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" \
-v /haoke/rmq/rmqbroker/conf/broker.conf:/etc/rocketmq/broker.conf \
-v /haoke/rmq/rmqbroker/logs:/opt/logs \
-v /haoke/rmq/rmqbroker/store:/opt/store \
foxiswho/rocketmq:broker-4.3.2

在Broker日志中查看Broker和Name Server的地址,判断Broker是否能正确连接到Name Server。

The broker[broker_haoke_im, 192.168.25.114:10911] boot success. serializeType=JSON and name server is 192.168.25.114:9876

排查3:Producer 没有正确连接到 Name Server 
当时一时糊涂 Linux 上开启了 Broker 端口是 9876,然后自己并没有在防护墙中开启此端口,所以导致客户端 Producer 无论如何也连接不上
如下所示,命令分别含义是:

  1. 查看防护墙开放的端口,此时没有 9876
  2. 设置防火墙开放9876 端口
  3. 重新加载 firewall,修改配置后,必须重新加载才能生效:firewall-cmd --reload 
  4. 再次查看防火墙开放的端口,此时已有 9876
[root@localhost apache-rocketmq]# firewall-cmd --zone=public --list-ports
8090/tcp 80/tcp 8080/tcp
[root@localhost apache-rocketmq]# firewall-cmd --zone=public --add-port=9876/tcp --permanent
success
[root@localhost apache-rocketmq]# firewall-cmd --reload
success
[root@localhost apache-rocketmq]# firewall-cmd --zone=public --list-ports
9876/tcp 8090/tcp 80/tcp 8080/tcp
[root@localhost apache-rocketmq]

2.2 RemotingTooMuchRequestException: sendDefaultImpl call timeout

异常说明:

从 Windows 上连接虚拟机中的 nameServer 时要经过 Linux 系统的防火墙,而防火墙一般都会有超时的机制,在网络连接长时间不传输数据时,会关闭这个 TCP 的会话,关闭后再读写,就有可能导致这个异常。

解决办法:

关闭防火墙:

systemctl stop firewalld

 

你可能感兴趣的:(SOA架构)