-
Continue with the project you completed in Lab 1.
-
To
pom.xml
add the following dependency to include the starter for Spring Boot Actuator:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
-
Build the application:
$ ./mvnw package
-
Run the application:
$ java -jar target/hello-spring-boot-0.0.1-SNAPSHOT.jar
-
Try out the following endpoints. The output is omitted here because it can be quite large:
- http://localhost:8080/beans
-
Dumps all of the beans in the Spring context.
- http://localhost:8080/autoconfig
-
Dumps all of the auto-configuration performed as part of application bootstrapping.
- http://localhost:8080/configprops
-
Displays a collated list of all
@ConfigurationProperties
. - http://localhost:8080/env
-
Dumps the application’s shell environment as well as all Java system properties.
- http://localhost:8080/mappings
-
Dumps all URI request mappings and the controller methods to which they are mapped.
- http://localhost:8080/dump
-
Performs a thread dump.
- http://localhost:8080/trace
-
Displays trace information (by default the last few HTTP requests).
Spring Boot provides an endpoint (http://localhost:8080/info) that allows the exposure of arbitrary metadata.
One thing that it does well is expose information about the specific build and version control coordinates for a given deployment.
-
Add the following plugin to your Maven build. It will add Git branch and commit coordinates to the
/info
endpoint:<plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin</artifactId> <configuration> <dotGitDirectory>../../../.git</dotGitDirectory> </configuration> </plugin>
NoteThe path ../../../.git
refers to the.git
directory at the root of the course materials. -
Add the following properties to
src/main/resources/application.yml
:info: build: artifact: @project.artifactId@ name: @project.name@ description: @project.description@ version: @project.version@
These will add the project’s Maven coordinates to the
/info
endpoint. The Spring Boot Maven plugin will cause them to automatically be replaced in the assembled JAR. -
Build the application:
$ ./mvnw package
-
Run the application:
$ java -jar target/hello-spring-boot-0.0.1-SNAPSHOT.jar
-
Visit the application in the browser (http://localhost:8080/info), and verify that the output is similar to the following:
{ "git" : { "commit" : { "time" : "2015-12-06T17:25:32-0500", "id" : "78b0e1b" }, "branch" : "master" }, "build" : { "version" : "0.0.1-SNAPSHOT", "name" : "hello-spring-boot", "artifact" : "hello-spring-boot", "description" : "Demo project for Spring Boot" } }
Spring Boot provides an endpoint (http://localhost:8080/health) that allows for the notion of various health indicators.
-
Normally, when Spring Security is not enabled, the
/health
endpoint will only expose anUP
orDOWN
value. To simplify working with the endpoint for this lab, we will turn off its sensitivity. Add the following tosrc/main/resources/application.yml
:endpoints: health: sensitive: false
-
Create the class
io.pivotal.spring.FlappingHealthIndicator
and into it paste the following code:@Component public class FlappingHealthIndicator implements HealthIndicator{ private Random random = new Random(System.currentTimeMillis()); @Override public Health health() { int result = random.nextInt(100); if (result < 50) { return Health.down().withDetail("flapper", "failure").withDetail("random", result).build(); } else { return Health.up().withDetail("flapper", "ok").withDetail("random", result).build(); } } }
This demo health indicator will randomize the health check.
-
Build the application:
$ ./mvnw package
-
Run the application:
$ java -jar target/hello-spring-boot-0.0.1-SNAPSHOT.jar
-
Visit the application in the browser (http://localhost:8080/health), and verify that the output is similar to the following (and changes randomly!):
{ status: "UP", flapping: { status: "UP", flapper: "ok", random: 69 }, diskSpace: { status: "UP", free: 113632186368, threshold: 10485760 } }
Spring Boot provides an endpoint (http://localhost:8080/metrics) that exposes several automatically collected metrics for your application. It also allows for the creation of custom metrics.
-
Create the class
io.pivotal.spring.GreetingService
and into it paste the following code:@Component public class GreetingService { @Autowired CounterService counterService; @Value("${greeting}") String greeting; public String getGreeting() { counterService.increment("counter.services.greeting.invoked"); return greeting; } }
This class is using the
@Autowired
CounterService
to count the number of times that thegetGreeting()
method has been invoked. -
Refactor the contents of the class
io.pivotal.spring.HelloSpringBootApplication
:@Autowired private GreetingService greetingService; @RequestMapping("/") public String hello() { return String.format("%s World!", greetingService.getGreeting()); } public static void main(String[] args) { SpringApplication.run(HelloSpringBootApplication.class, args); }
hello()
is now delegating the source of the greeting to our newly createdGreetingService
. -
Build the application:
$ ./mvnw package
-
Run the application:
$ java -jar target/hello-spring-boot-0.0.1-SNAPSHOT.jar
-
Visit the application in the browser (http://localhost:8080) and refresh the page several times.
-
Now visit the
/metrics
endpoint (http://localhost:8080/metrics). Among the autogenerated metrics you should see acounter
for theGreetingService
invocations:counter.services.greeting.invoked: 16,
To learn more about the autogenerated metrics, visit http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html.