简体   繁体   English

Ruby on rails with jRuby

[英]Ruby on rails with jRuby

I'm working on a Ruby on Rails app, currently hosted on Heroku. 我正在开发一个Ruby on Rails应用程序,目前托管在Heroku上。 We have about 5 web dynos and about 2 worker process running on average. 我们平均有大约5个网络dynos和大约2个工作进程。 But because we're using adeptscale these can change a lot, and the cost is increasing from month to month. 但是因为我们使用的是adeptscale,所以这些变化很大,而且成本每月都在增加。

We're thinking about changing the process and the infrastructure (using our own, off of amazon/google etc). 我们正在考虑改变流程和基础设施(使用我们自己的,亚马逊/谷歌等)。 And also because of the performance, access to java libraries and other gains we're planning to go with jRuby. 而且由于性能,访问java库以及我们计划使用jRuby获得的其他收益。

I haven't got much experience with jRuby at all, but I do have Java experience. 我根本没有jRuby的经验,但我确实有Java经验。 So I have a few questions: 所以我有几个问题:

Question intro: Since rails philosophy/approach differs from Javas, ie ruby webserver uses far less memory but can only process one request at a time, and so having multiple servers sort of compensates the inability to process multiple requests. 问题介绍:由于rails哲学/方法与Javas不同,即ruby webserver使用的内存少得多,但一次只能处理一个请求,因此有多个服务器可以补偿无法处理多个请求。

  1. If we go with jRuby (and have our rails project packaged as a war file and deployed to any servlet container ie Tomcat or Jboss(more than just container)), will we be able to process multiple requests then? 如果我们使用jRuby(并将我们的rails项目打包为war文件并部署到任何servlet容器,即Tomcat或Jboss(不仅仅是容器)),那么我们能够处理多个请求吗?

Question intro: Currently we got some application logic running in the workers(instead of blocking the webserver, and not being able to serve other clients/browser clients). 问题介绍:目前我们在工作者中运行了一些应用程序逻辑(而不是阻止Web服务器,而不能为其他客户端/浏览器客户端提供服务)。 ie when users submit some form and then our app needs to contact the 3rd party service to return the response, we simply let the worker do the workload of getting back from the 3rd party service and updating the ui (which reports waiting status) via websockets that the 3rd party service returned x/y or whatever status. 即当用户提交某个表单然后我们的应用程序需要联系第三方服务以返回响应时,我们只需让工作人员完成从第三方服务返回的工作量并通过websockets更新ui(报告等待状态)第三方服务返回x / y或任何状态。

  1. If we switch to jRuby, how will we achieve the similar logic? 如果我们切换到jRuby,我们将如何实现类似的逻辑? I mean do we go with the java code which has some kind of thread pool of workers and then free workers do the workload of contacting the 3rd party service etc? 我的意思是我们使用java代码,它有一些工作线程池,然后免费工作人员负责联系第三方服务等? How would we go about this if we decide to go with jRuby? 如果我们决定和jRuby一起去,我们将如何解决这个问题呢?

1) You can serve multiple requests at a time in jruby with nearly any container, but you can also serve multiple requests at a time with mri-ruby . 1)您可以使用几乎任何容器在jruby中一次提供多个请求,但您也可以使用mri-ruby一次提供多个请求 You only have to have a threadsafe app (config.threadsafe! is default in rails4). 您只需要一个线程安全的应用程序(config.threadsafe!在rails4中是默认的)。 Different rack servers have different approaches to serve multiple requests at a time. 不同的机架服务器有不同的方法一次服务多个请求。 For example unicorn uses multiple processes while passenger or puma go for a multi-threaded approach . 例如, 独角兽使用多个进程,而乘客或美洲狮则采用多线程方法

In my experience jruby containers like jboss or tomcat are more complicated to configure properly. 根据我的经验,像jboss或tomcat这样的jruby容器配置起来比较复杂。 But there are things like tourquebox , trinidad that help you with this. 但是像tourqueboxtrinidad这样的东西可以帮助你解决这个问题。 But you can even still go for some of the ruby servers (eg puma) that dont use c extensions. 但你甚至可以去一些不使用c扩展的ruby服务器(例如puma)。

2) If I understand you correctly you are looking for some background-processing library? 2)如果我理解你正确你正在寻找一些后台处理库? You can use sidekiq or resque with ruby or jruby (while jruby will be faster in general, and its easier to debug memory leaks). 您可以使用sidekiq或resque与ruby或jruby(而jruby一般会更快,并且更容易调试内存泄漏)。 You can even use ruby for your rack servers and jruby for your workers (can even be run in parallel with things like rvm/rbenv) 您甚至可以将ruby用于机架服务器,并为工作人员使用jruby(甚至可以与rvm / rbenv等并行运行)

In general I would only go for the jruby option if you know what you are doing and need better performance for your app servers or if you want to speed up your worker servers. 一般情况下,如果您知道自己在做什么,并且需要为应用服务器提供更好的性能,或者想要加快工作服务器的速度,我只会选择jruby选项。 If I was you I would probably stay in the ruby world and use puma for your app and sidekiq as a background service . 如果我是你,我可能会留在红宝石世界,并使用美洲狮为您的应用程序和sidekiq作为后台服务 Both are very elegant and need not so much configuration. 两者都非常优雅,不需要那么多配置。

  1. Yes, JRuby uses Java threads and is really multithreaded. 是的,JRuby使用Java线程,实际上是多线程的。 And I can say that it's really good in integration with Java, even using classes for JNI. 我可以说它与Java集成非常好,甚至使用JNI类。 I can recommend next servers (some have already been mentioned): 我可以推荐下一个服务器(有些已经提到):

The 'simplest' way to run application on servlet container is make .war with warbler. 在servlet容器上运行应用程序的“最简单”方法是make .war with warbler。 Usually resulting .war file includes all dependencies and JRuby interpreter, so resulting file usually is 30 Mb. 通常生成的.war文件包含所有依赖项和JRuby解释器,因此生成的文件通常为30 Mb。 But I think that it is not so easy to setup warbler, then I wouldn't recommend this way if you don't really need to run Rails in enterprise Java environment. 但我认为设置warbler并不容易,如果你真的不需要在企业Java环境中运行Rails,我就不会推荐这种方式。

And I would just remind that Rails opens DB connection for any request, then default size of DB connection pool of 5 isn't enough - don't forget to increase it before load testing :) (eg default thread pool for puma is 16, IBM WAS is 50, Tomcat - 200 threads). 我只想提醒Rails为任何请求打开数据库连接,然后默认大小为5的数据库连接池是不够的 - 不要忘记在加载测试之前增加它:)(例如,puma的默认线程池是16, IBM WAS是50,Tomcat - 200个线程)。

I agree with smallbutton.com that puma is good choice. 我同意smallbutton.com的说法 ,美洲狮是不错的选择。 Finally, with puma you can switch between JRuby and other interpreter almost easy (in my experience there is one difference - gem's names) 最后,使用puma你可以在JRuby和其他翻译之间切换几乎很容易(根据我的经验,有一个区别 - 宝石的名字)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM