1. 程式人生 > >2017年最新JRebel+Eclipse熱部署教程

2017年最新JRebel+Eclipse熱部署教程

在開發web專案時,常常修改很少的程式碼(例如配置檔案或java程式碼)就需要重啟伺服器,來檢視修改後的效果。如果專案比較大,重啟一次伺服器幾十秒,這簡直就是噩夢!

好在JRebel這個神器的存在,使得我們可以在修改程式碼後進行熱部署,即不需要重啟伺服器,就可以檢視修改後的效果!

下面記錄一下JRebel的安裝教程使用方法

Eclipse下JRebel的安裝和使用

0x0 下載安裝Jrebel(模仿大神們的裝逼排序方法)

  1. 開啟Eclipse—->Help—->EclipseMarketplace; 
    這裡寫圖片描述
  2. 在搜尋框輸入jrebel,回車搜尋; 
    這裡寫圖片描述
  3. 安裝完成後,eclipse右上角會多出一個檢視選項,同時Help選單中也多出一個JRebel的選單; 
    這裡寫圖片描述

    這裡寫圖片描述
  4. 點選Help—->JRebel—->Activation進入啟用介面; 
    這裡寫圖片描述
  5. 這裡要輸入啟用碼,然後點選啟用按鈕,途中按鈕和你們的不同是因為我的已經啟用。

0x1 啟用Jrebel

以下介紹如何獲取啟用碼(免費的哦):

  1. 註冊賬號密碼,並登陸;

  2. 登陸後,出現下圖,點選subscribe訂閱 
    這裡寫圖片描述

  3. 用twitter或者facebook進行訂閱(需要翻牆) 
    這裡寫圖片描述
  4. 訂閱成功後,點選install and active,就會出現喜聞樂見的啟用碼: 
    這裡寫圖片描述

  5. 在eclipse中輸入啟用碼啟用,點即右上角的JRebel,切換到Jrebel檢視,可以看到下圖: 
    這裡寫圖片描述

0x2 使用Jrebel

啟用完成後,JRebel已經預設配置好了。 
現在開始使用JRebel執行專案:

非Maven專案 
1. 右鍵點選你要使用JRebel的專案,會出現JRebelR選項,點選Add JRebel Nature,點選後src下會多出一個配置檔案rebel。xml 
選這個這個是為了可以監聽你專案中class檔案的變化,如果你修改了java程式碼,生成新的class後會被重新載入) 
這裡寫圖片描述 
多出來的配置檔案rebel.xml 
2. 可以執行專案了,右鍵專案,點選Run As—->Run Configuration,配置執行引數,然後Run 
這裡寫圖片描述
3. 看控制檯,出現以下內容說明JRebel監控成功:

2017-11-24 21:39:52 JRebel: Directory 'C:\Users\hasee\oxygen-workspace\crm01\build\classes'
will be monitored for changes. 2017-11-24 21:39:52 JRebel: Directory 'C:\Users\hasee\oxygen-workspace\crm01\WebContent' will be monitored for changes. 2017-11-24 21:39:55 JRebel: Starting logging to file: C:\Users\hasee\.jrebel\jrebel.log 2017-11-24 21:39:55 JRebel: 2017-11-24 21:39:55 JRebel: ############################################################# 2017-11-24 21:39:55 JRebel: 2017-11-24 21:39:55 JRebel: JRebel Agent 7.1.2 (201711071615) 2017-11-24 21:39:55 JRebel: (c) Copyright ZeroTurnaround AS, Estonia, Tartu. 2017-11-24 21:39:55 JRebel: 2017-11-24 21:39:55 JRebel: Over the last 2 days JRebel prevented 2017-11-24 21:39:55 JRebel: at least 6 redeploys/restarts saving you about 0.2 hours. 2017-11-24 21:39:55 JRebel: 2017-11-24 21:39:55 JRebel: Licensed to fly fly (using myJRebel). 2017-11-24 21:39:55 JRebel: 2017-11-24 21:39:55 JRebel: 2017-11-24 21:39:55 JRebel: ############################################################# 2017-11-24 21:39:55 JRebel: 2017-11-24 21:39:55 JRebel: Monitoring Log4j configuration in 'file:/C:/Users/hasee/oxygen-workspace/crm01/build/classes/log4j.properties'. 十一月 24, 2017 9:39:55 下午 org.apache.catalina.core.ApplicationContext log 資訊: Initializing Spring root WebApplicationContext 21:39:55,852 INFO ContextLoader:304 - Root WebApplicationContext: initialization started 21:39:55,979 INFO XmlWebApplicationContext:583 - Refreshing Root WebApplicationContext: startup date [Fri Nov 24 21:39:55 CST 2017]; root of context hierarchy 2017-11-24 21:39:56 JRebel: Monitoring Spring bean definitions in 'C:\Users\hasee\oxygen-workspace\crm01\build\classes\applicationContext.xml'. 21:39:56,197 INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [applicationContext.xml] 2017-11-24 21:39:56 JRebel: Monitoring properties in 'file:/C:/Users/hasee/oxygen-workspace/crm01/build/classes/druid.properties'.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

這裡寫圖片描述
這裡說明class檔案和webcontent中的檔案會被監視,如果發生改變則會重新部署

這裡寫圖片描述
這裡說明spring配置檔案applicationContext.xml和德魯伊連線池配置檔案會被監聽

  1. 測試下,修改一段程式碼,給Action中加一個方法
    /***********************************
     * Test JRebel
     * @return
     ***********************************/
    @Action(value = "test", results = {
            @Result(name = "test", location = "customer_list", type = "redirectAction")
    })
    public String testJRebel() {
        System.out.println("Hello JRebel");
        return "test";
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

看到控制檯輸出:

2017-11-24 21:45:43 JRebel: Reloading class 'com.fly.action.CustomerAction'.
  • 1

大功告成了

補充

如果安裝完成後專案無法啟動,報錯:

JRebel-JVMTI [FATAL] A fatal error occurred while proccessing the base java classes. The JVM has been shut down...
  • 1

極有可能是以下原因: 
1. eclipse所在路徑包含中文! 
2. 當前系統使用者資料夾包含中文