Wednesday, October 21, 2020

Spring Boot Packaging

 

Spring Boot Packaging

In the J2EE application, modules are packed as JAR, WAR, and EAR. It is the compressed file formats that is used in the J2EE. J2EE defines three types of archives:

  • WAR
  • JAR
  • EAR

 

EAR > WAR > JAR

WAR

WAR stands for Web Archive.  WAR file represents the web application. Web module contains servlet classes, JSP files, HTML files, JavaScripts, etc. are packaged as a JAR file with .war extension. It contains a special directory called WEB-INF.

WAR file = JAR file + WEB-INF

 JAR

 JAR stands for Java Archive. An EJB (Enterprise Java Beans) module that contains bean files (class files), a manifest, and EJB deployment descriptor (XML file) are packaged as JAR files with the extension .jar. It is used by software developers to distribute Java classes and various metadata.

 JAR file = Java classes + manifest + descriptor.

EAR

EAR stands for Enterprise Archive. EAR file represents the enterprise application. The above two files are packaged as a JAR file with the .ear extension. It is deployed into the Application Server.It is a special JAR that contains an application.xml file in the META-INF folder.

EAR file = multiple JAR's and WAR's

 

Spring Actuator

 

 

What is Spring Actuator?

Spring Boot Actuator is available from the very first release of Spring Boot. It proves several features like health check-up, auditing JVM metrics, log information, caching statics, etc. We have the option to use JMX or HTTP end points to manage and monitor our applications on the production environment.

Actuator also make it easy to integrate with external monitoring systems like Prometheus, Graphite, DataDog, New Relic, etc. The Actuator uses for Micrometer, an application metrics facade to support the integration with external monitoring systems.Actuator make it super easy to integrate our application with any external system with a very minimal configurations.

 

Enabling

If we use Sprng Initializer we have to add Spring Web Starter and Spring Boot Actuator starter.

On pom.xml you will be able to see it:

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


Verifying 

Spring boot actuator creates and expose the end points. Actuator end points allow us to monitor and interact with our application. Spring Boot 2 provides the option to expose these end points as  JMX or HTTP end points. These different end points provide specific information / status for our application, let’s briefly cover a few of these endpoints.

Once you start your application you will be able to see  if it is working using the following endpoint:

https://localhost:8080//actuator

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/actuator",
      "templated": false
    },
    "health": {
      "href": "http://localhost:8080/actuator/health",
      "templated": false
    },
    "health-path": {
      "href": "http://localhost:8080/actuator/health/{*path}",
      "templated": true
    },
    "info": {
      "href": "http://localhost:8080/actuator/info",
      "templated": false
    }
  }
}


To enable all endpoints you need to change your application.properties:

management.endpoints.web.exposure.include=*

 

The most basic actuator is the health endpoint which provides basic application health information.

 http://localhost:8080/actuator/health

 

{ "status": "UP" } 


The healthendpoint will show status as DOWNif there is any issue within the application like database connection or lower disk space etc.

The info endpoint display general information from the standard files like META-INF/build-info.properties.Initially this will be empty but you can add information to your application.properties to be shown here.

## Configuring info endpoint
info.app.name=Spring Sample Application
info.app.description=This is my first spring boot application
info.app.version=1.0.0

{
  "app": {
    "name": "Spring Sample Application",
    "description": "This is my first spring boot application",
    "version": "1.0.0"
  }
}


Spring Boot Actuator Endpoints

There are several endpoints that can be enable on your application. There are also some additional endpoints available for Spring MVC, Spring WebFlux, or Jersey.

 

Enable/Disable Actuator Endpoints

 Spring boot actuator make it easy to enable or disable any end point using the application. properties file. To enable an endpoint, use the management.endpoint.<id>.enabled property in the application.properties file.

 To enable beans endpoint:

management.endpoint.beans.enabled=true

To include/exclude endpoints over HTTP we should configure the properties through the application.properties.

management.endpoints.web.exposure.include=health,info
management.endpoints.web.exposure.exclude=

 To expose all endpoints:

