这是我的 docker-compose.yml的摘录:
version: '3.5'
services:
traefik:
image: "traefik:v2.4"
command:
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.websecure.address=:443"
- "--entrypoints.web.address=:80"
- "--entrypoints.web.http.redirections.entryPoint.to=websecure"
- "--entrypoints.web.http.redirections.entryPoint.scheme=https"
- "--certificatesresolvers.leresolver.acme.tlschallenge=true"
- "--certificatesresolvers.leresolver.acme.email=${TRAEFIK_EMAIL_CERT}"
- "--certificatesresolvers.leresolver.acme.storage=/letsencrypt/acme.json"
- "--api.dashboard=true"
- "--entryPoints.web.forwardedHeaders.trustedIPs=127.0.0.1/32"
labels:
- "traefik.enable=true"
- "traefik.http.routers.dashboard.rule=Host(`${TRAEFIK_FRONTEND_DOMAIN}`)"
- "traefik.http.routers.dashboard.entrypoints=websecure"
- "traefik.http.routers.dashboard.tls.certresolver=leresolver"
- "traefik.http.routers.dashboard.service=api@internal"
ports:
- 80:80
- 443:443
volumes:
- ./data/letsencrypt:/letsencrypt
- /var/run/docker.sock:/var/run/docker.sock:ro
restart: unless-stopped
db_app:
image: postgres:13.2
volumes:
- db_app:/var/lib/postgresql/data
environment:
POSTGRES_DB: app
POSTGRES_USER: app
POSTGRES_PASSWORD: app
expose:
- 5432
restart: unless-stopped
app_backend:
image: registry.gitlab.com/mygroup/app/backend:latest
environment:
# ...
SPRING_R2DBC_URL: r2dbc:postgresql://db_app:5432/app
SPRING_R2DBC_USERNAME: app
SPRING_R2DBC_PASSWORD: app
expose:
- 8080
depends_on:
- db_app
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.http.routers.app.rule=Host(`${APP_BACKEND_DOMAIN}`)"
- "traefik.http.routers.app.entrypoints=websecure"
- "traefik.http.routers.app.tls.certresolver=leresolver"
- "traefik.http.routers.app.service=app-api"
- "traefik.http.services.app-api.loadbalancer.server.port=8080"
并且在启动期间后端显示以下异常(但继续运行!):
2021-07-28 22:01:53 Exception in thread "DefaultDispatcher-worker-1" org.springframework.dao.DataAccessResourceFailureException: Failed to obtain R2DBC Connection; nested exception is io.r2dbc.postgresql.PostgresqlConnectionFactory$PostgresConnectionException: Cannot connect to db_app:5432:5432
2021-07-28 22:01:53 at org.springframework.r2dbc.connection.ConnectionFactoryUtils.lambda$getConnection$0(ConnectionFactoryUtils.java:88)
2021-07-28 22:01:53 at reactor.core.publisher.Mono.lambda$onErrorMap$31(Mono.java:3488)
2021-07-28 22:01:53 at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:94)
// ...
2021-07-28 22:01:53 at java.base/java.lang.Thread.run(Unknown Source)
2021-07-28 22:01:53 Caused by: io.r2dbc.postgresql.PostgresqlConnectionFactory$PostgresConnectionException: Cannot connect to db_app:5432:5432
2021-07-28 22:01:53 at io.r2dbc.postgresql.PostgresqlConnectionFactory.cannotConnect(PostgresqlConnectionFactory.java:218)
2021-07-28 22:01:53 at reactor.core.publisher.Mono.lambda$onErrorMap$31(Mono.java:3488)
2021-07-28 22:01:53 at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:94)
2021-07-28 22:01:53 ... 101 more
2021-07-28 22:01:53 Caused by: java.net.UnknownHostException: db_app:5432: invalid IPv6 address
2021-07-28 22:01:53 at java.base/java.net.InetAddress.getAllByName(Unknown Source)
2021-07-28 22:01:53 at io.netty.util.internal.SocketUtils$9.run(SocketUtils.java:169)
// ...
2021-07-28 22:01:53 at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:125)
2021-07-28 22:01:53 ... 73 more
我特别想知道Cannot connect to db_app:5432:5432
,因为端口似乎被添加了两次?有趣的是,当我在启动时为 flyway 迁移添加 jdbc 连接时,它工作正常并且迁移被执行:
SPRING_FLYWAY_URL: jdbc:postgresql://db_app:5432/app
SPRING_FLYWAY_USER: app
SPRING_FLYWAY_PASSWORD: app
当我将 R2DBC URI 更改为以下内容时,它可以正常工作:
SPRING_R2DBC_URL: r2dbc:postgresql://db_app/app
但我不明白为什么?所以,我的问题是添加“附加”端口的位置以及为什么行为与 JDBC 不同?
springBoot 版本 2.5.1