我正在尝试使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而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向量
[当我通过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代码本身没有问题。
我正在尝试使Scala播放Web应用程序(RESTful)。我一直在遵循SQL连接的播放教程,但在将带有播放应用程序的容器连接到...
您应该将您的应用程序指向另一个容器,而不是从系统的角度表示相同机器的localhost
: