CLI and Actuator

Spring Boot CLI

We can write groovy applications and they don’t require any conventional Java project structure, imports, or even compilation.

// app.groovy
@RestController
class HelloController{
	@GetMapping("/")
	String hello(){
		return "Hello Groovy!";
	}
}

// run with: $ spring run app.groovy

Spring Boot CLI has removed the run command. How to run Groovy scripts effortlessy as it used to be done like above remains unanswered. Ref

Actuator

Used to: Monitor and view internals of the Spring Boot application in runtime, we can control it too (like /shutdown).

Use below dependency to use actuator:

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
</dependencies>

Endpoints are exposed at: localhost:8080/actuator/<endpoint_name>. By default only the /health endpoint is exposed.

You can enable or disable each individual endpoint and expose them (make them remotely accessible) over HTTP or JMX. An endpoint is accessible only when it is both enabled and exposed.

Some Common Endpoints

/beans			-> shows all beans' dependencies, scope, type  
/autoconfig 	-> all autoconfigs done by Spring Boot
/metrics		-> applications metrics like memory, heap, threads
/health			-> "UP" status and diskSpace

/info
/shutdown

Custom Information (/info)

We can send our own definined information on the /info endpoint. Specify info using properties.

info.name.first=Abhishek
info.name.last=Arya
info.address=India

management.info.env.enabled=true
// hit /info with GET and we get the following JSON
{
	"name": {
		"first": "Abhishek",
		"last": "Arya"
	},

	"address": "India"
}

/shutdown

It’s disabed by default since it can kill the app. Enable it by property as follows:

management.endpoint.shutdown.enabled=true

Hit with POST and it gives response back. This is the only endpoint with POST, all are GET otherwise.

// request - POST http://localhost:8080/shutdown

// response
{
 "message": "Shutting down, bye..."
}

Customize Endpoints

Enable/Disable Endpoints

By default, all endpoints except /shutdown are enabled. We can often disable all (1) and enable only the ones we need (2).

management.endpoint.<endpoint_id>.enabled=false

(1)
management.endpoints.enabled-by-default=false
(2)
management.endpoint.info.enabled=true

Here endpoint_id is nothing but name of the endpoint like health, shutdown, etc…

Exposing Endpoints

expose only health and info:
management.endpoints.web.exposure.include=health,info

expose all:
management.endpoints.web.exposure.include=*

exclude env and beans:
management.endpoints.web.exposure.exclude=env,beans

Only the /health endpoint is exposed by default.

More

We can change /actuator base path to anything we like /foobar.

management.endpoints.web.base-path=/foobar

We can change endpoint_id, toggle sensitivity, secure endpoints with authentication.

We can write custom health indicators, or an entire custom endpoint.

References: