我的项目使用 Docker Compose 创建两个服务(app 和 postgres)。在本地,应用程序(使用 Compojure、JDBC、Korma、Ragtime 等的 Clojure 应用程序)立即无问题地连接到 postgres。但是,当我将我的应用程序部署到 Digital Ocean Droplet(1 GB RAM/30 GB 磁盘/Ubuntu 16.04.2 x64)以进行测试时,应用程序似乎需要几分钟才能连接到 postgres - 例如,Korma 插入挂起几分钟,然后最终开始工作没有问题。Droplet 很小,但它似乎并不缺乏资源(基于 htop 的输出)。
以下是我的申请的相关部分:
;; project.clj
(defproject backend "0.1.0-SNAPSHOT"
:min-lein-version "2.0.0"
:dependencies [[com.grammarly/perseverance "0.1.2"]
[commons-codec/commons-codec "1.4"]
[compojure "1.4.0"]
[environ "1.0.3"]
[clj-http "2.3.0"]
[korma "0.4.3"]
[lock-key "1.4.1"]
[me.raynes/fs "1.4.6"]
[midje "1.6.3"]
[org.clojure/clojure "1.8.0"]
[org.clojure/core.async "0.3.441"]
[org.clojure/java.jdbc "0.7.0-alpha2"]
[postgresql "9.3-1102.jdbc41"]
[ragtime "0.6.0"]
[ring-cors "0.1.7"]
[ring-mock "0.1.5"]
[ring/ring-defaults "0.1.5"]
[ring/ring-json "0.4.0"]]
:plugins [[lein-environ "1.0.3"]
[lein-midje "3.1.3"]
[lein-ring "0.9.7"]]
:aliases {"migrate" ["run" "-m" "backend.db/ragtime-migrate"]
"rollback" ["run" "-m" "backend.db/ragtime-rollback"]}
:ring {:handler backend.handler/app}
:profiles
{:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
[ring/ring-mock "0.3.0"]]}})
;; db.clj
(ns backend.db
(:use [korma.core]
[korma.db])
(:require [clojure.string :as string]
[environ.core :as environ]
[lock-key.core :refer [encrypt-as-base64 decrypt-from-base64]
:rename {encrypt-as-base64 encrypt
decrypt-from-base64 decrypt}]
[ragtime.jdbc :as jdbc]
[ragtime.repl :as repl]))
(def database-host (environ/env :postgres-port-5432-tcp-addr)) ;; set by Docker
(def database-name (environ/env :database-name))
(def database-password (environ/env :database-password))
(def database-port (environ/env :postgres-port-5432-tcp-port)) ;; set by Docker
(def database-sslmode (environ/env :database-sslmode))
(def database-user (environ/env :database-user))
(def database-url (str "jdbc:postgresql://"
database-host
":"
database-port
"/"
database-name
"?user="
database-user
"&password="
database-password))
(defn load-config []
{:datastore (jdbc/sql-database {:connection-uri database-url})
:migrations (jdbc/load-resources "migrations")})
(defn ragtime-migrate []
(repl/migrate (load-config)))
(defn ragtime-rollback []
(repl/rollback (load-config)))
(defdb db (postgres {:db database-name
:host database-host
:password database-password
:port database-port
:user database-user
:sslmode database-sslmode}))
(defentity engagements)
(def lock (environ/env :lock))
(defn query-engagement [id]
(let [engagement (first
(select
engagements
(where {:id (read-string id)})))
decrypted-email (->
(:email_address engagement)
(decrypt lock))]
(conj engagement {:email_address decrypted-email})))
(defn create-engagement [email-address image-path]
(let [encrypted-email (encrypt email-address lock)]
(insert engagements
(values [{:email_address encrypted-email
:image_path image-path}]))))
;; docker-compose.yml
app:
build: .
volumes:
- .:/app
ports:
- "127.0.0.1:3000:3000"
links:
- postgres
postgres:
build: .
dockerfile: Dockerfile-postgres
expose:
- "5432"
我做错了什么吗?这可能是 JDBC 连接池问题吗?是否有调试此类问题的约定?
更新:如果我直接在 Digital Ocean Droplet 上运行应用程序,而不是通过 Docker,我可以确认问题仍然存在。