我可以在特定的servlet上下文中运行lein ring server-headless吗?

Bob*_*har 5 clojure ring leiningen

我有一个Ring应用程序,通过部署到生产作为一个uberwar东西; myservice.war.在生产中,WAR文件被抛入Jetty,它在其名称后面的上下文中运行

$ curl -i -X GET http://myservice.qa1.example.com:8080/myservice/healthz
HTTP/1.1 200 OK
...
Run Code Online (Sandbox Code Playgroud)

当我通过lein ring在本地运行时,我需要它在相同的环境中运行; 为MyService.

$lein ring server-headless
2015-10-14 14:04:03,457  level=INFO [main] Server:271 - jetty-7.6.13.v20130916
2015-10-14 14:04:03,482  level=INFO [main] AbstractConnector:338 - Started SelectChannelConnector@0.0.0.0:10313
Started server on port 10313
Run Code Online (Sandbox Code Playgroud)

但同样的卷曲在我当地全部404.

$ curl -i -X GET http://localhost:10313/myservice/healthz
HTTP/1.1 404 Not Found
...
Run Code Online (Sandbox Code Playgroud)

lein ring将它部署到根上下文中.

$ curl -i -X GET http://localhost:10313/healthz
HTTP/1.1 200 OK
...
Run Code Online (Sandbox Code Playgroud)

那是怎么回事?如何指示lein ring部署到我选择的上下文名称中?我需要curl -i -X GET http://localhost:10313/myservice/healthz从lein ring工作

ez1*_*1sl 1

解决此问题的一种方法是为您的应用程序创建第二组(独立)路由。您还可以为独立案例创建第二个处理程序。然后,您可以使用 Leiningen 配置文件为独立案例和 uberwar 案例指定不同的处理程序。独立运行应用程序时使用默认配置文件。:uberjar创建 uberwar 时会使用该配置文件。因此,lein ring server-headless当战争部署到容器中时,您的独立处理程序将与常规处理程序一起使用。

创建第二组路由不需要太多额外的代码。您可以将现有的路由包装在您选择的上下文中。假设以下是您的路由和环处理程序:

(defroutes app-routes
  (GET "/healthz" [] "Hello World")
  (route/not-found "Not Found"))

(def app
  (wrap-defaults app-routes site-defaults))
Run Code Online (Sandbox Code Playgroud)

独立案例的附加路由和处理程序如下所示:

(defroutes standalone-routes
  (context "/myservice" req app-routes)
  (route/not-found "Not Found"))

(def standalone-app
  (wrap-defaults standalone-routes site-defaults))
Run Code Online (Sandbox Code Playgroud)

现在,lein-ring进入project.clj. 我们希望默认的环处理程序指向standalone-app。uberwar 的环处理程序应该指向app:ring项目映射中的条目应project.clj如下所示(根据您的实际命名空间进行调整):

:ring {:handler myservice.handler/standalone-app}
Run Code Online (Sandbox Code Playgroud)

另外,将以下内容合并到您的:profiles地图中project.clj

:uberjar {:ring {:handler myservice.handler/app}}
Run Code Online (Sandbox Code Playgroud)

请务必使用最新版本的lein-ring插件。0.9.7 版本对我有用。早期版本(例如 0.8.3)无法工作,因为它们:uberjar在运行uberwar任务时没有使用配置文件。

lein ring server-headless如果您执行了所有这些操作,并假设您的 war 文件名为 myservice.war,则无论您的应用程序是启动的还是 war 文件部署在 Jetty 中,URI 的上下文部分都将是相同的。

$ curl http://localhost:[port]/myservice/healthz
Run Code Online (Sandbox Code Playgroud)