-1

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

AMQ221020:在 localhost:61616 为协议启动 EPOLL 接受器 [CORE]

但我无法从 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);
    }
}

码头工人文件:

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

4

1 回答 1

1

连接失败是因为 netty 接受器正在侦听 localhost,因此它只能接受来自在同一主机/容器上运行的进程的连接。

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

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

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

于 2021-11-18T09:44:14.473 回答