简体   繁体   English

如何从主机外部(同一网络)连接到在 docker 容器内运行的 activemq 代理 [Windows]

[英]How to connect to an activemq broker which is running inside a docker container from outside the host (same network) [Windows]

I created an embedded ActiveMQ, and I could start it inside a Tomcat and connect successfully to the broker on my localhost without docker.我创建了一个嵌入式 ActiveMQ,我可以在 Tomcat 中启动它,并在没有 docker 的情况下成功连接到我的本地主机上的代理。

broker.addConnector("tcp://" +  System.getProperty("JMS_HOST") + ":" + System.getProperty(JMS_PORT));

ActiveMQ JMS Message Broker (My-Broker-7777, ID:My-PC-64834-1660051787310-0:0) started ActiveMQ JMS 消息代理 (My-Broker-7777, ID:My-PC-64834-1660051787310-0:0) 已启动

Then I used this embedded ActiveMQ inside a docker container with Tomcat and started successfully.然后我在带有 Tomcat 的 docker 容器中使用了这个嵌入式 ActiveMQ 并成功启动。

ActiveMQ JMS Message Broker (My-Broker-7777, ID:d2051a39295f-41549-1660080225701-0:0) started ActiveMQ JMS 消息代理 (My-Broker-7777, ID:d2051a39295f-41549-1660080225701-0:0) 已启动

I connected to the ActiveMQ inside the container and got the error.我连接到容器内的 ActiveMQ 并收到错误消息。

javax.jms.JMSException: Could not connect to broker URL: tcp://0.0.0.0:7777. javax.jms.JMSException:无法连接到代理 URL:tcp://0.0.0.0:7777。 Reason: java.net.ConnectException: Connection refused.原因:java.net.ConnectException:连接被拒绝。

My Dokerfile我的 Dockerfile

FROM tomcat:9-jre8
RUN rm -rf /usr/local/tomcat/webapps/*
RUN mv /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./tomcat/tomcat-users.xml  /usr/local/tomcat/conf
COPY ./tomcat/context.xml  /usr/local/tomcat/webapps/manager/META-INF
COPY ./dist/my-broker-1.0.war  /usr/local/tomcat/webapps
ENV JPDA_ADDRESS=0.0.0.0:8000
ENV JPDA_TRANSPORT=dt_socket
EXPOSE 8080
EXPOSE 7777
ENV JAVA_OPTS="-DJMS_PORT=7777 -DJMS_HOST=0.0.0.0"
CMD ["catalina.sh", "jpda", "run"]

docker run -it -p 8089:8080 --name my-broker my-broker

I could access the Tomcat manager via localhost:8089我可以通过 localhost:8089 访问 Tomcat 管理器

I have tried to change JMS_HOST to 'localhost' or remove JMS_HOST property inside the container, but the error is the same.我试图将 JMS_HOST 更改为 'localhost' 或删除容器内的 JMS_HOST 属性,但错误是相同的。

Below is the code I used to connect to the ActiveMQ from another tomcat on my local machine.下面是我用来从本地机器上的另一个 tomcat 连接到 ActiveMQ 的代码。 I also tried to set JMS_HOST of this Tomcat as 'localhost' or '0.0.0.0'我还尝试将此 Tomcat 的 JMS_HOST 设置为“localhost”或“0.0.0.0”

<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://${JMS_HOST}:${JMS_PORT}"/>
</bean>

Is my approach correct?我的方法正确吗? I am not sure my technique is allowed in the docker world.我不确定我的技术在 docker 世界中是否允许。 Thank you for your attention.感谢您的关注。

I found the problem was why I could not connect to the ActiveMQ, which running inside a container with Tomcat from another Tomcat outside on the local machine.我发现问题是为什么我无法连接到 ActiveMQ,它在一个容器内运行,其中 Tomcat 来自本地机器外部的另一个 Tomcat。

I modified the docker run as below我修改了 docker 运行如下

docker run -it -p 8089:8080 -p 7777:7777 --name my-broker my-broker

The JMS_HOST is not important in my case, it can be 'localhost' or '0.0.0.0'在我的情况下,JMS_HOST 并不重要,它可以是 'localhost' 或 '0.0.0.0'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM