Spring 启动客户端不会在 docker 上注册到 Spring 管理员



大家好。

我有一个问题,我已经挣扎了几天,但我无法解决它。

问题是我有两个停靠的Spring应用程序,其中一个是管理员,另一个是客户端。当我从Docker中运行这两个时,客户端可以毫无问题地向管理员注册,并且我可以从管理面板中看到客户端。但是,当我尝试在Docker中使用Docker网络将这两个容器作为两个容器运行时,客户端应用程序会出现以下错误:

de.codecentric.boot.admin.client.registration.DefaultApplicationRegistrator: Failed to register application as Application(name=spring client, managementUrl=http://545ade59e8dc:8585/actuator, healthUrl=http://545ade59e8dc:8585/actuator/health, serviceUrl=http://545ade59e8dc:8585/) at spring-boot-admin ([http://localhost:8080/instances]): I/O error on POST request for "http://localhost:8080/instances": Connection refused; nested exception is java.net.ConnectException: Connection refused. Further attempts are logged on DEBUG level

管理应用程序的docker-compose.yml文件:

version: '3.3'
networks:
spring-admin-network:
driver: bridge
services:
spring-admin:
build:
context: .
args:
JAR_FILE: "./target/spring-admin-1.0.0.jar"
image: spring-admin
container_name: spring-admin
ports:
- "127.0.0.1:8080:8585"
networks:
- spring-admin-network

以及客户端应用程序的docker-compose.yml文件:

version: '3.3'
networks:
spring-admin-network:
driver: bridge
services:
repo-reporter:
build:
context: .
args:
JAR_FILE: "./target/spring-client-1.0.0.jar"
image: spring-client
container_name: spring-client
ports:
- "127.0.0.1:8081:8585"
networks:
- spring-admin-network

以及用于管理应用程序的application.properies

spring.application.name=spring-admin
server.port=8585
spring.security.user.name=admin
spring.security.user.password=admin

以及客户端应用程序的application.properies

spring.application.name=spring-client
server.port=8585
server.compression.enabled=true
server.http2.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.boot.admin.client.url=http://localhost:8080
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name}
spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password}

我执行的命令:

docker network create spring-admin-network
...Some hash...
cd /spring-admin
docker-compose build
...Successfully built...
docker-compose up -d
...Creating network "springadmin_spring-admin-network" with driver "bridge"
Creating spring-admin ... done...
cd ../spring-client
docker-compose build
...Successfully built...
docker-compose up -d
...Creating network "springclient_spring-admin-network" with driver "bridge"
Creating spring-client ... done...
docker logs -f spring-client
.
.
.
de.codecentric.boot.admin.client.registration.DefaultApplicationRegistrator: Failed to register application as Application(name=spring client, managementUrl=http://545ade59e8dc:8585/actuator, healthUrl=http://545ade59e8dc:8585/actuator/health, serviceUrl=http://545ade59e8dc:8585/) at spring-boot-admin ([http://localhost:8080/instances]): I/O error on POST request for "http://localhost:8080/instances": Connection refused; nested exception is java.net.ConnectException: Connection refused. Further attempts are logged on DEBUG level

提前谢谢。

Docker在构建和运行Docker compose文件时会创建一个新的Docker网络。作为的一个例子

xxx_spring-admin-network(用于管理应用程序(

yyy_spring-admin-network(用于客户端应用程序(

你可以通过检查

docker network ls

首先,您应该建立一个Docker网络。

docker network create spring-admin-network

然后进行docker-compose.yml更改。两个文件都应该指向现有的网络。

version: '3.3'
networks:
spring-admin-network:
external: true
...