Tuesday, September 5, 2017

Spring Boot: Spring Boot Admin



Objective:
The objective of this tutorial is to provide overview about the Spring Boot Admin with a simple example.
Overview:
Spring Boot Admin:
Spring Boot Admin is a simple application to manage and monitor your Spring Boot Applications. The applications register with our Spring Boot Admin Client (via http) or are discovered using Spring Cloud (e.g. Eureka). The UI is just an Angular.js application on top of the Spring Boot Actuator endpoints. In case you want to use the more advanced features (e.g. jmx-, loglevel-management), Jolokia must be included in the client application.

Background:
Spring Actuators provide various endpoints as explained in one of my article. The end points provides a lot of insights about Spring Boot application. But if you have many applications running then monitoring each application by hitting the end points and inspecting the JSON response is tedious process. To avoid this hassle Code Centric team came up with Spring Boot Admin module which will provide us Admin UI Dash board to administer Spring Boot applications. This module crunches the data from Actuator end points and provides insights about all the registered applications in single dash-board. Now we will demonstrate the Spring Boot Admin features in the following sections.

Steps:
1. Goto Spring Initializer (http://start.spring.io/), enter the Artifact with some name (eureka-service, in my case), Dependencies as “Web”, “Eureka Discovery, “Actuator” and hit the button “Generate project”. This will download the zip file spring-boot-admin.zip.

2. Now unzip this project. Import the project as Maven project to Eclipse or any other IDE of your choice. I used Spring Tool Suite (STS) as my IDE here.

3. Now open the pom.xml and add the following two dependencies and save it.

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.3</version>
</dependency>

4. Now open the SpringBootAdminApplication.java (under \src\main\java) and add the annotation @EnableAdminServer and @EnableDiscoveryClient above @SpringBootApplication.

@EnableAdminServer
@EnableDiscoveryClient
@SpringBootApplication
public class SpringBootAdminApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}

5. Add the following values to application.properties (under \src\main\resources). The points indicate that the application name is eureka-server running on port 8761. Enabling security to false will help to display values for the endpoint /metrics (which will be explained below).

spring.application.name=spring-boot-admin
server.port=8082
management.security.enabled=false

6. Now run the project as Spring Boot App (Right click on the project --> Run As --> Spring Boot App). Note: I am using Spring Tool Suite (STS) as my IDE.




7. Now open your browser and type the following URL, which will bring the Spring Boot UI as shown in the below screen shot. You can hit “Allow” button in the popup.
http://localhost:8082/

Home Page (Applications Tab):





Details Tab:


Metrics Tab:


Environment Tab:


Logging Tab:


Threads Tab:


Audit Tab:


Trace Tab:


Heapdump Tab:
It will download the Heapdump automatically.

Journal Tab:


Steps to add JMX Tab:
1. If you would like to add JMX tab to be added to Spring Boot Admin, include the following Jolokia dependency to pom.xml

<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>

</dependency>

2. Create a file named logback.xml under \src\main\resources and add the following entry. This will help

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
              <include resource="org/springframework/boot/logging/logback/base.xml"/>
              <jmxConfigurator/>

</configuration>

3. Now you will see the JMX tab as shown below:


Login Feature:
It is possible to introduce login feature to Spring Boot Admin UI. The following additional settings are required to enable this:
1. Add the following entry in pom.xml to add Spring Security and Admin server UI login feature.
<dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-security</artifactId>
       </dependency>
             
       <dependency>
              <groupId>de.codecentric</groupId>
              <artifactId>spring-boot-admin-server-ui-login</artifactId>
              <version>1.5.3</version>

       </dependency>

2. Add the following additional entries highlighted in yellow in the application.properties file (under \src\main\resources). This is the user id and password.
spring.application.name=spring-boot-admin
server.port=8082
management.security.enabled=false
security.user.name=admin
security.user.password=admin123

3. Add the Spring Security logic as shown below:

@EnableAdminServer
@EnableDiscoveryClient
@SpringBootApplication
public class SpringBootAdminApplication {

       public static void main(String[] args) {
              SpringApplication.run(SpringBootAdminApplication.class, args);
       }


@Configuration
       public static class SecurityConfig extends WebSecurityConfigurerAdapter {
              @Override
              protected void configure(HttpSecurity http) throws Exception {
// Page with login form is served as /login.html and does a POST on /login
                                                                                                              http.formLogin().loginPage("/login.html")
.loginProcessingUrl("/login").permitAll();
                     // The UI does a POST on /logout on logout
                     http.logout().logoutUrl("/logout");
                     // The ui currently doesn't support csrf
                     http.csrf().disable();

                     // Requests for the login page and the static assets are allowed
                     http.authorizeRequests()
.antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
                     .permitAll();
                     // ... and any other request needs to be authorized
                     http.authorizeRequests().antMatchers("/**").authenticated();

// Enable so that the clients can authenticate via HTTP basic for registering
                     http.httpBasic();
              }
       }

       // end::configuration-spring-security[]

4. Now if you start the server and hit the following URL, you will now see the login page as shown below.

5. Enter the user id as admin and password as admin123 so that you will land to the Spring Boot Admin UI home page. Also you will find the logout button the home page. On-click of that you will be logged out and will display login screen again.


That’s all. Hope this was useful.

No comments:

Post a Comment