---
# System prepended metadata

title: Spring Boot Application Programmatically Monitoring and Restating
tags: [Monitor, SpringBoot]

---

###### tags: `SpringBoot` `Monitor`

# Spring Boot Application Programmatically Monitoring and Restating

## 使用對象
> 當專案使用Spring boot project建置時可以使用,例如Spring rest service

## 安裝方式
1. Adding the Actuator Dependency

```maven=
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter</artifactId>
</dependency>
```
2. Setting the application.properties

```properties=
#Spring Boot Restart Endpoint
management.endpoint.restart.enabled=true
management.endpoints.web.exposure.include=restart,health
#=====(Option)=====
server.servlet.contextPath=/definedContextPath
# Show details of health endpoint
management.endpoint.health.show-details=always
```

## 測試

### Monitor status
> curl GET http://localhost:{PORT}{CONTEXTPATH}/actuator/health
![get rest service status](https://i.imgur.com/3lzCktb.png)


### Restart service
> curl -X POST http://localhost:{PORT}{CONTEXTPATH}/actuator/restart
![restarting service](https://i.imgur.com/mhFeuFE.png)
![service restarted](https://i.imgur.com/Z2lJ4j0.png)

## 參考資源
* [Spring Boot Actuator: Production-ready Features](https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.enabling)
* [How to Restart Spring Boot Automatically using Actuator](https://javapointers.com/spring/spring-boot/how-to-restart-spring-boot-automatically-using-actuator/)
