SlideShare a Scribd company logo
1 of 21
Download to read offline
JRuby 在 Rails 上的应用


    -- 安装、迁移、部署和源码保护


   唐力群 crazycode@gmail.com
     http://sprintonrails.com
目录
    JRuby 介绍
●



    JRuby 安装
●



    JRuby on Rails 快速安装
●



    Warbler: 打包发布到应用服务器
●



    JRuby 编译
●



    介绍 Rails 编译打包脚本
●



    已知的限制
●
为什么要 JRuby
    Java 原生支持
●



    广泛的 Java 资源
●



    保护知识产权(编译支持)
●
案例
    Thoughtworks 的 Mingle 是一个基于 Rails 开
●


    发的敏捷项目管理工具,运行 JRuby 上,是首
    个商用 JRuby on Rails 产品,通过 JRuby 实现
    了源代码保护,实现二进制产品发布
        参考 http://www.thoughtworks.com/mingle
    –
JRuby 安装
    从 http://jruby.codehaus.org 下载最新的版本,目前稳定版是
●


    1.1.6
    压缩到一个目录,例如 /opt/jruby-1.1.6
●



    设置环境变量:
●



        设置 JRUBY_HOME 指向 /opt/jruby-1.1.6
    –

        把 $JRUBY_HOME/bin 加到 $PATH
    –

    环境变量生效后,运行 jruby -v 验证安装成功
●



        $ jruby -v
        jruby 1.1.6 (ruby 1.8.6 patchlevel 114) (2008-12-17 r
        ev 8388) [i386-java]
JRuby on Rails 安装
    幸运的是, JRuby1.1.6 自带了 gem1.3.1
●



    通过以下命令安装 Rails 支持:
●



     $ jruby -S gem install mongrel rails 
     $ jruby -S gem install activerecord-jdbcmysql-
     adapter
Rails 快速开始
    通过以下命令可以创建一个使用 mysql 的应用:
●



      $ jruby -S rails jrorapp -d mysql 
    当然, config/database.yml 需要修改 adapter 为 jdbcmysql :
●



      development:
        adapter: jdbcmysql
        encoding: utf8
        database: jrorapp_development
        username: root
        password: password
    现在,我们可以创建数据库了:
●




      $  jruby -S rake db:create:all 
Rails 快速开始
    使用 Scaffold 生成一个例子:
●



      $ jruby script/generate scaffold post title:strin
      g body:text published:boolean
    执行 Migration 和启动 app:
●



      $ jruby -S rake db:migrate
      $ jruby script/server
    现在就可以测试: http://localhost:3000/posts
●
从 CRuby 应用迁移到 JRuby
 JRuby 对 Rails 的兼容性已经相当完美了,需要
 考虑的更多是插件的兼容性
 基本上只需要修改 database.yml ,以及确保
 所使用的 gems/plugins 能够正常使用(或有 jr
 uby 替代版本即可)
 在 jrorapp 的 github 中提供了 cruby/jruby 两
 个分支版本,实际上只有 database.yml 的差
 异
JRuby/CRuby 性能比较

 基于前面生成的应用测试,包括有数据库访问
 和页面生成
 测试 CRuby/JRuby 在各种部署形式下负载能
 力的差异
 使用 pen 做简单的负载均衡
  $ sudo pen -r 80 localhost:3000 localhost 3
  001 localhost:3002
 使用 Apache Bench 工具:
  $ ab -n 500 -c 10 http://localhost/posts
测试结果 -CRuby/JRuby 比较
 200
                                 184.35
 180


 160
               143.45
 140

                        119.47
 120
                                                     108
                                                             单进程
                                          97.38
 100   92.19
                                                             三进程
  80


  60


  40


  20


   0
       Cruby/Mongrel      Cruby/Thin      Jruby1.1/Mongrel
测试汇总 - 虚拟机比较
Jruby 通过以下 JVM 参数可以得到一定优化 :
       -Xmx1024m -Xms512m -Djruby.thread.pooling=true -Djruby.objectspace.enabled=false

 160


                                                             135.6
 140                                           134.2


 120


                                 95.18
 100
               90.43

                                                                          未优化
  80
                                                                          优化后
  60


  40


  20


   0
                       Sun JVM                     Bea Jrockit
使用 Warbler 打包
    Warbler 是把 rails app 打包成 war 的工
