0% found this document useful (0 votes)
72 views29 pages

Spring Boot & Actuators: John Humphreys, VP in Systems Mgmt. Engineering Nomura Securities

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views29 pages

Spring Boot & Actuators: John Humphreys, VP in Systems Mgmt. Engineering Nomura Securities

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Spring Boot & Actuators

John Humphreys, VP in Systems Mgmt. Engineering


Nomura Securities
Email: [email protected]

1
© 2016 Pivotal
Agenda
§  Problems with Development
§  What is Spring Boot?
§  How do we use it?
§  Live coding – Build a spring boot app
§  Live coding – Add actuators for monitoring
§  Deployment as a service
§  Live coding - Hook up to the admin console

2 Spring Boot Actuators – Spring Days – John Humphreys


Problems With Development
In Java, and in general.

3 Spring Boot Actuators – Spring Days – John Humphreys


What problems are we trying to solve?
§  Developers keep re-solving the same problems.
•  The point of coding is to build your business value-added services; be they your
trading algorithms, your music service, etc.
•  Any time spent doing anything else is (usually) wasted.
§  Development with Java is mostly boiler-plate.
•  Web-applications are very common and require lots of configuration/setup.
•  Packaging, deployment, and monitoring take time (every time!). This is a waste
to your productivity; they are tangential to your business logic.
•  There are too many custom variations of essentially the same configuration,
project layout, and deployment (makes standardization/training hard).

4 Spring Boot Actuators – Spring Days – John Humphreys


What is Spring Boot?
An introduction to the framework

5 Spring Boot Actuators – Spring Days – John Humphreys


According to the project itself:


Spring Boot makes it easy to create
standalone, production-grade Spring
based applications that you can just run.
Spring.io

6 Spring Boot Actuators – Spring Days – John Humphreys


According to the project itself:


We take an opinionated view of the
platform and third-party libraries so that
you can get started with minimum fuss.
Spring.io

7 Spring Boot Actuators – Spring Days – John Humphreys


Key Features
§  Get Spring web-services running with just couple lines of code (literally).
§  Embed Tomcat or Undertow directly into them, providing a runnable JAR.
§  Automatically configure Spring wherever possible (you can actually run
without any configuration out of the box).
§  Make your WAR function as an init.d service.
§  Add standard metrics, health checks, and externalized configuration.
§  Integrate with a huge number of things out of the box; for example:
•  Consul for discoverability/distributed configuration.
•  Admin console for managing spring apps.

8 Spring Boot Actuators – Spring Days – John Humphreys


How Do We Use It?
A brief introduction.

9 Spring Boot Actuators – Spring Days – John Humphreys


Bare Application Code
You can actually start up a Spring Boot web-app with just this code (and a few
maven dependencies):

It will properly launch a web server and pick up any configuration files you
provide. But that’s all it does as we have no endpoints!
10 Spring Boot Actuators – Spring Days – John Humphreys
Adding an Endpoint
To make our service useful, we need to add a controller and endpoint.

Spring Boot will automatically component scan our classes (locate them and
wire them together) when it finds annotations like @Controller.
11 Spring Boot Actuators – Spring Days – John Humphreys
Maven Dependencies
To make the code we just saw work, we need just 2 things from maven.
1.  Derive from Spring Boot starter parent POM.
2.  Include the spring-boot starter web dependency.

12 Spring Boot Actuators – Spring Days – John Humphreys


Configuration File
Since we are trying to read a ${custom.message} property, we also need a
configuration file.
But by default we don’t need one, it would just run on port 8080 out of the
box; this is just specific to our code.
§  Add a “resources” folder under src/main
§  Add an application.properties file to it.
§  Add custom.message=Hello Spring Days!!!
§  You can also override the server defaults here:
•  server.port=[number]
•  server.contextpath=[/path]
13 Spring Boot Actuators – Spring Days – John Humphreys
What Does @SpringBootApplication Do?
It is syntactic sugar for multiple other powerful annotations commonly used
together in Spring. From the docs:

“ Many Spring Boot developers always have their


classes annotated with @Configuration,
@EnableAutoConfiguration, and
@ComponentScan…Spring Boot provides a
convenient @SpringBootApplication alternative.
Spring.io

14 Spring Boot Actuators – Spring Days – John Humphreys


