建站不啰嗦,上手跟我做(三十四)Rocketmq 安装

安装 Docker

建站不啰嗦,上手跟我做(二十二)Docker 使用

安装 RocketMQ

有两种方法可以安装 RocketMQ:使用官方镜像或者自己构建镜像。

使用官方镜像

查询官方镜像

[root@localhost ~]# docker search rocketmq
INDEX       NAME                                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/foxiswho/rocketmq                    rocketmq                                        88               
docker.io   docker.io/apache/rocketmq                                                                      87               
docker.io   docker.io/rocketmqinc/rocketmq                 Image repository for Apache RocketMQ            55               
docker.io   docker.io/styletang/rocketmq-console-ng        rocketmq-console-ng                             41               
docker.io   docker.io/xuchengen/rocketmq                   单机版Apache RocketMQ方便开发者一键部署开发环境,已内置Dashboa...   31               
docker.io   docker.io/apacherocketmq/rocketmq              Docker Image for Apache RocketMQ                23               
docker.io   docker.io/rocketmqinc/rocketmq-broker          Customized RocketMQ Broker Image for Rocke...   14               
docker.io   docker.io/apacherocketmq/rocketmq-dashboard    Docker Image for Apache RocketMQ Dashboard      11               
docker.io   docker.io/king019/rocketmq                     rocketmq                                        9                
docker.io   docker.io/rocketmqinc/rocketmq-namesrv         Customized RocketMQ Name Server Image for ...   9                
docker.io   docker.io/apacherocketmq/rocketmq-broker       Apache RocketMQ docker image for broker.        8                
docker.io   docker.io/apacherocketmq/rocketmq-console      This repo has moved to rocketmq-dashboard,...   8                
docker.io   docker.io/apacherocketmq/rocketmq-nameserver   Apache RocketMQ docker image for name server.   5                
docker.io   docker.io/pangliang/rocketmq-console-ng        Deprecated  use apacherocketmq/rocketmq-da...   4                
docker.io   docker.io/dyrnq/rocketmq                       This is a rocketmq docker image based Adop...   3                
docker.io   docker.io/apacherocketmq/rocketmq-operator     RocketMQ Operator is to manage RocketMQ se...   2                
docker.io   docker.io/toyangdon/rocketmq-broker                                                            2                
docker.io   docker.io/2019liurui/rocketmq-broker           RocketMQ broker image for RocketMQ-Operator     1                
docker.io   docker.io/slpcat/rocketmq-exporter             https://github.com/slpcat/docker-images/tr...   1                
docker.io   docker.io/aaronai/rocketmq-proxy-it                                                            0                
docker.io   docker.io/apache/rocketmq-ci                                                                   0                
docker.io   docker.io/apache/rocketmq-operator             Apache RocketMQ Operator                        0                
docker.io   docker.io/foxundermoon/rocketmq-3.2.6          old   version for inner usage for  rocketmq     0                
docker.io   docker.io/pureinsights/rocketmq-broker                                                         0                
docker.io   docker.io/rocketmqinc/rocketmq-operator        The Kubernetes operator for RocketMQ            0

拉取官方镜像

[root@localhost ~]# docker pull docker.io/rocketmqinc/rocketmq 
Using default tag: latest
Trying to pull repository docker.io/rocketmqinc/rocketmq ... 
latest: Pulling from docker.io/rocketmqinc/rocketmq
a02a4930cb5d: Pull complete 
90eb47c86343: Pull complete 
43bf660f95f6: Pull complete 
7f5ca7a20245: Pull complete 
707a618112e3: Pull complete 
4b527473eafb: Pull complete 
fedd1d06c8ba: Pull complete 
27782fcf3aef: Pull complete 
Digest: sha256:b08556227ad8d169b7927f4c48335877cb10060e6781374ce5a644ea29f6fbe1
Status: Downloaded newer image for docker.io/rocketmqinc/rocketmq:latest
[root@localhost ~]# docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             SIZE
docker.io/rocketmqinc/rocketmq   latest              09bbc30a03b6        4 years ago         380 MB

启动 nameserver

[root@localhost opt]# sudo docker run -d --name rmqnamesrv -p 9876:9876 docker.io/rocketmqinc/rocketmq sh mqnamesrv
484cd8209452552e42cac90a385abe37d8e333424ff476421c89001ca155df5e
[root@localhost opt]# docker ps
CONTAINER ID        IMAGE                            COMMAND             CREATED             STATUS              PORTS                                          NAMES
484cd8209452        docker.io/rocketmqinc/rocketmq   "sh mqnamesrv"      4 seconds ago       Up 3 seconds        10909/tcp, 0.0.0.0:9876->9876/tcp, 10911/tcp   rmqnamesrv

