### 1、安装rocketMQ
#### 安装nameserver rocketmq:server-4.5.2
```
docker run -d -p 9876:9876 --network mynetwork --ip 10.20.0.11 --name rmqserver foxiswho/rocketmq:server-4.5.2
```
#### 安装broker rocketmq:broker-4.5.1
```
docker run -d -p 10911:10911 -p 10909:10909 --network mynetwork --ip 10.20.0.12 --name rmqbroker --link rmqserver:namesrv -e "NAMESRV_ADDR=namesrv:9876" -e "JAVA_OPTS=-Duser.home=/opt" -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m" -v /config/rocketmq/broker.conf:/etc/rocketmq/broker.conf foxiswho/rocketmq:broker-4.5.1
```
#### 安装rocketmq console styletang/rocketmq-console-ng:latest
```
docker run -d --name rmqconsole -p 8180:8080 --network mynetwork --ip 10.20.0.13 --link rmqserver:namesrv\
-e "JAVA_OPTS=-Drocketmq.namesrv.addr=namesrv:9876\
-Dcom.rocketmq.sendMessageWithVIPChannel=false"\
-t styletang/rocketmq-console-ng
```
ps:这种方式启动后 远程访问 broker的地址总是显示的docker的内网地址 例如上面设定的10.20.0.12:10911 看网上说修改broker的配置文件 /etc/rocketmq/broker.conf 中的 brokerIP1 为外网地址
```
brokerClusterName = DefaultCluster
brokerName = broker-a
brokerId = 0
namesrvAddr = 192.168.199.177:9876 #内网地址,将broker的IP告诉nameserver
brokerIP1 = 61.188.xxx.xx #修改为自己的外网地址
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
```
然而,没有卵用,还是内网地址,真是搞人心态。
为了验证不是MQ本身的其他问题,我将测试项目放到线上去运行,地址就用docker的内网地址,结果没有意外的成功发送和接受了。所以MQ没有问题,还是地址设置的问题。
#### 采用 docker-compose去部署
所以,我换了docker-compose的方式去部署,加上使用配置文件启动的命令,并自定义了网络,再做尝试,docker-compose.yml文件如下:
```
version: '3.5'
services:
rmqnamesrv:
image: foxiswho/rocketmq:server-4.5.2
container_name: rmqnamesrv
ports:
- 9876:9876
volumes:
- /opt/rocketmq/logs:/opt/logs
- /opt/rocketmq/store:/opt/store
environment:
JAVA_OPT_EXT: "-Duser.home=/opt -Xms128m -Xmx128m -Xmn128m"
networks:
rmq:
aliases:
- rmqnamesrv
rmqbroker:
image: foxiswho/rocketmq:broker-4.5.1
container_name: rmqbroker
ports:
- 10909:10909
- 10911:10911
volumes:
- /opt/rocketmq/logs:/opt/logs
- /opt/rocketmq/store:/opt/store
- /opt/rocketmq/conf/broker.conf:/etc/rocketmq/broker.conf
environment:
JAVA_OPT_EXT: "-Duser.home=/opt -server -Xms128m -Xmx128m -Xmn128m"
command: ["/bin/bash","mqbroker","-c","/etc/rocketmq/broker.conf","-n","rmqnamesrv:9876","autoCreateTopicEnable=true"] #这里设置了配置文件
depends_on:
- rmqnamesrv
networks:
rmq:
aliases:
- rmqbroker
rmqconsole:
image: styletang/rocketmq-console-ng
container_name: rmqconsole
ports:
- 8180: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
```
命令如下
docker-comose stop/start/rm/restart/logs
然而,还是访问的内网地址,准备放弃。
但是,我还是不愿意就这么算了。
接着我查看docker-compose logs rmqconsole 查看日志
```2019-12-02 22:42:11 INFO ShutdownHook - Try to shutdown service thread:TransactionalMessageCheckService started:true lastThread:Thread[TransactionalMessageCheckService,5,main]
2019-12-02 22:42:11 INFO ShutdownHook - shutdown thread TransactionalMessageCheckService interrupt false
2019-12-02 22:42:11 INFO ShutdownHook - join thread TransactionalMessageCheckService eclipse time(ms) 1 90000
2019-12-02 22:42:11 INFO ShutdownHook - Shutdown hook over, consuming total time(ms): 4795
2019-12-02 22:42:50 INFO main - rocketmqHome=/opt/rocketmq-4.5.1 #这句话比较关键
2019-12-02 22:42:50 INFO main - namesrvAddr=rmqnamesrv:9876
2019-12-02 22:42:50 INFO main - brokerIP1=172.22.0.3 #日志显示也是用的内部地址
2019-12-02 22:42:50 INFO main - brokerIP2=172.22.0.3
```
这时候我就开始觉得,是不是初始化的时候根本就没有读取/etc/rocketmq/broker.conf这个配置文件,而是还有别的。
于是我就去 日志提示的 rocketMQ的项目路径下去一探究竟。rocketmqHome=/opt/rocketmq-4.5.1
```
[root@2bf75e4b1aa2 rocketmq-4.5.1]# cd /opt/rocketmq-4.5.1/
[root@2bf75e4b1aa2 rocketmq-4.5.1]# ls
LICENSE NOTICE README.md benchmark bin conf lib
#bin下为执行文件 例如关闭broker 就用 sh mqshutdown broker
# conf为配置文件
[root@2bf75e4b1aa2 rocketmq-4.5.1]# cd conf/
[root@2bf75e4b1aa2 conf]# ls
2m-2s-async 2m-2s-sync 2m-noslave broker.conf.old dledger logback_broker.xml logback_namesrv.xml logback_tools.xml plain_acl.yml tools.yml
# broker.conf.old 这个文件引起了我的注意,他的内容和 /etc/rocket/broker.conf一样,我就严重怀疑,是不是初始化读取的就是这个文件。
#于是我修改了,内容同上,重启容器,开始祈祷。。。。。
```
```
2019-12-02 23:10:29.175 INFO 176312 --- [ main] com.tianzow.mq.MqApplication : Started MqApplication in 3.0 seconds (JVM running for 3.451)
消息发送成功
收到MQ消息:MessageExt [queueId=11, storeSize=221, queueOffset=3, sysFlag=0, bornTimestamp=1575299430046, bornHost=/27.10.126.231:53403, storeTimestamp=1575299429571, storeHost=/61.188.xxx.xx:10911, msgId=3DBCD10A00002A9F0000000000002970, commitLogOffset=10608, bodyCRC=1085448723, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='test-demo', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=4, KEYS=bd3b8129-df4a-4850-9d60-8b7c95e02e16, CONSUME_START_TIME=1575299430108, UNIQ_KEY=C0A801D3B0B818B4AAC20A1F669D0000, WAIT=true, TAGS=111}, body=[-27, -113, -111, -23, -128, -127, -26, -75, -117, -24, -81, -107, -26, -74, -120, -26, -127, -81], transactionId='null'}]
```
一切索然无味,浪费我大好青春。
#### 总结 broker-4.5.1这个版本,配置文件读取的是/opt/rocketmq-4.5.1/broker.conf.old这个文件,因此修改它即可。