使用 JRebel 可以在修改代碼后惶岭,動態(tài)重新加載修改的代碼,免去了代碼工程全量重建犯眠、重啟的耗時流程按灶,有效地提高開發(fā)者的效率。在 IDEA 的插件市場搜索 JRebel for IntelliJ 找到安裝即可
1筐咧、熱加載本地
-
1-安裝 JRebel插件到idea鸯旁。Preferences => Plugins
-
2-激活JRebel。我這里使用的url激活量蕊,如圖铺罢,鏈接:填寫推薦的,郵箱:隨意填寫
推薦鏈接
https://www.cnblogs.com/bigshark/p/11343541.html
https://www.hexianwei.com/2019/07/10/jrebel%E6%BF%80%E6%B4%BB/
激活后残炮,會自動配置JRebel的工程韭赘,跟隨提示即可,完成后如圖
-
3-查看是否配置完整势就,安裝好后泉瞻,默認會出現(xiàn)JRebel & XRebel的選項“耄【2】處的沒有打勾袖牙,記得打勾
-
這里設置熱加載反應時長,我這里選擇1秒舅锄,實際測試中鞭达,在5秒左右。其他的保持默認
-
4-JReble的熱加載巧娱,依賴于工程的配置碉怔,需要打開這里
-
5-啟動項目,未安裝JRebel插件前禁添,【1】用于正常idea啟動項目撮胧。安裝JRebel后,如果熱加載有效果老翘,必須使用【2】來啟動項目芹啥。【2】中的最后一個是用于铺峭,遠程啟動項目的墓怀,這個后面介紹
接下來修改了這里的代碼,8888為9999卫键,則5秒左右后傀履,再次請求這個url。內(nèi)容就變化了莉炉。至此本地熱加載OKOKOKOKOK
@GetMapping(value = "/test")
public RestResponse test(){
return RestResponse.success("test 8888");
}
2钓账、遠程熱加載
- 1-在遠程的服務器也需要安裝JRebel
- 下載JRebel碴犬,我這里下載的是最新版本
https://www.jrebel.com/products/jrebel/download/prev-releases
- 2-上傳下載好的jrebel-2020.3.1-nosetup.zip到服務器,位置隨意放置梆暮,我放在了 /usr/local/src 下面
- 解壓
unzip jrebel-2020.3.1-nosetup.zip
- 激活JRebel后服协,自動運行,這里使用上面的激活方式啦粹。產(chǎn)考官方給出的方式 https://manuals.jrebel.com/jrebel/standalone/activate.html#command-line-utility 查看Command line utility的激活方法偿荷。
./activate.sh https://jrebel.hexianwei.com/bbacb537-d186-4a29-9737-36a0e737235e 3344433@qq.com
- 設置密碼 必須的
java -jar jrebel.jar -set-remote-password xxxx
這里服務器端設置JRebel設置完成
-
3-設置idea端。如果不直接遠程運行項目唠椭,這里可以保持默認跳纳,還是使用【Run via IDE】
-
4-配置項目的JRebel。resources中的rebel.xml和rebel-remote.xml在【meishi】對應的勾勾那里泪蔫,打勾了棒旗,才會出現(xiàn)喘批。內(nèi)容保持默認即可
rebel.xml
<?xml version="1.0" encoding="UTF-8"?>
<application generated-by="intellij" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://update.zeroturnaround.com/jrebel/rebel-2_3.xsd">
<id>meishi</id>
<classpath>
<dir name="/Users/paycloud110/sss/meishi/target/classes">
</dir>
</classpath>
</application>
rebel-remote.xml
<?xml version="1.0" encoding="UTF-8"?>
<rebel-remote xmlns="http://www.zeroturnaround.com/rebel/remote">
<id>com.sailundipu.ljw.meishi</id>
</rebel-remote>
- 5-項目打包成常用的jar包撩荣,部署到服務器上。這個時候服務器啟動jar包的命令為
- 官網(wǎng)怎么運行項目
https://manuals.jrebel.com/jrebel/remoteserver/serverconfiguration.html
nohup java -agentpath:/usr/local/src/jrebel/lib/libjrebel64.so -Drebel.remoting_plugin=true -jar /usr/local/java/meishi_jar/meishi.jar >meishi.log &
- 6-項目正常啟動后饶深,我這里使用的端口是9400
2020-11-03 14:59:48 JRebel: (c) Copyright 2007-2020 Perforce Software, Inc.
2020-11-03 14:59:48 JRebel:
2020-11-03 14:59:48 JRebel: Over the last 1 days JRebel prevented
2020-11-03 14:59:48 JRebel: at least 0 redeploys/restarts saving you about 0 hours.
2020-11-03 14:59:48 JRebel:
2020-11-03 14:59:48 JRebel: JRebel started in remote server mode.
2020-11-03 14:59:48 JRebel:
2020-11-03 14:59:48 JRebel:
2020-11-03 14:59:48 JRebel: #############################################################
2020-11-03 14:59:48 JRebel:
省了N多餐曹。。敌厘。台猴。。俱两。饱狂。。宪彩。休讳。。尿孔。俊柔。。活合。雏婶。。
2020-11-03 15:00:04.669 [main] INFO [org.springframework.boot.web.embedded.tomcat.TomcatWebServer:92]-Tomcat initialized with port(s): 9400 (http)
2020-11-03 15:00:04.776 [main] INFO [org.apache.coyote.http11.Http11NioProtocol:173]-Initializing ProtocolHandler ["http-nio-9400"]
2020-11-03 15:00:04.778 [main] INFO [org.apache.catalina.core.StandardService:173]-Starting service [Tomcat]
2020-11-03 15:00:04.779 [main] INFO [org.apache.catalina.core.StandardEngine:173]-Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-11-03 15:00:05.127 [main] INFO [org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]:173]-Initializing Spring embedded WebApplicationContext
2020-11-03 15:00:05.128 [main] INFO [org.springframework.web.context.ContextLoader:284]-Root WebApplicationContext: initialization completed in 7456 ms
2020-11-03 15:00:10.117 [main] INFO [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor:181]-Initializing ExecutorService 'applicationTaskExecutor'
2020-11-03 15:00:10.214 [main] WARN [org.springframework.boot.devtools.autoconfigure.OptionalLiveReloadServer:61]-Unable to start LiveReload server
2020-11-03 15:00:12.325 [main] INFO [org.apache.coyote.http11.Http11NioProtocol:173]-Starting ProtocolHandler ["http-nio-9400"]
2020-11-03 15:00:12.420 [main] INFO [org.springframework.boot.web.embedded.tomcat.TomcatWebServer:204]-Tomcat started on port(s): 9400 (http) with context path
2020-11-03 15:00:12.432 [main] INFO [com.sailundipu.ljw.shop.ShopApplication:61]-Started ShopApplication in 18.884 seconds (JVM running for 27.553)
-
7-接下來要配置本地idea和服務器的遠程連接
【2】名字隨意寫白指,自己記得就行
【3】服務器的ip和項目的運行端口留晚。我這里填寫http://192.168.31.220:9400
【4】服務器那里的JRebel設置了密碼,這里填寫設置的密碼
【5】點擊Test Connection連接告嘲,測試是否聯(lián)通
- 8-運行本地項目错维,這個時候通過JRebel會和遠程服務器那邊聯(lián)通憨闰,本地修改了代碼,服務器那邊會更新需五,延時是有的大概在8秒左右鹉动。
注意:
- 如果要實現(xiàn)本地修改了,遠程也更新宏邮,則必須項目啟動的時候使用如下方式
nohup java -agentpath:/usr/local/src/jrebel/lib/libjrebel64.so -Drebel.remoting_plugin=true -jar /usr/local/java/meishi_jar/meishi.jar > meishi.log &
- 也可以使用【Run via IDE】來實現(xiàn)本地修改和遠程更新