您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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)}}'
docker run -d -p 9876:9876 --name rmqserver foxiswho/rocketmq:server-4.5.1
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
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
<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”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。