【发布时间】:2021-12-28 16:54:26
【问题描述】:
我正在尝试使用 Spring Boot 和 Docker 部署 ActiveMQ Artemis。 Artemis 加上 Spring Boot 工作正常。我正在使用spring-boot-maven-plugin 来生成一个胖罐子。当我尝试将该胖 jar 部署到 Docker 中时,我看到接受器已启动:
AMQ221020:在 localhost:61616 为协议 [CORE] 启动 EPOLL 接受器
但我无法从 Docker 外部连接到它。
线程“主”ActiveMQConnectionTimedOutException[errorType=CONNECTION_TIMEDOUT message=AMQ119013:等待接收集群拓扑超时。组:空]
我正在使用这个命令在 Docker 中运行它:
docker run -p 61616:61616 8bd9ff19ea08
有什么想法吗?
这里是pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
</parent>
<groupId>SpringBootArtemis2</groupId>
<artifactId>SpringBootArtemis2</artifactId>
<name>Spring Boot Artemis Starter</name>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.source>1.8</java.source>
<spring.boot.version>2.5.6</spring.boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Spring启动配置文件:
@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 {
configuration.addAcceptorConfiguration("netty", "tcp://localhost:61616");
}
catch (Exception e) {
throw new RuntimeException("Failed to add netty transport acceptor to artemis instance", e);
}
}
};
}
}
SpringBootApplication 文件:
@SpringBootApplication
public class SpringBootExampleMain {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootExampleMain.class, args);
}
}
docker 文件:
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/SpringBootArtemis2-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 61616
消费者代码:
ServerLocator locator = ActiveMQClient.createServerLocator("tcp://localhost:61616");
ClientSessionFactory factory = locator.createSessionFactory();
ClientSession session = factory.createSession();
ClientProducer producer = session.createProducer("example");
ClientMessage message = session.createMessage(true);
message.getBodyBuffer().writeString("Hello");
session.createQueue("example", "example", true);
ClientConsumer consumer = session.createConsumer("example");
producer.send(message);
session.start();
ClientMessage msgReceived = consumer.receive();
System.out.println("message = " + msgReceived.getBodyBuffer().readString());
session.close();
我也有一个不同的设置暴露 OpenWire,我有相同的行为:没有 docker 可以正常工作,但不能使用 docker
【问题讨论】:
-
如果一个进程正在监听 Docker 容器中的
localhost,那么该进程是否可以在 Docker 容器外部访问?通常localhost只能被运行在同一主机上的进程访问。 -
是的,这要归功于端口转发。在我的第二个设置中,我嵌入了 hawtio Spring boot 插件并且我能够访问它。问题似乎与该端口号或协议有关。
-
@JustinBertram 是对的,实际上不可能将容器的本地主机映射到外部,如果进程正在侦听本地主机,它只能接受来自同一主机/容器上运行的进程的连接.
-
Spring-boot 嵌入式 tomcat 监听 localhost 是如何工作的?
标签: spring-boot docker activemq-artemis artemiscloud