【通过Groovy去热修复线上逻辑】1.执行线上数据修复 2.写工具
1.执行groovy // 实际执行的话, 我们是通过vue管理系统提交
http://localhost:8080/groovy/execute?script=import com.example.groovytest.controller.LoginController; LoginController.num=251222
还有个技巧: 而执行执行的,则是: 写的工具什么的,想直接使用, 无需打jar包。
2.查询 // 可以看到,每次执行完groovy脚本,逻辑就修改了
http://localhost:8080/login/getNum
3.GroovyService
package com.example.groovytest.service;import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import org.springframework.stereotype.Service;@Service
public class GroovyService {public void executeGroovyScript(String script) {try{GroovyClassLoader loader = new GroovyClassLoader();Class<?> groovyClass = loader.parseClass(script);GroovyObject groovyObject = (GroovyObject) groovyClass.getDeclaredConstructor().newInstance();groovyObject.invokeMethod("run", null);}catch (Exception e){throw new RuntimeException(e);}}
}
4.LoginController
package com.example.groovytest.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/login")
public class LoginController {public static volatile int num = 111;@RequestMapping("/getNum")public String getNum() {return String.valueOf(num);}
}
5.GroovyController.java
package com.example.groovytest.controller;import com.example.groovytest.service.GroovyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/groovy")
public class GroovyController {@Autowiredprivate GroovyService groovyService;@RequestMapping("/execute")public String execute(String script) {try {groovyService.executeGroovyScript(script);} catch (Exception e) {return "fail:" + e.getMessage();}return "success";}
}
总结:
1.可见Groovy真的是脚本,我们是通过文本的方式当做字符串去提交的代码。
2.线上的话,我们可以选择都有哪些服务器执行这些逻辑修复,从而快速执行Groovy脚本进行修复即可。