【问题标题】:How to build clojure application with ring server如何使用环服务器构建 clojure 应用程序
【发布时间】:2012-08-22 14:07:34
【问题描述】:

我有一个带有环库的 clojure 项目。这是project.clj:

(defproject words "1.0.0-SNAPSHOT"
:description "Websocket handler for sessions"
:dependencies [[org.clojure/clojure "1.4.0"]
  [org.clojure/clojure-contrib "1.2.0"]
  [aleph "0.3.0-alpha1"]
  [org.clojure/data.json "0.1.2"]
  [clj-redis "0.0.13-SNAPSHOT"]
  [compojure "0.6.2"]
  [clj-http "0.1.3"]]
:main words.play
;; Lein ring plugin will provide `lein ring server` functionality
;; (and some other relative to ring actions)
:plugins [[lein-ring "0.6.6"]]
:ring {:handler words.api/engine})

在开发环境中,我使用 2 个命令运行它: lein 运行服务器 林环服务器 它的工作原理。

对于生产环境,我想最小化依赖关系并将其构建到独立 jar 中:

lein uberjar

如何从一个 jar 文件构建和运行两台服务器?

【问题讨论】:

    标签: jar clojure leiningen ring


    【解决方案1】:

    关于

    :main words.play
    

    我建议你在 words.play 中实现 -main 函数

    (defn -main [& args]
      (case (first args)
        "server1" (do (println "Starting server1") (start-server1))
        "server2" (do (println "Starting server2") (start-server2))
        (println "Enter server name, pls")))
    

    注意,:gen-class 在命名空间定义中是必需的:

    (ns words.play
        (:gen-class))
    

    start-server1start-server2 的实现应依赖于具体框架:(run-jetty ...) 用于 ring,(start-http-server ...) 用于 aleph 等等(您可以在相关文档中找到更多信息)。

    用法:

    lein uberjar
    ## to start first server
    java -jar my-project-1.0.0-SNAPSHOT-standalone.jar server1
    ## to start second one
    java -jar my-project-1.0.0-SNAPSHOT-standalone.jar server2
    

    【讨论】:

      【解决方案2】:

      最直接的方法是从启动应用程序的 clojure 源文件中预编译一个类。你的-main 函数最终应该调用类似(run-jetty #'engine {:port 8080}) 的东西。

      如果您不熟悉 Clojure 提前编译(“aot”),这里有一个很好的教程: http://kotka.de/blog/2010/02/gen-class_how_it_works_and_how_to_use_it.html

      然后就是创建一个 shell 脚本,用 java -cp you-uber.jar words.Main 之类的东西启动你的应用程序。

      请注意,您的“应用启动器”类的名称和最终的 jar 名称是完全任意的。

      【讨论】:

        【解决方案3】:

        您可以使用lein ring uberjar。那将启动环服务器。您可以在 lein-ring 提供的:init 钩子中启动其他服务器。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-22
          • 2013-02-18
          • 1970-01-01
          相关资源
          最近更新 更多