Spring, Spring Boot, and Spring Boot Actuator

Spring Boot is Spring’s micro(service) framework built on top of spring platform. What it brings on top of spring is what makes it valuable for doing microservices in java.

Here are the few things that Spring boot provides which makes it indispensable:

  • First of all, the Auto Configuration and all the power that comes with it. You have the ElasticSearch, or Redis, or mongo in the classpath and you are set without doing anything. The thing that it is all transparent with some know-how of Spring, some debugging, and Spring Boot Actuator is even greater. So, you have this auto configure enabled but you can disable or modify it too. You can add your autoconfig too. Its like a fully functional framework within framework.
  • Second, it use maven to the hilt. Maven (or gradle) is such a strong framework. And with starter poms and all, your dependency and configuration code is very stripped down. There will still be cases when dependency will result into conflict. And given the native support of Java for this, that will be 20% of the cases easily. But still in 80% of the cases you are sorted.
  • Third, convention over configuration. Imagine remembering all the knobs and levers of running tomcat, or jetty, or undertow…. and the ever growing list of web servers. But not in Spring Boot. This has been time and again. Grails, Play, and take any other example in non Java world too. And convention over configuration win again 8 out of 10 times. Spring Boot is the (one of the) best in that regard in (pure) Java world.
  • Fourth, Configuration is outside the code. One of the main roadblock in going MicroServices way is too much of configuration. Your configuration multiplies 10 times straight (if I am benevolent) the moment you take the step from monolith to microservices. And managing all those configs in Boot is easy and not a pain. You can have centralised configuration too, if you are starting on greenfield projects (suggested way)
  • Fifth, Spring Boot Actuator. A good MicroServices architecture is as much about operations as code. You have to have a good micro(services) framework that is there with you at runtime too beside the dev, build etc. Now a) Spring Boot Actuator doesn’t get that much attention but it has tons of features where it can be used for Java MicroServices in production / live environment. Right from audit, health, info, autoconfig, security, metrics, heap memory, logging there are close to 13–15 production ready features in Spring Boot Actuator. b) There are many features still to be done and are still getting done with every release in Actuator.
  • You get embedded containers / web servers, fat jars. I am grouping these two. But specifically on the first part Spring Boot is not only about HTTP (Request / Response), you can have Event Driven Java MicroServices too with it. And lately, it is getting more traction as compare to HTTP, somewhat welcome trend, IMHO.

Switching back to Spring.

So, Spring Boot stands on the giant shoulders of Spring framework. And all of what is available in Spring, Spring Boot just makes it fun to work with.

I am not sure, but with GraalVM, Functional APIs, Kotlin support, etc. Spring would only go more places.

Earlish 2010, I feel Spring was on downward path. XML or even verbose Java config was not helping it out. Convention over Configuration was one master stroke that propelled it back. Spring Boot is simply the next level of that. Auto Configuration is even bigger than that.

Spring also reclassified the module. (I am coming back from early Spring 3 days. Where there was core spring and 7 modules. The spring is organized a bit differently from those days. I missed it I think. I don’t know when it changed.

Spring is lot of frameworks from the beginning. And Spring Boot hardly misses anything which is there in Spring. (Would love to know what is missing myself too.)


Spring Boot Actuator

Spring Boot Actuator: Production-ready features as it goes by the tag name gives you lot of features to manage Spring Java MicroServices. Additionally it doesn’t reinvent the wheel and same can be done over HTTP or JMX and uses lots of libraries and integrates with lots of libraries to provide those management.

As mentioned, this is an important part of MicroServices architecture which cannot be overstated.

Here are all the endpoints it exposes:

The endpoints are quite descriptive and what each endpoint exposes can be made sense out of it from the endpoint name.

Adding Spring boot actuator is matter of adding the starter pom for actuator just like other starters.

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

After that you get the benefit of conventions. As in the images above, all endpoints besides shutdown are enabled by default (good default and you don’t have to do anything for that)

Enabling shutdown goes like this, in properties file format (good defaulting again of the properties key name):

management.endpoint.shutdown.enabled=true

Then we can have Modifying Endpoints Response Cache TTL (they are cached by default), Securing Endpoints, changing the base path etc. I am not going into the details, hope you got the drift.

management.endpoints.web.base-path=/xyz
The latest version of Spring Boot Actuator integrates nicely with Spring Security. There were some problems earlier in terms of separate design, however they have been addressed.

What more? You can create your custom endpoints too. Like everything, it’s a framework. Standard @Endpoint and @Controller annotations can be used for creating custom actuator endpoints too.

Specifically mentioning about further two:

  1. /health is one of the endpoint as shown above. It integrates many inbuilt HealthIndictors for things like various DataStores, caches, DiskSpace etc. Extending like above custom HealthIndicators can also be written. There is support for ReactiveHealthIndicators and auto configured RecativeHealthIndicators too.
  2. Application Info endpoints and InfoContributor which includes infos like java info and git info etc.

A sample ExampleInfoContributer code, straight from Spring Boot Actuator docs:

import java.util.Collections;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

@Component
public class ExampleInfoContributor implements InfoContributor {

@Override
public void contribute(Info.Builder builder) {
builder.withDetail("example",
Collections.singletonMap("key", "value"));
}

}

That’s it for now. Hope you enjoyed my take on Spring Boot and Actuator.

References:

  1. https://docs.spring.io/spring-boot/docs/current/reference/html/index.html
  2. https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready.html
  3. https://docs.spring.io/spring/docs/current/spring-framework-reference/index.html
Like what you read? Give koolak82 a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.