gpt4 book ai didi

postgresql - docker-compose: spring-boot web服务访问postgres db服务

转载 作者:行者123 更新时间:2023-11-29 12:09:36 26 4
gpt4 key购买 nike

在我的 docker-compose.yal 文件中,我定义了一个基于 spring-boot 的 Web 服务和一个 postgresql 数据库服务。 Web 服务引用数据库服务,并有一个数据库数据源 URL 的环境变量,它需要引用数据库服务的主机名。这是它的样子:

version: '3'
services:

web:
container_name: medmap-server
hostname: medmap-server
build: ../../../build/docker
ports: ["8090:8090"]
links:
- db:database
environment:
- spring.datasource.url=jdbc:postgresql://medmap-db:5433/medmapdb
- spring.datasource.username=docker
- spring.datasource.password=docker
- logging.level.org.hibernate=DEBUG

db:
container_name: medmap-db
hostname: medmap-db
image: postgres:9.6.2
ports:
- "5433:5433"
volumes:
- /var/lib/medmap/data
environment:
- POSTGRES_PASSWORD=docker
- POSTGRES_USER=docker
- POSTGRES_DB=medmapdb
- PGDATA=/var/lib/medmap/data/pgdata

当我使用 docker-compose up 启动此应用程序时,出现以下错误:

medmap-server | Caused by: org.postgresql.util.PSQLException: Connection to medmap-db:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
.
medmap-server | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:239) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server | at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:127) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server | at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server | at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server | at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:41) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server | at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server | at org.postgresql.Driver.makeConnection(Driver.java:414) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server | at org.postgresql.Driver.connect(Driver.java:282) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server | at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server | at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server | at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:732) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server | at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:664) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server | at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:479) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server | at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server | at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server | at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server | at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131) ~[tomcat-jdbc-8.5.11.jar!/:na]
medmap-server | at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.0.12.Final.jar!/:
5.0.12.Final]
medmap-server | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5
.0.12.Final.jar!/:5.0.12.Final]
medmap-server | at org.hibernate.tool.schema.extract.internal.ExtractionContextImpl.getJdbcConnection(ExtractionContextImpl.java:62) ~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
medmap-server | ... 38 common frames omitted
medmap-server | Caused by: java.net.ConnectException: Connection refused (Connection refused)
medmap-server | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_121]
medmap-server | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_121]
medmap-server | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_121]
medmap-server | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_121]
medmap-server | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_121]
medmap-server | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_121]
medmap-server | at org.postgresql.core.PGStream.<init>(PGStream.java:61) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:121) ~[postgresql-9.4-1200-jdbc41.jar!/:9.4]
medmap-server | ... 58 common frames omitted
medmap-server |
medmap-server exited with code 1

我不知道 pg_hba.conf 文件是如何创建的,以允许一个容器中的 web 服务访问另一个容器中的 db 服务。 TIA 对此有任何帮助。

最佳答案

default port for postgres is 5432 ,尝试定义它而不是 5433

port (integer) The TCP port the server listens on; 5432 by default. Note that the same port number is used for all IP addresses the server listens on. This parameter can only be set at server start.

关于postgresql - docker-compose: spring-boot web服务访问postgres db服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43562842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com