我正在尝试通过Docker使用Clojure应用程序连接到MongoDB

时间:2016-05-30 10:24:53

标签: mongodb docker clojure compojure

我有以下docker-compose规则......

catalog-service:
    build: ./services/catalog 
    ports: 
        - "2000:3000"
    depends_on:
        - catalog-datastore
    restart: always

catalog-datastore:
    image: mongo:3.0
    command: mongod --smallfiles --quiet --logpath=/dev/null
    ports: 
        - "27017:27017"

以下用于clojure应用程序的Dockerfile ...

FROM clojure
COPY . /usr/src/app
WORKDIR /usr/src/app
CMD ["lein", "ring", "server"]

我的应用程序中的以下连接代码......

(ns catalog.handler
  (:import com.mchange.v2.c3p0.ComboPooledDataSource)
  (:use compojure.core)
  (:use cheshire.core)
  (:use ring.util.response)
  (:require [compojure.handler :as handler]
            [ring.middleware.json :as middleware]
            [clojure.java.jdbc :as sql]
            [compojure.route :as route]
            [somnium.congomongo :as m]))

(def conn
  (m/make-connection "catalog"
                     :host "catalog-datastore"
                     :port 27017))

(defn get-all []
  (m/fetch :catalog))

(defn get-single [id]
  (m/fetch-one
    :catalog
    :where{:_id (Long/parseLong id)}))

(defroutes app-routes
  (context "/catalog" [] (defroutes catalog-routes
                           (GET "/" [] (get-all))
                           (GET "/:id", [id] (get-single)))))

(def app 
  (-> (handler/api app-routes)
      (middleware/wrap-json-body)
      (middleware/wrap-json-response)))

当我尝试运行应用程序时,我收到错误...

java.lang.AssertionError
Assert failed: (connection? conn)

1 个答案:

答案 0 :(得分:2)

来自docs

set the connection globally

(m/set-connection! conn)

or locally

(m/with-mongo conn
  (m/insert! :robots {:name "robby"}))

看起来你错过了那一点。 :P