What do @SpringBootApplication’s sub-annotations do?
§  @Configuration is used to turn a class into a JavaConfig source so you can
define beans in it, etc. The class used in SpringApplication.run() should be
a configuration class (though XML configs are possible).
§  @EnableAutoConfiguration attempts to guess and configure beans that you
are likely to need based on our code and class-path. E.g. if Tomcat is
present due to web dependencies in the POM, it will set it up and use it for
you.
§  @ComponentScan is used to tell Spring to automatically search for and
wire up classes based on their annotations (e.g. make @Controllers register
themselves, populate @Value variables, etc.)

15 Spring Boot Actuators – Spring Days – John Humphreys


Live Coding Session Part #1
Let’s make the application!

16 Spring Boot Actuators – Spring Days – John Humphreys


Spring Boot InitializR
Before we code this, just know that Spring Initializr can do it for you! I’m just
doing it to show you how easy it is J.

17 Spring Boot Actuators – Spring Days – John Humphreys


Live Coding Session Part #1
§  The code is hosted on GitHub; feel free to explore it.
•  https://github.com/w00te/spring-days-spring-boot-example
§  Also, to keep things co-located, here’s the admin console code for later on.
•  https://github.com/w00te/spring-days-spring-boot-admin-console
§  What we’ll achieve
•  Create a new Java 1.8 Maven application using the quick-start archetype.
•  Add in our maven dependencies.
•  Add in our main class and controller.
•  Add a properties file.
•  Test our application on our own custom-defined port/context.
18 Spring Boot Actuators – Spring Days – John Humphreys
Live Coding Session Part #2
Let’s add monitoring and deployment features!

19 Spring Boot Actuators – Spring Days – John Humphreys


Adding Actuator
§  Spring boot actuator provides production grade metrics, auditing, and
monitoring features to your application.
§  Just adding the actuator dependency makes it available on our 8080 port. If
we add hateoas as well, it will make it restfully navigable under /actuator.
No code is required J.
§  We’ll also disable actuator endpoint security to make this demo run
smoother of course J. We can do this by adding these two properties:

20 Spring Boot Actuators – Spring Days – John Humphreys


Actuator Features to Notice
§  Live thread dumps
§  Live stack traces
§  Logger configurations
§  Password-masked properties file auditing
§  Heap dump trigger (can be disabled)
§  Web-request traces
§  Application metrics
§  Spring bean analysis
§  More! (and it’s extensible)
21 Spring Boot Actuators – Spring Days – John Humphreys
Application Deployment
Deploying as an init.d service on Linux

22 Spring Boot Actuators – Spring Days – John Humphreys


Deploying as an Init.d Service (Part I)
§  Spring Boot builds an uber-JAR by default. So, the JAR it builds has all
dependencies required for your application to run.
§  It does this with the spring-boot-maven-plugin.
§  If you ask it to make the JAR executable as well, it will be runnable and will
support Linux init.d services (management with “service <name> start/stop/
restart/status, auto-start with OS, etc.).

23 Spring Boot Actuators – Spring Days – John Humphreys


Deploying as an Init.d Service (Part II)
§  Once you’ve made a JAR executable, you can view it in a text editor and you
will notice that there is literally a bash script at the top of your JAR above
the binary section; it’s pretty interesting to see.
§  External configuration:
•  Put a <jar-name>.conf file next to the JAR (or a sym-link to one).
•  Define your environment variables in it (e.g. JVM size, logging and property file
locations, etc).
•  It will automatically be picked up at run time.

24 Spring Boot Actuators – Spring Days – John Humphreys


Live Coding Session Part #3
Integrating with the Admin Console

25 Spring Boot Actuators – Spring Days – John Humphreys


Admin Console Server
§  Monitors your spring-boot applications and lets you interact with them.
§  Is a spring-boot application itself; include parent POM and a couple
dependencies, set ports in the same way. Run as a separate application.
http://codecentric.github.io/spring-boot-admin/1.5.0/#getting-started
§  Add a client-dependency to the apps you want to monitor (like the one we
just built) and set a property, and it works great! J

26 Spring Boot Actuators – Spring Days – John Humphreys


Admin Console Server Features
§  Dynamically change logging levels (e.g. add trace logging to debug a
request temporarily and know how to fix it in dev).
§  Perform JMX calls.
§  View your application metrics.
§  View web request and response pairs for your web-app (including headers).
§  View the environment, stack traces, etc.
§  Basically anything actuator exposes is beautifully represented here.
§  Applications are automatically detected and you get a log of when they start
up, shut down, etc.

27 Spring Boot Actuators – Spring Days – John Humphreys


28 Spring Boot Actuators – Spring Days – John Humphreys
The End
Any questions or comments?

29 Spring Boot Actuators – Spring Days – John Humphreys

You might also like