其中:

  • -d:在后台启动容器
  • --name rmqnamesrv:指定容器名称为 rmqnamesrv
  • -p 9876:9876:将容器内的 9876 端口映射到主机的 9876 端口
  • link rmqnamesrv:namesrv:将 rmqnamesrv 容器与 rmqbroker 容器进行关联
  • -p 10911:10911 -p 10909:10909:将容器内的 10911 和 10909 端口映射到主机的相应端口
  • -e "JAVA_OPTS=-Duser.home=/opt" -e "JAVA_OPT_EXT=-server -Xms1g -Xmx1g -Xmn256m":设置容器启动参数
  • sh bin/mqnamesrvsh bin/mqbroker -n namesrv:9876:指定容器启动命令

注:在新版本的 RocketMQ 中,Namesrv 已经支持 cluster 模式,可以启动多个 Namesrv 实例进行 HA 高可用,Broker 的配置文件中也支持 HA 高可用,可以通过配置文件中的autoCreateTopicEnable开启自动创建 Topic 功能。

启动 broker

[root@localhost opt]# docker run -d --name rmqbroker --link rmqnamesrv:namesrv -p 10911:10911 -p 10909:10909 -e "NAMESRV_ADDR=192.168.56.103:9876" -e "AUTO_CREATE_TOPIC_ENABLE=true" -e "AUTO_CREATE_TOPIC_DEFAULT_PARTITIONS=4" docker.io/rocketmqinc/rocketmq sh mqbroker -n 192.168.56.103:9876
6bb584926cd7015427c3a09ee6a3530aa81cd3c5165dda72649765ae972b780f
[root@localhost opt]# docker ps
CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS                                                          NAMES
6bb584926cd7        docker.io/rocketmqinc/rocketmq   "sh mqbroker -n na..."   4 seconds ago       Up 4 seconds        0.0.0.0:10909->10909/tcp, 9876/tcp, 0.0.0.0:10911->10911/tcp   rmqbroker
484cd8209452        docker.io/rocketmqinc/rocketmq   "sh mqnamesrv"           41 seconds ago      Up 40 seconds       10909/tcp, 0.0.0.0:9876->9876/tcp, 10911/tcp                   rmqnamesrv

重启后启动 nameserver 和 broker

