Use spring-boot-admin to monitor spring-boot services (transferred from Niubi)

Respect originality: http://www.cnblogs.com/ityouknow/p/8440455.html

 

The previous article "springboot (19): Using Spring Boot Actuator to monitor applications" introduced the use of Spring Boot Actuator. Spring Boot Actuator provides monitoring of a single Spring Boot. The information includes: application status, memory, thread, stack, etc. etc., which comprehensively monitors the entire life cycle of the Spring Boot application.

However, there are some problems with such monitoring: first, all monitoring needs to call a fixed interface to view, if you need to call a lot of interfaces to fully view the application status, and the Json information returned by the interface is inconvenient for operators to understand; second, if Spring Boot The application cluster is very large, and each application needs to call different interfaces to view monitoring information, which is very cumbersome and inefficient. In this context, another open source software was born: Spring Boot Admin.

What is Spring Boot Admin?

Spring Boot Admin is an open source software for managing and monitoring Spring Boot applications. Each application is considered a client and is registered to the admin server through HTTP or Eureka for display. The Spring Boot Admin UI part uses AngularJs to display data on the front end.

Spring Boot Admin is a monitoring tool for UI beautification and encapsulation of spring-boot's actuator interface. He can: browse the basic information of all monitored spring-boot projects in the list, detailed Health information, memory information, JVM information, garbage collection information, various configuration information (such as data source, cache list and hit rate), etc., You can also directly modify the level of the logger.

This article will show you how to use Spring Boot Admin to monitor Spring Boot applications.

Monitor Monolithic Applications

This section shows you how to monitor a single Spring Boot application using Spring Boot Admin.

Admin Server端

project dependencies

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

configuration file

server.port=8000

The server set the port to: 8000.

startup class

@Configuration
@EnableAutoConfiguration
@EnableAdminServer public class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); } }

After completing the above three steps, start the server and access the browser to http://localhost:8000see the following interface:

sample code

Admin Client

project dependencies

<dependencies>
  <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>1.5.6</version> </dependency> </dependencies>

configuration file

server.port=8001

spring.boot.admin.url=http://localhost:8000  
management.security.enabled=false 

- spring.boot.admin.url Configure the address of the Admin Server
- management.security.enabled=false Turn off security verification

startup class

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

After the configuration is complete, start the client service and access the service again: http://localhost:8000you can see the relevant information of the client.

The home page will display the monitored services. Click on the details to view the specific monitoring information of a service.

As can be seen from the above figure, Spring Boot Admin graphically displays various information of the application, most of which come from the interfaces provided by Spring Boot Actuator.

Monitor Microservices

If we are using a single Spring Boot application, we need to configure the address information of the Admin Server in each monitored application; if the applications are registered in Eureka, we do not need to configure each application, Spring Boot Admin will automatically Grab application-related information from the registry.

Four example projects are used here to demonstrate:

  • spring-boot-admin-server Admin Server端
  • spring-cloud-eureka registry
  • spring-cloud-producer application 1, Admin Client
  • spring-cloud-producer-2 Application 2, Admin Client

First start the registration center spring-cloud-eureka, if you don't know Eureka, you can check this article springcloud (2): registration center Eureka

Server side

Example project: spring-boot-admin-server

project dependencies

<dependencies>
  <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui</artifactId> <version>1.5.6</version> </dependency> </dependencies>

Added support for eureka

configuration file

server:
  port: 8000
spring:
  application:
    name: admin-server
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

management.security.enabled: false

The relevant configuration of eureka is added to the configuration file

startup class

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient @EnableAdminServer public class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); } }

After the above steps are completed, start the Server side.

Client

Example projects: spring-cloud-producer and spring-cloud-producer-2

project dependencies

<dependencies>
  <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>1.5.6</version> </dependency> </dependencies>

configuration file

server:
  port: 9000
spring:
  application:
    name: producer
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
management:
  security:
    enabled: false

We found that the Admin Server related configuration was not added to the configuration file

startup class

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication { public static void main(String[] args) { SpringApplication.run(ProducerApplication.class, args); } }

web tier

@RequestMapping("/hello")
  public String index(@RequestParam String name) { logger.info("request one/two name is "+name); return "hello "+name+",this is first messge"; }

The web layer adds a request method of /hello, and one/two is used in the method to distinguish which application it is. spring-cloud-producer-2 is similar to spring-cloud-producer code, you can check the sample code for details.

After completing the above configuration, start the projects respectively: spring-cloud-producer and spring-cloud-producer-2, the browser access http://localhost:8000 can see the following interface:

As can be seen from the above figure, the Admin Server monitors four instances, including the Server itself, the registry, and two PRODUCERs. It means that Admin Server automatically grabs all instance information from the service center and monitors it. Click Detail to view the monitoring information of a specific example.

sample code

Email alert

Spring Boot Admin displays all application information in the microservice in the background, which is very convenient for us to monitor and manage the overall microservice. However, it is impossible for our operators to stare at the monitoring background 24 hours a day, so if there is an abnormality in the service, it would be great to have a corresponding email alert. In fact, Spring Boot Admin also supports it.

We retrofit the above example project spring-boot-admin-server.

add dependencies

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

Added starter package for email sending

configuration file

spring:
  mail:
    host: smtp.qq.com
    username: [email protected]
    password: xxxx
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
  boot:
    admin:
      notify:
        mail:
          from: [email protected]
          to: [email protected]
# http://codecentric.github.io/spring-boot-admin/1.5.6/#mail-notifications

Add email sending related information in the configuration file: email sender, receiver, protocol, mobile authorization code, etc. For Spring Boot mail sending, you can refer to springboot (ten): mail service

After the configuration is complete, restart the project spring-boot-admin-server, so that the Admin Server has the function of email alarm. By default, the Admin Server monitors the online and offline services in Eureka. When the service is online and offline, we You will receive the following email:

Of course, this is only the most basic email monitoring. In actual use, we need to customize the content of email alerts according to our situation, such as monitoring the usage of heap memory, and alerting when a certain percentage is reached.

sample code

refer to

Spring Boot Admin Reference Guide

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325390590&siteId=291194637