使用docker安装RocketMQ

本文档介绍了如何使用Docker在本地安装并部署RocketMQ,包括从GitHub克隆RocketMQ-docker项目,构建镜像,启动单机模式的RocketMQ实例,验证启动成功,以及配置外网访问。同时,还展示了如何升级RocketMQ版本和安装RocketMQ控制台。此外,提供了Go语言客户端的简单示例,演示如何发送和消费消息。
摘要由CSDN通过智能技术生成

使用docker安装RocketMQ

官网地址

1、下载

# git clone https://github.com/apache/rocketmq-docker.git
# cd rocketmq-docker/
# ls
CONTRIBUTING.md  image-build  LICENSE  NOTICE  product  README.md  stage.sh  templates
# cd image-build/
# ls
build-image.sh  Dockerfile-alpine  Dockerfile-centos  scripts  update.sh

2、创建RocketMQ镜像

构建命令: sh build-image.sh RMQ-VERSION BASE-IMAGE
可选RMQ-VERSION

我们使用 4.7.1 alpine

➜  image-build git:(master)  sh build-image.sh 4.7.1 alpine
Version = 4.7.1
Sending build context to Docker daemon  27.14kB
Step 1/22 : FROM openjdk:8-alpine
 ---> a3562aa0b991

//..........构建时间较长请耐心等待

//构建完成
Successfully built 128108c2e50d
Successfully tagged apacherocketmq/rocketmq:4.7.1-alpine

构建完成后,我们就能查询到镜像

# docker images |grep mq
apacherocketmq/rocketmq  4.7.1-alpine   128108c2e50d     4   9 seconds ago      145MB

生成配置

# cd ..
# ls
CONTRIBUTING.md  image-build  LICENSE  NOTICE  product  README.md  stage.sh  templates
# sh stage.sh 4.7.1 (这里的4.7.1对应之前的镜像版本)
Stage version = 4.7.1
mkdir /root/rocketmq/rocketmq-docker/stages/4.7.1
staged templates into folder /root/rocketmq/rocketmq-docker/stages/4.7.1
# ls
CONTRIBUTING.md  image-build  LICENSE  NOTICE  product  README.md  stages  stage.sh  templates

生成了stages目录,里面存放配置模板文件

# cd stages/
# ls
4.7.1
# cd 4.7.1/
# ls
data            kubernetes        play-docker-compose.sh  play-docker.sh      play-kubernetes.sh  ssl
docker-compose  play-consumer.sh  play-docker-dledger.sh  play-docker-tls.sh  play-producer.sh

部署方式有以下5种

  1. 单机
./play-docker.sh alpine
  1. docker-composer
./play-docker-compose.sh
  1. kubernetes集群
./play-kubernetes.sh
  1. Cluster of Dledger storage(RocketMQ需要4.4.0版本以上)
./play-docker-dledger.sh
  1. TLS
./play-docker-tls.sh
./play-producer.sh
./play-consumer.sh

我这里选择的是单机部署,可以看到生成了两个容器

# docker ps |grep mq
5b557ea1e6be        apacherocketmq/rocketmq:4.7.1-alpine                         "sh mqbroker"            25 seconds ago                                                                                      Up 24 seconds       0.0.0.0:10909->10909/tcp, 9876/tcp, 0.0.0.0:10911-10912->10911-10912/tcp   rmqbroker
8b1318aee5d6        apacherocketmq/rocketmq:4.7.1-alpine                         "sh mqnamesrv"           26 seconds ago                                                                                      Up 25 seconds       10909/tcp, 0.0.0.0:9876->9876/tcp, 10911-10912/tcp                         rmqnamesrv

验证RocketMQ启动成功

1、使用命令 docker ps|grep rmqbroker找到RocketMQ broker的容器id

2、使用命令 docker exec -it 5b557ea1e6be ./mqadmin clusterList -n nameserver_ip:9876 验证RocketMQ broker工作正常

nameserver_ip请进入容器rmqnamesrv内部查看

# docker exec -it 5b557ea1e6be ./mqadmin clusterList -n 172.17.0.2:9876
RocketMQLog:WARN No appenders could be found for logger (io.netty.util.internal.PlatformDependent0).
RocketMQLog:WARN Please initialize the logger system properly.
#Cluster Name     #Broker Name            #BID  #Addr                  #Version                #InTPS(LOAD)       #OutTPS(LOAD) #PCWait(ms) #Hour #SPACE
DefaultCluster    5b557ea1e6be            0     172.17.0.8:10911       V4_7_1                   0.00(0,0ms)         0.00(0,0ms)          0 447225.46 -1.0000

如需升级执行以下命令

cd image-build
./update.sh

安装RocketMQ控制台

//拉取镜像
docker pull apacherocketmq/rocketmq-console:2.0.0
//运行容器,这里172.15.65.xx为宿主机ip
docker run -e "JAVA_OPTS=-Drocketmq.namesrv.addr=172.15.65.xx:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false" -p 6881:8080 -t apacherocketmq/rocketmq-console:2.0.0

启动容器后可以通过 http://localhost:6881/#/ 登录控制台

golang client使用问题

由于使用的docker服务启动,broker的地址是内网地址,需要将地址修改为外网地址