注意先启动 nameserver,然后启动 broker

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS                            PORTS                                                          NAMES
6bb584926cd7        docker.io/rocketmqinc/rocketmq   "sh mqbroker -n na..."   4 seconds ago       Up 4 seconds        0.0.0.0:10909->10909/tcp, 9876/tcp, 0.0.0.0:10911->10911/tcp   rmqbroker
484cd8209452        docker.io/rocketmqinc/rocketmq   "sh mqnamesrv"           41 seconds ago      Up 40 seconds       10909/tcp, 0.0.0.0:9876->9876/tcp, 10911/tcp                   rmqnamesrv
[root@localhost ~]# docker restart 6bb584926cd7
Error response from daemon: Cannot link to a non running container: /rmqnamesrv AS /rmqbroker/namesrv
Error: failed to start containers: 59ded41503cf
[root@localhost ~]# docker restart 59ded41503cf
c68758b8a4d6
[root@localhost ~]# docker restart 6bb584926cd7
6bb584926cd7
[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS                                                          NAMES
6bb584926cd7        docker.io/rocketmqinc/rocketmq   "sh mqbroker -n na..."   4 seconds ago       Up 4 seconds        0.0.0.0:10909->10909/tcp, 9876/tcp, 0.0.0.0:10911->10911/tcp   rmqbroker
484cd8209452        docker.io/rocketmqinc/rocketmq   "sh mqnamesrv"           41 seconds ago      Up 40 seconds       10909/tcp, 0.0.0.0:9876->9876/tcp, 10911/tcp                   rmqnamesrv

构建自己的镜像

  1. 下载 RocketMQ 源码
git clone https://github.com/apache/rocketmq.git
  1. 构建镜像
cd rocketmq
mvn -Prelease-all -DskipTests clean install
cd docker
docker-compose up

验证安装

控制台镜像下载

[root@localhost ~]# docker pull docker.io/styletang/rocketmq-console-ng
Using default tag: latest
Trying to pull repository docker.io/styletang/rocketmq-console-ng ... 
latest: Pulling from docker.io/styletang/rocketmq-console-ng
e12c678537ae: Pull complete 
8d9ed335b7db: Pull complete 
3318dd58ae60: Pull complete 
624ba6156166: Pull complete 
c7a02d193df7: Pull complete 
813b62320378: Pull complete 
49e2d6393f32: Pull complete 
0a2b7222259b: Pull complete 
478657249124: Pull complete 
5f8636e9fbb7: Pull complete 
Digest: sha256:96e99d10eda871640b9529b75da30fa6ca85fc15722661acb9660639e6904fc3
Status: Downloaded newer image for docker.io/styletang/rocketmq-console-ng:latest
[root@localhost ~]# docker images
REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
docker.io/rocketmqinc/rocketmq            latest              09bbc30a03b6        4 years ago         380 MB
docker.io/styletang/rocketmq-console-ng   latest              7df83bb6e638        5 years ago         702 MB

控制台启动

[root@localhost opt]# sudo docker run -d --name rmqconsole -p 8080:8080 -e "rocketmq.config.namesrvAddr=192.168.56.103:9876" docker.io/styletang/rocketmq-console-ng
3fa2ca80c7e5352c3e45522367ade8249f0a79f8a9978d18725b54e7a3a0913e
[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE                                     COMMAND                  CREATED             STATUS              PORTS                                                          NAMES
3fa2ca80c7e5        docker.io/styletang/rocketmq-console-ng   "sh -c 'java $JAVA..."   5 seconds ago       Up 4 seconds        0.0.0.0:8080->8080/tcp                                         rmqconsole
6bb584926cd7        docker.io/rocketmqinc/rocketmq            "sh mqbroker -n na..."   9 minutes ago       Up 9 minutes        0.0.0.0:10909->10909/tcp, 9876/tcp, 0.0.0.0:10911->10911/tcp   rmqbroker
484cd8209452        docker.io/rocketmqinc/rocketmq            "sh mqnamesrv"           10 minutes ago      Up 9 minutes        10909/tcp, 0.0.0.0:9876->9876/tcp, 10911/tcp                   rmqnamesrv
其中:

- `-d`:在后台启动容器
- `--name rmqconsole`:指定容器名称为rmqconsole
- `-p 8080:8080`:将容器内的8080端口映射到主机的8080端口
- `-e "rocketmq.config.namesrvAddr=<namesrv_ip>:9876"`:指定RocketMQ Namesrv的IP和端口
- `pangliang/rocketmq-console-ng:2.0.0`:指定容器的镜像名称和版本号

注:在上面的命令中,`<namesrv_ip>`需要替换成实际的RocketMQ Namesrv的IP地址。
484cd8209452552e42cac90a385abe37d8e333424ff476421c89001ca155df5e

通过 Web 控制台或者命令行工具验证 RocketMQ 是否安装成功。

Web 控制台

在浏览器中输入http://192.168.56.103:8080/,出现相应的控制台页面,即表示安装成功。

总结

Docker 可以帮助我们快速安装 RocketMQ,提高开发效率,加快项目部署。在实际使用过程中,我们可以根据项目需求进行相关配置,以满足不同的业务需求。

异常处理

[root@localhost ~]# docker run -d -p 18018:18018 -e "rocketmq.config.namesrvAddr=192.168.56.103:9876" docker.io/styletang/rocketmq-console-ng
016d5770fe4878206f6030b5dab1b607395cdea88983115bab9ccfb5892ace6b
/usr/bin/docker-current: Error response from daemon: driver failed programming external connectivity on endpoint infallible_bose (c9273752eaa32125d1fabb22f1e63e58f310ac56694c7ee4473e0ea15c0cb420):  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 18018 -j DNAT --to-destination 172.17.0.4:18018 ! -i docker0: iptables: No chain/target/match by that name.
 (exit status 1)).

这个错误可以看出是 iptables 防火墙配置的问题,可能是 iptables 并没有为容器分配正确的转发规则。建议先尝试将 iptables 重置为默认状态:

sudo iptables -F
sudo iptables -t nat -F
sudo iptables -t mangle -F
sudo iptables -X

然后重启 Docker 服务:

sudo systemctl restart docker

namesrv 对应的 ip

[root@localhost opt]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:88:ef:61 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.103/24 brd 192.168.56.255 scope global noprefixroute dynamic enp0s8
       valid_lft 576sec preferred_lft 576sec
    inet6 fe80::8afe:5dbd:e19d:88fe/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:a3:a9:32:1e brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:a3ff:fea9:321e/64 scope link 
       valid_lft forever preferred_lft forever

namesrv 的 ip 为 docker0 对应的 ip

发送消息失败

Caused by: org.apache.rocketmq.client.exception.MQClientException: The specified topic[测试] contains illegal characters, allowing only ^[%|a-zA-Z0-9_-]+$
For more information, please visit the url, http://rocketmq.apache.org/docs/faq/
        at org.apache.rocketmq.client.Validators.checkTopic(Validators.java:125)
        at org.apache.rocketmq.client.Validators.checkMessage(Validators.java:97)
        at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.sendDefaultImpl(DefaultMQProducerImpl.java:436)
        at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1049)
        at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1008)
        at org.apache.rocketmq.client.producer.DefaultMQProducer.send(DefaultMQProducer.java:204)
        at org.apache.rocketmq.console.service.impl.TopicServiceImpl.sendTopicMessageRequest(TopicServiceImpl.java:204)
        ... 67 common frames omitted

这个异常表示指定的 RocketMQ 主题(Topic)名字包含非法字符。按照 RocketMQ 规范,主题名只允许包含以下字符:

  • 大小写字母 a-z 或 A-Z
  • 数字 0-9
  • 下划线 _
  • 短横线 -
  • 百分号 %
    如果主题名包含其他字符,就会出现该异常。

图片.png

图片.png

上一篇 建站不啰嗦,上手跟我做(三十三)Jenkins 安装
目录