●


    具,目前版本使用 JRuby1.1.6 和 JRubyR
    ack0.9.3 ,而不是使用较旧的 GoldSpike
    为什么是 Warbler?
●


    – 简单,专注于打包
    – 灵活,易于扩展
使用 Warbler 打包
安装:
  $ jruby -S gem install warbler
首先是配置当前应用 :
  $ jruby -S warbler config
将生成 config/warbler.rb ,修改加入 mysql 相关的 gem
  config.gems += [quot;activerecord-jdbc-adapterquot;, quot;jruby-opensslquot;, quot;a
  ctiverecord-jdbcmysql-adapterquot;, quot;jdbc-mysqlquot;]
打包 rails application:
  $ jruby -S warbler
可以在当前目录生成 war 文件,发布到 JavaEE Application 服务器即可
编译 Rails
    可以使用 JRuby1.1 引入的 AOT(Ahead of
●


    Time) 特性编译 rails 代码,包括 plugins 都可
    以编译
    通过修改 activesupport 的加载接口,使用 rail
●


    s 可识别 .class 后缀
    在 Warbler 中加入编译 Rails 功能,简化产品
●


    打包发布流程 (Warbler-AOT)
Warbler-AOT
 修改 activesupport 的 dependencies.rb:
Warbler-AOT
 为 warbler 加入了新的打包方法:
  war:aot 编译 rails 文件后打包
  war:aot 同时会修改 rails2.2.2 的 dependencies.r
  b 文件(简单覆盖),以支持 .class 文件
 修改后的 warbler 发布在 http://github.com/c
 razycode/warbler ,可直接以插件文件安装:
  $ script/plugin install git://github.com/crazyco
  de/warbler.git
Warbler 使用效果
JRuby 选择时必须考虑的问题
    虽然已经做了大量的改进,目前在性能上和原生
●


    Ruby 还是有一定差距,特别是 Ruby1.9.1 在
    这方面取得了长足进步的情况下
    需要本地编译的 gem 插件不能直接安装使用,
●


    如 thin
    Rmagick :提供了一个 rmagick4j ,据说可以
●


    和 attachment_fu 兼容,但我使用的 flexima
    ge 不兼容
参考信息
    http://wiki.jruby.org
●



    http://github.com/craycode 本次演讲的源码
●
谢谢!




      Q&A

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

