如何解决“无法连接到服务器 (32103) - java.net.ConnectException: 连接被拒绝”在码头工人?

发布于 2025-01-19 03:53:11 字数 2150 浏览 1 评论 0原文

我有一个 Java Maven 应用程序,当在本地运行时,该应用程序成功连接到在 Docker 中运行的 MQTT 代理。问题是,当同一个Java应用程序在docker的容器中运行时,无法连接。这以前有用过。

这是我得到的异常:

Failed to connect to the MQTT broker with address ws://localhost:9001 Unable to connect to server (32103) - java.net.ConnectException: Connection refused

这是 Java 应用程序的 Dockerfile:

#Required
FROM eclipse-temurin:16

# The jar file that will be run. This is a result of the build process
ARG JAR_FILE=target/*.jar

# The dependencies folder for all maven dependencies
ARG DEPENDENCIES=target/dependency/

# Make a directory for the jar
RUN mkdir /src

# Make a directory for the dependencies
RUN mkdir /src/lib

#Make a directoryfor the ServerConfig file 
RUN mkdir -p /src/main/resources
#Echo Jar File
RUN echo $JAR_FILE

# Copy the jar from it's original location to the docker container
COPY acs-simulator/${JAR_FILE} /src/jar/
#Echo Jar File
RUN echo $(ls -1 /src/jar/)


RUN echo pwd

# Copy all of the dependencies of the jar to the docker container
COPY acs-simulator/${DEPENDENCIES} /src/lib

# The entry point is the command the docker container will run when it start, so in this case it will start the jar
ENTRYPOINT ["java","-jar","/src/jar/acs-simulator-latest.jar"]

这也是 MQTT 代理配置:

## Logging

log_dest stdout
log_type error
log_type warning
log_type notice
log_type information

## MQTT Listener

listener 1883
protocol mqtt

## WebSockets Listener

listener 9001
protocol websockets

## Persistence

persistence false

这是我打印异常的方法:

  @Override
    public void onMQTTConnectionFailure(IMqttToken iMqttToken,Throwable throwable) {
        this.logger.log("Failed to connect to the MQTT broker with address " + this.client.getBrokerAddress() + " " + throwable + "   " + iMqttToken ,
                ILogger.LogLevel.ERROR);

        waitAndReconnectToBroker();
    }

I have a Java Maven application which when ran locally successfully connects to a MQTT broker ran in Docker. The problem is, it is unable to connect when the same Java application is run in a container in docker. This used to work before.

Here is the exception I get:

Failed to connect to the MQTT broker with address ws://localhost:9001 Unable to connect to server (32103) - java.net.ConnectException: Connection refused

Here is the Dockerfile of the Java application:

#Required
FROM eclipse-temurin:16

# The jar file that will be run. This is a result of the build process
ARG JAR_FILE=target/*.jar

# The dependencies folder for all maven dependencies
ARG DEPENDENCIES=target/dependency/

# Make a directory for the jar
RUN mkdir /src

# Make a directory for the dependencies
RUN mkdir /src/lib

#Make a directoryfor the ServerConfig file 
RUN mkdir -p /src/main/resources
#Echo Jar File
RUN echo $JAR_FILE

# Copy the jar from it's original location to the docker container
COPY acs-simulator/${JAR_FILE} /src/jar/
#Echo Jar File
RUN echo $(ls -1 /src/jar/)


RUN echo pwd

# Copy all of the dependencies of the jar to the docker container
COPY acs-simulator/${DEPENDENCIES} /src/lib

# The entry point is the command the docker container will run when it start, so in this case it will start the jar
ENTRYPOINT ["java","-jar","/src/jar/acs-simulator-latest.jar"]

Here is also the MQTT broker config:

## Logging

log_dest stdout
log_type error
log_type warning
log_type notice
log_type information

## MQTT Listener

listener 1883
protocol mqtt

## WebSockets Listener

listener 9001
protocol websockets

## Persistence

persistence false

Here is how I print the exception:

  @Override
    public void onMQTTConnectionFailure(IMqttToken iMqttToken,Throwable throwable) {
        this.logger.log("Failed to connect to the MQTT broker with address " + this.client.getBrokerAddress() + " " + throwable + "   " + iMqttToken ,
                ILogger.LogLevel.ERROR);

        waitAndReconnectToBroker();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文