使用 java 函数从 Spring 引导调用 Spring 执行器/重启端点

Call Spring actuator /restart endpoint from Spring boot using a java function

我正在寻找重新启动 spring 引导应用程序,因此使用 Spring Actuator /restart 端点正在使用 curl,但我希望使用 [=16 调用相同的函数=] 来自应用程序内部的代码,我试过这段代码,但它不起作用:

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        RestartEndpoint p = new RestartEndpoint();
        p.invoke();
    }
});
thread.setDaemon(false);
thread.start();

您需要注入 RestartEndPoint:

@Autowired
private RestartEndpoint restartEndpoint;

...

Thread restartThread = new Thread(() -> restartEndpoint.restart());
restartThread.setDaemon(false);
restartThread.start();

它有效,即使它会抛出一个异常来通知您这可能会导致内存泄漏:

The web application [xyx] appears to have started a thread named [Thread-6] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:

  • 注意此 question/answer 的未来 reader,RestartEndPoint 未包含在 spring-boot-actuator 中,您需要添加 spring-cloud-context依赖。
  1. 在此处获取 json
@Autowired
private HealthEndpoint healthEndpoint;

public Health getAlive() {
    return healthEndpoint.health();
}
  1. 添加自定义逻辑