首先导入devtools依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
在controller中如下:
@RestController
public class RestartController {
@Autowired
ApplicationContext applicationContext;
@RequestMapping("/restart")
public String restart() {
Restarter restarter = Restarter.getInstance();
restarter.restart(new FailureHandler() {
public Outcome handle(Throwable failure) {
System.out.println("当前系统出现问题,无法重启项目...........");
return Outcome.ABORT;
}
});
return "重启服务成功!";
}
}
启动类:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication application = new SpringApplicationBuilder(Application.class).build(args);
application.run();
}
}