如何解决“无法连接到服务器 (32103) - java.net.ConnectException: 连接被拒绝”在码头工人?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论