management.endpoints.web.exposure.include=*

It is not recommended to expose all endpoints for security reasons.

When you start your application you can actually see those endpoint your have started like this:

Exposing 13 endpoint(s) beneath base path '/actuator'

/metrics Endpoint

The metrics endpoint publishes information about the current application which includes  OS, JVM information, memory and heap information, thread pool, etc.

With '/actuator/metrics' you will see which metrics are available like: "http.server.requests", "jvm.buffer.count", "jvm.buffer.memory.used", "jvm.buffer.total.capacity", ...

With 'actuator/metrics/'metric_name you can see details of each metric.

Ex. http://localhost:8080/actuator/metrics/http.server.requests

{
  "name": "http.server.requests",
  "description": null,
  "baseUnit": "seconds",
  "measurements": [{
    "statistic": "COUNT",
    "value": 2.0
  }, {
    "statistic": "TOTAL_TIME",
    "value": 0.0518762
  }, {
    "statistic": "MAX",
    "value": 0.0
  }],
  "availableTags": [{
    "tag": "exception",
    "values": ["None"]
  }, {
    "tag": "method",
    "values": ["GET"]
  }, {
    "tag": "uri",
    "values": ["/actuator", "/actuator/metrics"]
  }, {
    "tag": "outcome",
    "values": ["SUCCESS"]
  }, {
    "tag": "status",
    "values": ["200"]
  }]
}

/health Endpoint

 The /health endpoint is to check the health or state of the running application. By default this endpoint only display a simple status of the application like up or DOWN. To display details you need to add the following property in the application.properties file.

management.endpoint.health.show-details=always

This will show the so called Health Indicators. On properties file you can enable or disable each individually. Some contain information like your disk space, if you  database is running and if you can ping your site. 

 

/loggers Endpoint

 To check the list of all configured logger for our application, we can use the http://localhost:8080/actuator/loggers endpoint.This endpoint display all the logger and configured log levels for your application and all libraries included.

It also provides the option to check the details for the individual logger.Use the URL http://localhost:8080/actuator/loggers/{name} where {name} is the name of the logger.To check the details for the {com.javdevjournal} logger, use the http://localhost:8080/actuator/loggers/com.javadevjournal.

 

Saturday, October 3, 2020

Ubuntu Multipass

 Motivation

    Recently I have started a Elasticsearch 7 Udemy course. Really interesting and highly recommended. 

    Unfortunately my notebook has a Windows 10 and Elasticsearch only runs on Unix type systems. So to run and test it I had 3 possible options:

  1. Oracle VM virtual box
  2. Docker
  3. Ubuntu Multipass

I have chosen Multipass because it is a chance to learn a new technology and i have a previous experience working with Linux systems.


Definition

 As it says on their site Multipass is a mini-cloud Ubuntu VM from Windows or Mac. It is a very simple way to start, configure and run multiple instances of Ubuntu VM. 

 First impression: Super easy to use. Small learning curve. Although to run Elasticsearch properly, I had to learn how to "hack"some configurations to improve memory and HD space.


Basic instructions

After you download and install the basic commands are:

1. Launch an instance (by default you get the current Ubuntu LTS)

multipass launch --name ubuntu-lts

 

2. Run commands in that instance, try running bash (logout or ctrl-d to quit)

multipass exec ubuntu-lts -- lsb_release -a

 

3. Pass a cloud-init metadata file to an instance on launch see using cloud-init with multipass for more details

multipass launch -n ubuntu-lts-custom --cloud-init cloud-config.yaml

 

4. See your instances

multipass list

 

5. Stop and start instances

multipass stop ubuntu-lts ubuntu-lts-custom multipass start ubuntu-lts

 

6. Clean up what you don’t need

multipass delete ubuntu-lts-custom multipass purge

 

7. Find alternate images to launch with multipass

multipass find

 

8. Get help

multipass help multipass help <command>


Future post: How to change instances memory and HD space.