美文网首页
JRebel的idea本地热加载和远程热加载

JRebel的idea本地热加载和远程热加载

作者: CoderLJW | 来源:发表于2020-11-03 14:31 被阅读0次

    使用 JRebel 可以在修改代码后,动态重新加载修改的代码,免去了代码工程全量重建、重启的耗时流程,有效地提高开发者的效率。在 IDEA 的插件市场搜索 JRebel for IntelliJ 找到安装即可

    1、热加载本地

    • 1-安装 JRebel插件到idea。Preferences => Plugins


      image.png
    • 2-激活JRebel。我这里使用的url激活,如图,链接:填写推荐的,邮箱:随意填写


      image.png

    推荐链接
    https://www.cnblogs.com/bigshark/p/11343541.html
    https://www.hexianwei.com/2019/07/10/jrebel%E6%BF%80%E6%B4%BB/
    激活后,会自动配置JRebel的工程,跟随提示即可,完成后如图

    image.png
    • 3-查看是否配置完整,安装好后,默认会出现JRebel & XRebel的选项。【2】处的没有打勾,记得打勾


      image.png
    • 这里设置热加载反应时长,我这里选择1秒,实际测试中,在5秒左右。其他的保持默认


      image.png
    • 4-JReble的热加载,依赖于工程的配置,需要打开这里


      image.png
    • 5-启动项目,未安装JRebel插件前,【1】用于正常idea启动项目。安装JRebel后,如果热加载有效果,必须使用【2】来启动项目。【2】中的最后一个是用于,远程启动项目的,这个后面介绍


      image.png

      接下来修改了这里的代码,8888为9999,则5秒左右后,再次请求这个url。内容就变化了。至此本地热加载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
    
    image.png
    • 2-上传下载好的jrebel-2020.3.1-nosetup.zip到服务器,位置随意放置,我放在了 /usr/local/src 下面
    - 解压
    unzip jrebel-2020.3.1-nosetup.zip
    
    - 激活JRebel后,自动运行,这里使用上面的激活方式。产考官方给出的方式 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】


      image.png
    • 4-配置项目的JRebel。resources中的rebel.xml和rebel-remote.xml在【meishi】对应的勾勾那里,打勾了,才会出现。内容保持默认即可


      image.png

      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包的命令为
    - 官网怎么运行项目
    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连接,测试是否联通


      image.png
    • 8-运行本地项目,这个时候通过JRebel会和远程服务器那边联通,本地修改了代码,服务器那边会更新,延时是有的大概在8秒左右。

    注意:

    • 如果要实现本地修改了,远程也更新,则必须项目启动的时候使用如下方式
    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】来实现本地修改和远程更新

    相关文章

      网友评论

          本文标题:JRebel的idea本地热加载和远程热加载

          本文链接:https://www.haomeiwen.com/subject/uqwbvktx.html