将 clojure 应用程序与 Ruby on Rails 应用程序相结合
我有一个 Clojure 后端,我想在其中放置一个 Rails 前端。我应该如何连接它们,具体该怎么做?
在我的脑海中,有两种方法:
- 在 Rails 上运行 Jruby,在与 clojure 应用程序运行普通 Ruby on Rails 相同的进程中
- ,通过消息队列、Web hooks 和连接将其连接到 clojure 后端数据库。
两者似乎都有缺点。在前者中,运行 jruby 似乎会限制我们可以重用的 gem,否则当库作者没有做出完全兼容的东西时会降低我们的生产力。在后者中,我想我们会错过代码重用 - 在某些情况下可能必须两次实现相同的事情 - 并且更复杂的接口(例如网络钩子而不仅仅是函数调用)将使我们付出代价。
最后,就 JRuby 而言,尚不清楚如何实际连接两者。两者都带有管理脚本:rake 和 leiningen,以及特定的存储库布局。我真的不知道如何开始将它们结合在一起。欢迎提供建议和战争故事。
I have a Clojure back-end that I want to put a Rails front-end to. How should I connect them, and how specifically do I do it?
Off the top of my head, there are two ways:
- run Jruby on rails, in the same process as the clojure app
- run normal Ruby on Rails, and connect it to the clojure backend through a message queue, web hooks, and through a joint DB.
Both seem to have disadvantages. In the former, it seems that running jruby will limit the gems we can reuse and otherwise cost us productivity when library authors have not made something perfectly compatible. In the latter, I imagine we'll miss out on code reuse - possibly having to implement the same thing twice in some cases - and that the more complicated interfaces (web hooks instead of just function calls for example) will cost us.
Finally, in the case of JRuby, it's unclear how to actually connect the two. Both come with management scripts: rake and leiningen, and specific repository layouts. I don't really how to start joining them together. Advise and war stories welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想如果是我,我肯定会使用消息队列在这些服务之间进行通信——尽管 JRuby 在前端也是一个不错的选择(我认为大多数 gems 在这一点上都相当兼容,即使是那些与原生扩展。)最终,您可以使用这两种方法的元素。
您所讨论的是一个相当典型的 SOA 设置,这是 SOA 的伟大之处之一 — 您可以在不同的语言和平台最擅长的地方加以利用。尝试在 jruby-on-rails 设置中使用 clojure 可能会导致很多痛苦和痛苦,IIRC 它首先会破坏使用 clojure 的一些好处。
不幸的是,我没有任何与你想要做的事情完全匹配的战争故事,但我现在正处于这样的架构之中,并且它的效果很好,使用 RabbitMQ 让 MRI 1.9 与后端对话运行 JRuby 的工人。不确定你如何在 clojure 中使用消息,但我想在某个地方一定有关于它的文档,并且你将能够保持干净的分离。如果您要在 jruby 上运行前端,您仍然可以在 jruby 进程和 clojure 后端之间共享代码,而不必将它们放在同一进程中。
I think if it were me, I'd definitely use message queues to communicate between those services—though JRuby really is a good choice on the front-end as well (I think most gems are fairly compatible at this point, even those with native extensions.) Ultimately, you can use elements of both approaches.
What you're talking about is a fairly typical SOA set up, and that's one of the great things about SOA—you can leverage different languages and platforms where they do best. Trying to use clojure within a jruby-on-rails setup is likely to lead to much pain and misery, and IIRC it'll taint some of the benefits of using clojure in the first place.
Unfortunately I don't have any war stories that precisely match up to what you're trying to do, but I'm in the middle of such an architecture right now and its working out great, using RabbitMQ to let MRI 1.9 talk to backend workers running JRuby. Not sure how you'd consume messages in clojure, but I imagine there's got to be docs on it somewhere, and you'll be able to maintain clean separation. If you were to run your front end on jruby, you can still share code between the jruby process and clojure backend without having to put them in the same process.
我还没有使用 Clojure + JRuby 做过任何事情,但我已经尝试过 JRuby + Scala/Java。
Scala/Java 端作为服务层,JRuby on Rails 位于其之上。这两个项目都是单独管理的:Scala/Java 项目通过 Maven 编译成 JAR,然后将其作为依赖项添加到 Rails 应用程序中。
这似乎工作得很好,我想 Clojure 会工作得更好,因为 Clojure 集合实现了 JRuby 使用的接口,在 Scala 中我必须调整服务层接口。
I haven't done anything yet with Clojure + JRuby, but I have tried out JRuby + Scala/Java.
Scala/Java side works as the service layer and JRuby on Rails sits on top of it. Both projects are managed separately: Scala/Java project is compiled via Maven into a JAR which is then added as a dependency to the Rails application.
This seems to work quite nicely and I guess Clojure would work even better, because Clojure collections implement the interfaces that JRuby consumes, in Scala I had to tweak the service layer interfaces.
免责声明:我还没有使用 Ruby 完成此操作,但在同一应用程序中混合 Clojure 和 Java 模块方面有相当多的经验,并且效果非常好。
如果你能让它工作,我强烈建议你采用 JRuby 路线,原因如下:
至于项目布局,我希望最简单的方法是创建两个不同的项目,将一个项目打包为 jar 并将其作为另一个项目的库包含在内。
Disclaimer: I haven't done this with Ruby, but have quite a bit of experience mixing Clojure and Java modules in the same application and it works pretty well.
If you can make it work, I'd strongly recommend going the JRuby route for the following reasons:
As for project layouts, I expect the simplest approach will be to create two different projects, package one as a jar and include it as a library for the other.