Get familiar with the Spring Boot Actuator and its features.
Time: 20 minutes.
Code Snippet Manager Actuator
You will continue with the Code Snippet Manager code. For this lab you will need the snippet-code-manager-jpa-rest project code. You will reuse the code from previous labs.
-
Open a browser and hit the url: http://start.spring.io
-
Click the Switch to the full version link.
-
Fill out the Code Snippet Manager Actuator Project metadata with (See Figure 1.0):
Table 1. Code Snippet Manager Actuator - metadata Property Value Group:
io.pivotal.workshop
Artifact:
code-snippet-manager-actuator
Name:
code-snippet-manager-actuator
Package Name:
io.pivotal.workshop.snippet
Dependencies:
Web, DevTools, Groovy Templates, JPA, Rest Repositories, H2, MySQL, Actuator, HATEOAS
Spring Boot:
2.0.0.M7
Figure 1.0: Spring Initializr - http://start.spring.io/TipYou can choose either Maven or Gradle project types. -
Type Web, DevTools, Groovy Templates, JPA, Rest Repositories, H2, MySQL, Actuator, HATEOAS in the Dependencies field and press Enter.
-
Click the Generate Project button.
-
Unzip the file in any directory you want.
-
Import your project in any IDE you want.
-
Copy all the packages (with code) into the new project.
-
The packages: domain, repository and config will remain with no change.
Spring Boot Actuator: Review Endpoints
-
In the src/main/resources/application.properties:
-
Enable all the endpoints
-
Change the default path /actuator, instead use the path /admin
application.propertiesmanagement.endpoints.web.base-path=/admin management.endpoints.web.expose=*
-
-
Run the application and make sure all the endpoints are enable. Review all of them.
Spring Boot Actuator: HealthIndicator
Using the same code, imagine that we are going to use a FileSystem to save some information. A specific path, by using the snippet.path property. We need to know:
-
If the path doesn’t exists, report out of service.
-
If the path exists but doesn’t have writing permissions, report system down.
-
If the path exists and it has write permissions, report up.
-
Let’s create a properties holder class. Add the SnippetProperties class in the io.pivotal.workshop.snippet.config package:
io.pivotal.workshop.snippet.config.SnippetProperties.javapackage io.pivotal.workshop.snippet.config; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "snippet") public class SnippetProperties { private String path; public String getPath() { return path; } public void setPath(String path) { this.path = path; } }
The above code will hold the information about the path with the property: snippet.path.
-
Create the SnippetHealthCheck class in the io.pivotal.workshop.snippet.actuator package:
io.pivotal.workshop.snippet.actuator.SnippetHealthCheck.javapackage io.pivotal.workshop.snippet.actuator; import java.io.File; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component public class SnippetHealthCheck implements HealthIndicator { private String path; public SnippetHealthCheck(@Value("${snippet.path:/tmp}")String path){ this.path = path; } @Override public Health health() { try { File file = new File(path); if(file.exists()){ if(file.canWrite()) return Health.up().build(); return Health.down().build(); }else{ return Health.outOfService().build(); } }catch(Exception ex) { return Health.down(ex).build(); } } }
Take a look at the constructor. If the snippet.path is not found, it will use the /tmp as default path.
-
In the src/main/resources/application.properties file add the following property:
## Snippet snippet.path=/tmp/snippets
TipDo not create this folder yet, we will make sure our custom health indicator works. TipIf you are using Windows try to use the C:/tmp/snippets or C:\\tmp\\snippets format. -
If you run the application and go to the http://localhost:8080/admin/health, you should have something like the following Figure 3.0:
Figure 3.0: Spring Boot Actuator - http://localhost:8080/admin/healthHere you can see the SnippetHealthCheck value: OUT_OF_SERVICE.
-
Create the /tmp/snippets (or C:\tmp\snippets for Windows) path but add/modify only read access, and then you can refresh the page, you should see the same as Figure 4.0:
Figure 4.0: Spring Boot Actuator - http://localhost:8080/admin/healthHere you can see the SnippetHealthCheck value: DOWN.
-
Modify the path to write access and refresh, you should have the following, see Figure 5.0.
Figure 5.0: Spring Boot Actuator - http://localhost:8080/admin/healthHere you can see the SnippetHealthCheck value: UP.
CHALLENGES
-
Add a graceful shutdown to the application by executing the following command:
curl -X POST localhost:8080/admin/shutdown
TipIf you are a Windows user, you can use the POSTMAN (https://www.getpostman.com/) application to execute the POST.
HOMEWORK
Review the Micrometer technology (http://micrometer.io) and apply it to the Code Snippet Manager.