gpt4 book ai didi

mysql - 在docker容器中运行时,Scala Play JDBC无法连接到MySQL

转载 作者:行者123 更新时间:2023-12-02 19:20:35 26 4
gpt4 key购买 nike

我正在尝试使Scala播放Web应用程序(RESTful)。我一直在遵循有关SQL连接的播放教程,但是在将具有播放应用程序的容器连接到具有mysql容器的容器时遇到了麻烦。经过大量调试后,我意识到scala应用程序可以在本地运行而不是在docker容器中运行。

来自application.conf的代码

db.default.driver=com.mysql.cj.jdbc.Driver
db.default.url="jdbc:mysql://localhost:49160/testdb"
db.default.username="root"
db.default.password="password"
db.default.host="localhost"

# db connections = ((pyhsical_core_count * 2) + effective_spindle_count)
fixedConnectionPool = 17

database.dispatcher {
executor = "thread-pool-executor"
throughput = 1
thread-pool-executor {
fixed-pool-size = ${fixedConnectionPool}
}
}

docker-compose.yml
version: "2"

services:
spades:
build: ./spades
depends_on:
- database
volumes:
- ./spades/cardsatra-spades:/home/app
ports:
- 49162:9000

database:
build: ./database
ports:
- 49160:3306
volumes:
- ./database/data:/var/lib/mysql:rw

sbt应用程序dockerfile
ARG OPENJDK_TAG=8u232
FROM openjdk:8u232

ARG SBT_VERSION=1.3.7

# Install sbt
RUN \
curl -L -o sbt-1.3.7.deb https://dl.bintray.com/sbt/debian/sbt-1.3.7.deb && \
dpkg -i sbt-1.3.7.deb && \
rm sbt-1.3.7.deb && \
apt-get update && \
apt-get install sbt && \
sbt sbtVersion

EXPOSE 9000

RUN mkdir /home/app
WORKDIR /home/app
COPY cardsatra-spades/entrypoint.sh .

CMD ["/bin/sh", "/home/app/entrypoint.sh"]

entrypoint.sh只运行sbt clean并运行sbt

数据库dockerfile
FROM mysql:8

# ENV MYSQL_DATABASE stormlight
ENV MYSQL_ROOT_PASSWORD password
ENV MYSQL_USER mysql
ENV MYSQL_PASSWORD password
ENV DATABASE_HOST db

Scala端点(省略类和导入)->这在GET / news / all路由上
def doSomething: Future[Vector[Newspost]] = Future {
db.withConnection { conn =>
var res: Vector[Newspost] = Vector[Newspost]()

val statement = conn.createStatement
val resultSet = statement.executeQuery("SELECT * FROM news")


while(resultSet.next) {
val id = resultSet.getInt("id")
val title = resultSet.getString("title")
val body = resultSet.getString("body")
val date = resultSet.getString("date")

res = res :+ Newspost(id, title, body, date)
}
res
}
}(dec)

当我使用docker-compose up数据库运行数据库并且使用sbt在本地运行play应用程序时,端点正常运行并返回Newspost vector

当我通过docker-compose运行两个应用程序时,我得到了巨大的堆栈跟踪
spades_1    | Getting req!
spades_1 | [error] p.a.h.DefaultHttpErrorHandler -
spades_1 |
spades_1 | ! @7emkpm006 - Internal server error, for (GET) [/news/all] ->
spades_1 |
spades_1 | play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms.]]
spades_1 | at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:332)
spades_1 | at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:251)
spades_1 | at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:421)
spades_1 | at play.core.server.AkkaHttpServer$$anonfun$2.applyOrElse(AkkaHttpServer.scala:417)
spades_1 | at scala.concurrent.impl.Promise$Transformation.run(Promise.scala:453)
spades_1 | at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
spades_1 | at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:92)
spades_1 | at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
spades_1 | at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:94)
spades_1 | at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:92)
spades_1 | Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms.

我已经省略了大部分Scala代码,因为它无法在docker中工作,我认为scala代码本身没有问题。

最佳答案

您应该将应用程序指向其他容器,而不是localhost,这从系统 Angular 来看意味着同一台“机器”(即同一容器):

db.default.url="jdbc:mysql://database:49160/testdb"

您当然可以使此设置取决于环境,以便该应用程序可以在开发模式和 docker-compose模式下工作。

关于mysql - 在docker容器中运行时,Scala Play JDBC无法连接到MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60141258/

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