JRuby在Rails中的应用

  • 1. JRuby 在 Rails 上的应用 -- 安装、迁移、部署和源码保护 唐力群 crazycode@gmail.com http://sprintonrails.com
  • 2. 目录 JRuby 介绍 ● JRuby 安装 ● JRuby on Rails 快速安装 ● Warbler: 打包发布到应用服务器 ● JRuby 编译 ● 介绍 Rails 编译打包脚本 ● 已知的限制 ●
  • 3. 为什么要 JRuby Java 原生支持 ● 广泛的 Java 资源 ● 保护知识产权(编译支持) ●
  • 4. 案例 Thoughtworks 的 Mingle 是一个基于 Rails 开 ● 发的敏捷项目管理工具,运行 JRuby 上,是首 个商用 JRuby on Rails 产品,通过 JRuby 实现 了源代码保护,实现二进制产品发布 参考 http://www.thoughtworks.com/mingle –
  • 5. JRuby 安装 从 http://jruby.codehaus.org 下载最新的版本,目前稳定版是 ● 1.1.6 压缩到一个目录,例如 /opt/jruby-1.1.6 ● 设置环境变量: ● 设置 JRUBY_HOME 指向 /opt/jruby-1.1.6 – 把 $JRUBY_HOME/bin 加到 $PATH – 环境变量生效后,运行 jruby -v 验证安装成功 ● $ jruby -v jruby 1.1.6 (ruby 1.8.6 patchlevel 114) (2008-12-17 r ev 8388) [i386-java]
  • 6. JRuby on Rails 安装 幸运的是, JRuby1.1.6 自带了 gem1.3.1 ● 通过以下命令安装 Rails 支持: ● $ jruby -S gem install mongrel rails  $ jruby -S gem install activerecord-jdbcmysql- adapter
  • 7. Rails 快速开始 通过以下命令可以创建一个使用 mysql 的应用: ● $ jruby -S rails jrorapp -d mysql  当然, config/database.yml 需要修改 adapter 为 jdbcmysql : ● development: adapter: jdbcmysql encoding: utf8 database: jrorapp_development username: root password: password 现在,我们可以创建数据库了: ● $  jruby -S rake db:create:all 
  • 8. Rails 快速开始 使用 Scaffold 生成一个例子: ● $ jruby script/generate scaffold post title:strin g body:text published:boolean 执行 Migration 和启动 app: ● $ jruby -S rake db:migrate $ jruby script/server 现在就可以测试: http://localhost:3000/posts ●
  • 9. 从 CRuby 应用迁移到 JRuby JRuby 对 Rails 的兼容性已经相当完美了,需要 考虑的更多是插件的兼容性 基本上只需要修改 database.yml ,以及确保 所使用的 gems/plugins 能够正常使用(或有 jr uby 替代版本即可) 在 jrorapp 的 github 中提供了 cruby/jruby 两 个分支版本,实际上只有 database.yml 的差 异
  • 10. JRuby/CRuby 性能比较 基于前面生成的应用测试,包括有数据库访问 和页面生成 测试 CRuby/JRuby 在各种部署形式下负载能 力的差异 使用 pen 做简单的负载均衡 $ sudo pen -r 80 localhost:3000 localhost 3 001 localhost:3002 使用 Apache Bench 工具: $ ab -n 500 -c 10 http://localhost/posts
  • 11. 测试结果 -CRuby/JRuby 比较 200 184.35 180 160 143.45 140 119.47 120 108 单进程 97.38 100 92.19 三进程 80 60 40 20 0 Cruby/Mongrel Cruby/Thin Jruby1.1/Mongrel
  • 12. 测试汇总 - 虚拟机比较 Jruby 通过以下 JVM 参数可以得到一定优化 : -Xmx1024m -Xms512m -Djruby.thread.pooling=true -Djruby.objectspace.enabled=false 160 135.6 140 134.2 120 95.18 100 90.43 未优化 80 优化后 60 40 20 0 Sun JVM Bea Jrockit
  • 13. 使用 Warbler 打包 Warbler 是把 rails app 打包成 war 的工 ● 具,目前版本使用 JRuby1.1.6 和 JRubyR ack0.9.3 ,而不是使用较旧的 GoldSpike 为什么是 Warbler? ● – 简单,专注于打包 – 灵活,易于扩展
  • 14. 使用 Warbler 打包 安装: $ jruby -S gem install warbler 首先是配置当前应用 : $ jruby -S warbler config 将生成 config/warbler.rb ,修改加入 mysql 相关的 gem config.gems += [quot;activerecord-jdbc-adapterquot;, quot;jruby-opensslquot;, quot;a ctiverecord-jdbcmysql-adapterquot;, quot;jdbc-mysqlquot;] 打包 rails application: $ jruby -S warbler 可以在当前目录生成 war 文件,发布到 JavaEE Application 服务器即可
  • 15. 编译 Rails 可以使用 JRuby1.1 引入的 AOT(Ahead of ● Time) 特性编译 rails 代码,包括 plugins 都可 以编译 通过修改 activesupport 的加载接口,使用 rail ● s 可识别 .class 后缀 在 Warbler 中加入编译 Rails 功能,简化产品 ● 打包发布流程 (Warbler-AOT)
  • 16. Warbler-AOT 修改 activesupport 的 dependencies.rb:
  • 17. Warbler-AOT 为 warbler 加入了新的打包方法: war:aot 编译 rails 文件后打包 war:aot 同时会修改 rails2.2.2 的 dependencies.r b 文件(简单覆盖),以支持 .class 文件 修改后的 warbler 发布在 http://github.com/c razycode/warbler ,可直接以插件文件安装: $ script/plugin install git://github.com/crazyco de/warbler.git
  • 19. JRuby 选择时必须考虑的问题 虽然已经做了大量的改进,目前在性能上和原生 ● Ruby 还是有一定差距,特别是 Ruby1.9.1 在 这方面取得了长足进步的情况下 需要本地编译的 gem 插件不能直接安装使用, ● 如 thin Rmagick :提供了一个 rmagick4j ,据说可以 ● 和 attachment_fu 兼容,但我使用的 flexima ge 不兼容
  • 20. 参考信息 http://wiki.jruby.org ● http://github.com/craycode 本次演讲的源码 ●
  • 21. 谢谢! Q&A