Docker部署Spring Boot集成ActiveMQ Artemis外部无法连接求解决方案
问题原因
你遇到的问题核心是Artemis服务仅绑定了Docker容器内部的环回地址,无法接收外部请求:
- 你在
ArtemisConfigurationCustomizer配置中,将Acceptor地址写死为tcp://localhost:61616,对应的是容器内的127.0.0.1环回接口,只有容器内部运行的进程可以访问该地址 - Docker的端口映射
-p 61616:61616是将宿主机的请求转发到容器的对外网卡(一般为eth0),请求根本无法抵达绑定在环回接口的Artemis服务,因此会触发连接超时错误 - 你提到的OpenWire协议配置也出现相同问题,本质原因也是Acceptor绑定了localhost地址
解决方案
修改SpringBootExampleConfiguration配置类中的Acceptor绑定地址即可:
@Configuration @EnableJms public class SpringBootExampleConfiguration { @Bean public ArtemisConfigurationCustomizer customizer() { return new ArtemisConfigurationCustomizer() { @Override public void customize(org.apache.activemq.artemis.core.config.Configuration configuration) { try { // 将localhost改为0.0.0.0,监听容器所有网卡接口 configuration.addAcceptorConfiguration("netty", "tcp://0.0.0.0:61616"); } catch (Exception e) { throw new RuntimeException("Failed to add netty transport acceptor to artemis instance", e); } } }; } }
如果是OpenWire协议的Acceptor配置,也按照相同规则,将绑定地址从localhost改为0.0.0.0即可。
修改完成后重新构建fat jar、重新打包Docker镜像,再启动容器即可从外部正常访问服务。
内容的提问来源于stack exchange,提问作者Nicolas Verducou
相关产品推荐
相关产品推荐

