运行 Spring 在 Docker 上启动的 ActiveMQ Artemis

Running ActiveMQ Artemis with Spring Boot on Docker

我正在尝试使用 Spring 引导和 Docker 部署 ActiveMQ Artemis。 Artemis plus Spring 引导工作正常。我正在使用 spring-boot-maven-plugin 生成一个胖罐子。当我尝试将该 fat jar 部署到 Docker 时,我看到接受器已启动:

AMQ221020: Started EPOLL Acceptor at localhost:61616 for protocols [CORE]

但我无法从 Docker 外部连接到它。

Exception in thread "main" ActiveMQConnectionTimedOutException[errorType=CONNECTION_TIMEDOUT message=AMQ119013: Timed out waiting to receive cluster topology. Group:null]

我正在使用此命令将其 运行 转换为 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

连接失败,因为 netty 接受器正在侦听本地主机,因此它只能接受来自同一 host/container.

进程 运行 的连接

要解决此问题,SpringBootExampleConfiguration 应将 netty 接受器设置为侦听 0.0.0.0,即。 e.

configuration.addAcceptorConfiguration("netty", "tcp://0.0.0.0:61616");

Docker 使部署微服务应用程序变得非常容易,但它对生产环境有一些限制。我会看一下开源 ArtemisCloud.io 项目,它是容器镜像的集合,提供了一种在 Kubernetes 上部署 Apache ActiveMQ Artemis Broker 的方法。