스칼라 플레이 웹 애플리케이션 (RESTful)을 만들려고합니다. SQL 연결에 대한 플레이 튜토리얼을 따라 왔지만 플레이 애플리케이션이있는 컨테이너를 mysql 컨테이너가있는 컨테이너에 연결하는 데 문제가 있습니다. 많은 디버깅을 한 후에 나는 스칼라 애플리케이션이 도커 컨테이너 대신 로컬에서 실행될 때 작동한다는 것을 깨달았습니다.
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 run을 실행합니다.
데이터베이스 도커 파일
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를 사용하여 로컬로 재생 응용 프로그램을 실행하면 엔드 포인트가 올바르게 작동하고 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.
나는 도커에서 작동하지 않기 때문에 대부분의 스칼라 코드를 생략했습니다. 스칼라 코드 자체에는 문제가 없다고 가정합니다.
localhost
시스템 관점 (즉, 동일한 컨테이너)에서 동일한 "머신"을 의미하는 것이 아니라 다른 컨테이너를 응용 프로그램으로 지정해야합니다 .
db.default.url="jdbc:mysql://database:49160/testdb"
물론이 설정을 환경에 따라 설정할 수 있으므로 앱이 개발 모드와 docker-compose
모드 에서 작동합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다