如何在Clojure中使用Stuart Sierra的组件库

时间:2015-03-16 06:37:47

标签: clojure dependency-injection functional-programming components

我正在努力探讨如何在Clojure应用中使用Stuart Sierra's component library。从观看他的Youtube video开始,我想我已经掌握了导致他创建图书馆的问题。但是我正在努力研究如何在一个新的,相当复杂的项目上实际使用它。

我意识到这听起来很模糊,但感觉就像我缺少一些关键概念,一旦我明白了,我就会很好地掌握如何使用组件。换句话说,Stuart的文档和视频详细介绍了各个组成部分的内容,但我错过了如何。

是否有任何详细的教程/演练内容:

  • 为什么你要使用组件来完成非平凡的Clojure应用程序
  • 一种方法,用于说明如何在非平凡的Clojure应用程序中细分功能,以便可以以合理的最佳方式实现组件。当你所拥有的只是例如,这是相当简单的。数据库,应用程序服务器和Web服务器层,但我很难掌握如何将它用于具有许多不同层的系统,这些层都需要协调一致地工作
  • 接近开发/测试/故障转移等的方法。在一个使用组件
  • 构建的非平凡的Clojure应用程序中

提前致谢

1 个答案:

答案 0 :(得分:37)

简而言之, Component是一个专门的DI框架。它可以设置一个给定两个映射的注入系统:系统映射和依赖映射。

让我们看看一个虚构的网络应用程序(免责声明,我在没有实际运行的情况下将其输入表单中):

(ns myapp.system
  (:require [com.stuartsierra.component :as component]
            ;; we'll talk about myapp.components later
            [myapp.components :as app-components]))

(defn system-map [config] ;; it's conventional to have a config map, but it's optional
  (component/system-map
    ;; construct all components + static config
    {:db (app-components/map->Db (:db config))
     :handler (app-components/map->AppHandler (:handler config))
     :server (app-components/map->Server (:web-server config))}))

(defn dependency-map
  ;; list inter-dependencies in either:
  ;;    {:key [:dependency1 :dependency2]} form or
  ;;    {:key {:name-arg1 :dependency1
  ;;           :name-arg2 :dependency2}} form
  {:handler [:db]
   :server {:app :handler})

;; calling this creates our system
(def create-system [& [config]]
  (component/system-using
    (system-map (or config {})
    (dependency-map)))

这允许我们在需要时调用(create-system)来创建整个应用程序的新实例。

使用(component/start created-system),我们可以运行它提供的系统服务。在这种情况下,它是正在侦听端口和开放数据库连接的Web服务器。

最后,我们可以使用(component/stop created-system)停止它以阻止系统运行(例如 - 停止Web服务器,断开与db的连接)。

现在让我们看看我们的应用程序components.clj

(ns myapp.components
  (:require [com.stuartsierra.component :as component]
            ;; lots of app requires would go here
            ;; I'm generalizing app-specific code to
            ;; this namespace
            [myapp.stuff :as app]))

(defrecord Db [host port]
   component/Lifecycle
   (start [c]
      (let [conn (app/db-connect host port)]
        (app/db-migrate conn)
        (assoc c :connection conn)))
   (stop [c]
      (when-let [conn (:connection c)]
        (app/db-disconnect conn))
      (dissoc c :connection)))

(defrecord AppHandler [db cookie-config]
   component/Lifecycle
   (start [c]
      (assoc c :handler (app/create-handler cookie-config db)))
   (stop [c] c))

;; you should probably use the jetty-component instead
;; https://github.com/weavejester/ring-jetty-component
(defrecord Server [app host port]
   component/Lifecycle
   (start [c]
      (assoc c :server (app/create-and-start-jetty-server
                        {:app (:handler app)
                         :host host 
                         :port port})))
   (stop [c]
      (when-let [server (:server c)]
         (app/stop-jetty-server server)
      (dissoc c :server)))

那我们刚刚做了什么?我们得到了一个可重新加载的系统。我认为使用figwheel的一些clojurescript开发人员开始看到相似之处。

这意味着我们可以在重新加载代码后轻松重启系统。到user.clj

(ns user
    (:require [myapp.system :as system]
              [com.stuartsierra.component :as component]
              [clojure.tools.namespace.repl :refer (refresh refresh-all)]
              ;; dev-system.clj only contains: (def the-system)
              [dev-system :refer [the-system]])

(def system-config
  {:web-server {:port 3000
                :host "localhost"}
   :db {:host 3456
        :host "localhost"}
   :handler {cookie-config {}}}


(defn init []
  (alter-root-var #'the-system
                  (constantly system/create-system system-config)))

(defn start []
  (alter-root-var #'the-system component/start))

(defn stop []
  (alter-root-var #'the-system
                  #(when % (component/stop %))))

(defn go []
  (init)
  (start))

(defn reset []
  (stop)
  (refresh :after 'user/go))

要运行系统,我们可以在我们的repl中输入:

(user)> (reset)

将重新加载我们的代码,然后重新启动整个系统。它将关闭正在运行的正在运行的系统。

我们还有其他好处:

  • 端到端测试很简单,只需编辑配置或替换组件以指向进程内服务(我已将其用于指向进程内kafka服务器以进行测试)。
  • 理论上,您可以为同一个JVM多次生成应用程序(不像第一点那样实际)。
  • 进行代码更改并且必须重新启动服务器时,无需重新启动REPL
  • 与环网重新加载不同,我们可以通过统一的方式重新启动我们的应用程序,无论其目的如何:后台工作程序,微服务或机器学习系统都可以以相同的方式构建。

值得注意的是,由于所有内容都在进程中,因此Component不会处理与故障转移,分布式系统或错误代码相关的任何内容;)

有很多“资源”(也称为有状态对象),Component可以帮助您在服务器内进行管理:

  • 与服务的连接(队列,dbs等)
  • 时间的流逝(调度员,cron等)
  • 记录(应用程序记录,异常记录,指标等)
  • 文件IO(blob存储,本地文件系统等)
  • 传入的客户端连接(Web,套接字等)
  • OS资源(设备,线程池等)

如果你只有一个web服务器+ db,那么组件看起来有点过分。但是现在很少有网络应用程序。

旁注:the-system移动到另一个命名空间会降低在开发时刷新the-system var的可能性(例如 - 调用refresh而不是{{1 }})。