搭建 RocketMQ服務器,這是我見過“最便捷”的方法!


搭建 RocketMQ服務器,這是我見過“最便捷”的方法!

最近學習使用 rocketmq,需要搭建 rocketmq 服務端,本文主要記錄 rocketmq 搭建過程以及這個過程踩到的一些坑。至於有多簡單呢,在本機已有Docker環境的情況下只需要三步即可。

  1. 從github上面拉取項目
  2. 修改broker.conf中的brokerIP1 參數,修改為本機IP
  3. 進入docker-compose.yml文件所在路徑,執行docker-compose up命令即可

前言

首先我們是使用Docker進行搭建環境的,所以我們先要在自己機器上的安裝Docker,具體的安裝過程以及對於Docker的介紹官方文檔裡面說的很清楚了https://docs.docker.com/get-started/。

我們要搭建RocketMQ服務器,那麼我們就要知道大概搭建RocketMQ服務器需要部署哪些東西。對於RocketMQ有一個架構圖,如下所示。而圖中所示的Producer(生產者)和Consumer(消費者)無需我們搭建,因為那是作為一個服務器進行啟動的。nameserver就是一個註冊中心一樣組件,我們可以將其簡單理解成springcloud中的Eureka,那麼nameserver是需要我們搭建的。broker就是真正處理消息的地方,也是需要我們搭建的。

搭建 RocketMQ服務器,這是我見過“最便捷”的方法!

正常情況我們搭建上面所提到的兩個組件其實就能已經能夠滿足我們的發送接收消息的需求了。但是通常情況下我們還需要搭建一個Web可視化的平臺用來查看MQ的服務狀態、消息的消費情況、主題的隊列配置等等。這裡使用rocketmq-console。同樣也是通過Docker來進行安裝。

部署

上面我們提到了需要安裝三個組件,那麼這三個組件又是需要能夠互相通信連接的,考慮到分開部署進行配置連接信息比較麻煩,於是這裡我們採用docker-compose進行配置部署。

首先我們需要創建docker-compose.yml配置文件。文件內容如下

<code>version: '3.5'
services:
rmqnamesrv:
image: foxiswho/rocketmq:server
container_name: rmqnamesrv
ports:
- 9876:9876
volumes:
- ./logs:/opt/logs
- ./store:/opt/store
networks:
rmq:
aliases:
- rmqnamesrv

rmqbroker:
image: foxiswho/rocketmq:broker
container_name: rmqbroker
ports:
- 10909:10909

- 10911:10911
volumes:
- ./logs:/opt/logs
- ./store:/opt/store
- ./conf/broker.conf:/etc/rocketmq/broker.conf
environment:
NAMESRV_ADDR: "rmqnamesrv:9876"
JAVA_OPTS: " -Duser.home=/opt"
JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
command: mqbroker -c /etc/rocketmq/broker.conf
depends_on:
- rmqnamesrv
networks:
rmq:
aliases:
- rmqbroker

rmqconsole:
image: styletang/rocketmq-console-ng
container_name: rmqconsole
ports:
- 8080:8080
environment:
JAVA_OPTS: "-Drocketmq.namesrv.addr=rmqnamesrv:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false"
depends_on:
- rmqnamesrv
networks:
rmq:
aliases:
- rmqconsole

networks:
rmq:
name: rmq
driver: bridge
/<code>

然後在與docker-compose.yml同級下面相應的建立三個文件夾conf、logs、store。然後在conf文件夾下面建立broker.conf配置文件,所有文件的目錄位置如下所示。

<code>docker-compose.yml
conf
\t- broker.conf
logs
store
/<code>

然後在編寫broker.conf配置文件裡面的內容

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

# broker 名字,注意此處不同的配置文件填寫的不一樣,如果在 broker-a.properties 使用: broker-a,
# 在 broker-b.properties 使用: broker-b
brokerName=broker-a

# 0 表示 Master,> 0 表示 Slave
brokerId=0

# nameServer地址,分號分割
# namesrvAddr=rocketmq-nameserver1:9876;rocketmq-nameserver2:9876

# 啟動IP,如果 docker 報 com.alibaba.rocketmq.remoting.exception.RemotingConnectException: connect to <192.168.0.120:10909> failed
# 解決方式1 加上一句 producer.setVipChannelEnabled(false);,解決方式2 brokerIP1 設置宿主機IP,不要使用docker 內部IP
brokerIP1=192.168.1.16

# 在發送消息時,自動創建服務器不存在的topic,默認創建的隊列數
defaultTopicQueueNums=4

# 是否允許 Broker 自動創建 Topic,建議線下開啟,線上關閉 !!!這裡仔細看是 false,false,false
autoCreateTopicEnable=true

# 是否允許 Broker 自動創建訂閱組,建議線下開啟,線上關閉
autoCreateSubscriptionGroup=true

# Broker 對外服務的監聽端口
listenPort=10911

# 刪除文件時間點,默認凌晨4點
deleteWhen=04