# docker ps |grep mq
8abb966542a3        apacherocketmq/rocketmq-console:2.0.0                        "sh -c 'java $JAVA..."   17 hours ago        Up 17 hours         0.0.0.0:6881->8080/tcp                                                     dazzling_tesla
5b557ea1e6be        apacherocketmq/rocketmq:4.7.1-alpine                         "sh mqbroker"            18 hours ago        Up 18 hours         0.0.0.0:10909->10909/tcp, 9876/tcp, 0.0.0.0:10911-10912->10911-10912/tcp   rmqbroker
8b1318aee5d6        apacherocketmq/rocketmq:4.7.1-alpine                         "sh mqnamesrv"           18 hours ago        Up 18 hours         10909/tcp, 0.0.0.0:9876->9876/tcp, 10911-10912/tcp                         rmqnamesrv
# docker exec -it 5b557ea1e6be bash // 进入到容器内部修改配置
# vi ../conf/broker.conf

在文件中添加外网地址 brokerIP1=172.15.65.xx

# 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.

brokerClusterName = DefaultCluster
brokerName = broker-a
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH

# add your IP
brokerIP1=172.15.65.xx

然后重启broker, docker restart 5b557ea1e6be

这里需要去修改启动脚本 ./play-docker.sh 里的start_namesrv_broker() 函数中的docker启动命令,在mybroker后面添加-c …/conf/broker.conf

这样查看cluster会发现Address变成了外网地址。

go中运行

获取go-client:go get -u -v github.com/apache/rocketmq-client-go/v2@v2.0.0

client-go producer-simple

https://github.com/apache/rocketmq-client-go/blob/v2.0.0/examples/producer/simple/main.go

/*
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.
*/

package main

import (
	"context"
	"fmt"
	"os"
	"strconv"

	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
)

// Package main implements a simple producer to send message.
func main() {
	p, _ := rocketmq.NewProducer(
		producer.WithNameServer([]string{"127.0.0.1:9876"}),
		producer.WithRetry(2),
	)
	err := p.Start()
	if err != nil {
		fmt.Printf("start producer error: %s", err.Error())
		os.Exit(1)
	}
	topic := "test"

	for i := 0; i < 10; i++ {
		msg := &primitive.Message{
			Topic: topic,
			Body:  []byte("Hello RocketMQ Go Client! " + strconv.Itoa(i)),
		}
		res, err := p.SendSync(context.Background(), msg)

		if err != nil {
			fmt.Printf("send message error: %s\n", err)
		} else {
			fmt.Printf("send message success: result=%s\n", res.String())
		}
	}
	err = p.Shutdown()
	if err != nil {
		fmt.Printf("shutdown producer error: %s", err.Error())
	}
}

consumer

https://github.com/apache/rocketmq-client-go/blob/v2.0.0/examples/consumer/simple/main.go

/*
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.
*/

package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/consumer"
	"github.com/apache/rocketmq-client-go/v2/primitive"
)

func main() {
	c, _ := rocketmq.NewPushConsumer(
		consumer.WithGroupName("testGroup"),
		consumer.WithNameServer([]string{"127.0.0.1:9876"}),
	)
	err := c.Subscribe("test", consumer.MessageSelector{}, func(ctx context.Context,
		msgs ...*primitive.MessageExt) (consumer.ConsumeResult, error) {
		for i := range msgs {
			fmt.Printf("subscribe callback: %v \n", msgs[i])
		}

		return consumer.ConsumeSuccess, nil
	})
	if err != nil {
		fmt.Println(err.Error())
	}
	// Note: start after subscribe
	err = c.Start()
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(-1)
	}
	time.Sleep(time.Hour)
	err = c.Shutdown()
	if err != nil {
		fmt.Printf("shutdown Consumer error: %s", err.Error())
	}
}

Docker是一种容器化平台,可以帮助开发者将应用程序及其依赖项打包成一个独立的容器,以便在不同的环境中进行部署和运行。而RocketMQ是一款开源的分布式消息中间件,具有高吞吐量、高可用性和可伸缩性的特点。 要在Docker安装RocketMQ,可以按照以下步骤进行操作: 1. 首先,确保你已经安装DockerDocker Compose。你可以在Docker官方网站上找到相应的安装指南。 2. 创建一个新的目录,用于存放RocketMQ的配置文件和数据。 3. 在该目录下创建一个名为`docker-compose.yml`的文件,并将以下内容复制到文件中: ```yaml version: '3' services: namesrv: image: rocketmqinc/rocketmq:4.8.0 container_name: rocketmq_namesrv ports: - 9876:9876 volumes: - ./data/namesrv/logs:/root/logs - ./data/namesrv/store:/root/store command: sh mqnamesrv broker: image: rocketmqinc/rocketmq:4.8.0 container_name: rocketmq_broker ports: - 10909:10909 - 10911:10911 environment: - NAMESRV_ADDR=namesrv:9876 volumes: - ./data/broker/logs:/root/logs - ./data/broker/store:/root/store command: sh mqbroker -c /opt/rocketmq-4.8.0/conf/broker.conf console: image: styletang/rocketmq-console-ng container_name: rocketmq_console ports: - 8080:8080 environment: - JAVA_OPTS=-Drocketmq.namesrv.addr=namesrv:9876 depends_on: - namesrv ``` 4. 保存并关闭`docker-compose.yml`文件。 5. 在终端中进入到该目录,并执行以下命令启动RocketMQ容器: ```bash docker-compose up -d ``` 6. 等待一段时间,RocketMQ容器将会启动并运行。你可以通过访问`http://localhost:8080`来访问RocketMQ的控制台。 至此,你已经成功在Docker安装RocketMQ。你可以使用RocketMQ的客户端工具或者编程语言的RocketMQ SDK来进行消息的发送和接收。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值