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 {
@Autowired
private GroovyService groovyService;
@RequestMapping("/execute")
public String execute(String script) {
try {
groovyService.executeGroovyScript(script);
} catch (Exception e) {
return "fail:" + e.getMessage();
}
return "success";
}
}
总结:
线上的话,我们可以选择都有哪些服务器执行这些逻辑修复,从而快速执行Groovy脚本进行修复即可!!!