docker安裝RocketMQ的實現步驟
一、檢索鏡像
docker search rocketmq
檢索具體版本
curl https://registry.hub.docker.com/v1/repositories/foxiswho/rocketmq/tags | tr -d '[\[\]" ]' | tr '}' '\n' | awk -F: -v image='foxiswho/rocketmq' '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}'
查看當前鏡像所有的版本shell命令
curl https://registry.hub.docker.com/v1/repositories/foxiswho/rocketmq/tags\ | tr -d '[\[\]" ]' | tr '}' '\n'\ | awk -F: -v image='foxiswho/rocketmq' '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}'
二、創建Broker Server
docker run -d -p 9876:9876 --name rmqserver foxiswho/rocketmq:server-4.5.1
三、創建broker
docker run -d -p 10911:10911 -p 10909:10909\ --name rmqbroker --link rmqserver:namesrv\ -e "NAMESRV_ADDR=namesrv:9876" -e "JAVA_OPTS=-Duser.home=/opt"\ -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m"\ foxiswho/rocketmq:broker-4.5.1
配置broker.conf
進入rmqbroker容器
docker exec -it rmqbroker /bin/bash cd /etc/rocketmq/ vi broker.conf
四、創建rocketmq console
docker run -d --name rmqconsole -p 8180:8080 --link rmqserver:namesrv\ -e "JAVA_OPTS=-Drocketmq.namesrv.addr=namesrv:9876\ -Dcom.rocketmq.sendMessageWithVIPChannel=false"\ -t styletang/rocketmq-console-ng
然後通過如下命令檢查一下啟動情況
docker ps|grep rocketmq
http://192.168.23.131:8180/
五、測試
由於我在宿主機安裝rocketmq,測試如下:
#測試消息發送命令 sh tools.sh org.apache.rocketmq.example.quickstart.Producer
#測試消息接收命令 sh tools.sh org.apache.rocketmq.example.quickstart.Consumer
六、java樣例
<dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>4.9.2</version> </dependency>
消息發送(Producer端發送同步消息)
import org.apache.rocketmq.client.producer.DefaultMQProducer; import org.apache.rocketmq.client.producer.SendResult; import org.apache.rocketmq.common.message.Message; import org.apache.rocketmq.remoting.common.RemotingHelper; public class SyncProducer { public static void main(String[] args) throws Exception { // 實例化消息生產者Producer DefaultMQProducer producer = new DefaultMQProducer("zhuzeqing-1"); // 設置NameServer的地址 producer.setNamesrvAddr("192.168.23.131:9876"); // 啟動Producer實例 producer.start(); for (int i = 0; i < 1; i++) { // 創建消息,並指定Topic,Tag和消息體 Message msg = new Message("zhuzeqing-1-Topic" /* Topic */, "TagA" /* Tag */, ("hello,rocketmq --" + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */ ); // 發送消息到一個Broker SendResult sendResult = producer.send(msg); // 通過sendResult返回消息是否成功送達 System.out.printf("%s%n", sendResult); } // 如果不再發送消息,關閉Producer實例。 producer.shutdown(); } }
消費消息
import java.util.List; import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer; import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext; import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.common.message.MessageExt; public class Consumer { public static void main(String[] args) throws InterruptedException, MQClientException { // 實例化消費者 DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("zhuzeqing-1"); // 設置NameServer的地址 consumer.setNamesrvAddr("192.168.23.131:9876"); // 訂閱一個或者多個Topic,以及Tag來過濾需要消費的消息 consumer.subscribe("zhuzeqing-1-Topic", "*"); // 註冊回調實現類來處理從broker拉取回來的消息 consumer.registerMessageListener(new MessageListenerConcurrently() { public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs); // 標記該消息已經被成功消費 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); // 啟動消費者實例 consumer.start(); System.out.printf("Consumer Started.%n"); } }
七、其他
查看rmqbroker IP
docker inspect rmqbroker
Broker容器中默認的配置文件的路徑為
/etc/rocketmq/broker.conf
官網:Apache RocketMQ
參考:
Docker中RocketMQ的安裝與使用_馮立彬的博客-CSDN博客_docker rocketmq
到此這篇關於docker安裝RocketMQ的實現步驟的文章就介紹到這瞭,更多相關docker安裝RocketMQ內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 超詳細Docker Desktop下安裝rocketmq的教程
- RocketMQ消息隊列實現隨機消息發送當做七夕禮物
- 分佈式消息隊列RocketMQ概念詳解
- 一文徹底掌握RocketMQ 的存儲模型
- SpringBoot整合RocketMQ的方法詳解