# 文件保留時間,默認48小時
fileReservedTime=120

# commitLog 每個文件的大小默認1G
mapedFileSizeCommitLog=1073741824

# ConsumeQueue 每個文件默認存 30W 條,根據業務情況調整
mapedFileSizeConsumeQueue=300000

# destroyMapedFileIntervalForcibly=120000
# redeleteHangedFileInterval=120000
# 檢測物理文件磁盤空間
diskMaxUsedSpaceRatio=88
# 存儲路徑
# storePathRootDir=/home/ztztdata/rocketmq-all-4.1.0-incubating/store
# commitLog 存儲路徑
# storePathCommitLog=/home/ztztdata/rocketmq-all-4.1.0-incubating/store/commitlog
# 消費隊列存儲
# storePathConsumeQueue=/home/ztztdata/rocketmq-all-4.1.0-incubating/store/consumequeue
# 消息索引存儲路徑
# storePathIndex=/home/ztztdata/rocketmq-all-4.1.0-incubating/store/index
# checkpoint 文件存儲路徑
# storeCheckpoint=/home/ztztdata/rocketmq-all-4.1.0-incubating/store/checkpoint
# abort 文件存儲路徑

# abortFile=/home/ztztdata/rocketmq-all-4.1.0-incubating/store/abort
# 限制的消息大小
maxMessageSize=65536

# flushCommitLogLeastPages=4
# flushConsumeQueueLeastPages=2
# flushCommitLogThoroughInterval=10000
# flushConsumeQueueThoroughInterval=60000

# Broker 的角色
# - ASYNC_MASTER 異步複製Master
# - SYNC_MASTER 同步雙寫Master
# - SLAVE
brokerRole=ASYNC_MASTER

# 刷盤方式
# - ASYNC_FLUSH 異步刷盤
# - SYNC_FLUSH 同步刷盤
flushDiskType=ASYNC_FLUSH

# 發消息線程池數量
# sendMessageThreadPoolNums=128
# 拉消息線程池數量
# pullMessageThreadPoolNums=128
/<code>

配置文件中的內容我們只需要改動一點即可,即brokerIP1 這個屬性,我們將其更改為我們本機的ip,可以利用ipconfig進行查看。

修改完以後我們直接在docker-compose.yml文件所在的位置輸入命令docker-compose up即可啟動。啟動成功以後在瀏覽器中輸入http://localhost:8080/即可看到管理頁面,就表示我們搭建成功了。

搭建 RocketMQ服務器,這是我見過“最便捷”的方法!

使用Springboot快速上手

這裡將會使用 springboot 快速上手使用 mq,將會使用rocketmq-spring-boot-starter模塊。

pom 配置如下

<code>
<dependency>
<groupid>org.apache.rocketmq/<groupid>
<artifactid>rocketmq-spring-boot-starter/<artifactid>
<version>2.0.3/<version>
/<dependency>
/<code>

gradle配置如下

<code>implementation 'org.apache.rocketmq:rocketmq-spring-boot-starter:2.0.3'
/<code>

消費服務發送方配置如下:

<code>## application.properties
rocketmq.name-server=ip:9876
rocketmq.producer.group=my-group
/<code>

消費服務發送方程序如下:

<code>@SpringBootApplication
public class ProducerApplication implements CommandLineRunner {
@Resource
private RocketMQTemplate rocketMQTemplate;

public static void main(String[] args){
SpringApplication.run(ProducerApplication.class, args);
}

public void run(String... args) throws Exception {
rocketMQTemplate.convertAndSend("test-topic-1", "Hello, World!");
rocketMQTemplate.send("test-topic-1", MessageBuilder.withPayload("Hello, World! I'm from spring message").build());
}


}
/<code>

這裡圖省事的話可以將消費者和生產者寫道同一個項目中。

消息消費方配置如下:

<code>## application.properties
rocketmq.name-server=ip:9876
/<code>

消息消費方運行程序如下:

<code>@SpringBootApplication
public class ConsumerApplication{

public static void main(String[] args){
SpringApplication.run(ConsumerApplication.class, args);
}

@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-1", consumerGroup = "my-consumer_test-topic-1")
public static class MyConsumer1 implements RocketMQListener<string> {
public void onMessage(String message) {
log.info("received message: {}", message);
}
}
}
/<string>/<code>

到現在為止我們就可以在本機上快樂的試驗各種關於RocketMQ的相關東西了。

RocketMQ的Docker配置文件存放處

RocketMQ的Docker配置文件存放處

RocketMQ的Docker配置文件存放處

大家可以直接從上面拉取項目,啟動RocketMQ只需要兩步。

  1. 修改broker.conf中的brokerIP1 參數,修改為本機IP
  2. 進入docker-compose.yml文件所在路徑,執行docker-compose up命令即可

如果大家不想自己搭建Springboot項目的話,可以從https://github.com/modouxiansheng/Doraemon上面直接拉取下來就行了。

鏈接:https://my.oschina.net/u/4030990/blog/3232512


分享到:


相關文章: