Use Spring Boot Admin project monitoring and management
First, what is Spring Boot Admin
Spring Boot Admin (SBA) is a community open source project, Spring Boot for managing and monitoring applications. Application by http
way of registering to Spring Boot management client, or the discovery mechanism by Spring Cloud service, and then for the actuator
interface data Vue.js
visualization management.
For us, we can see all monitored Spring Boot project by Spring Boot Admin, detailed Health, Memory, JVM system properties and environment, garbage collection information.
Two, Spring Boot Admin start
Spring Boot Admin composed of two characters: one is Server-side; one is Client-side, that application to be monitored. The following were two roles configuration:
Spring Boot Admin Server
Introducing dependent [the pom.xml]
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.0</version>
</dependency>
@EnableAdminServer comment
@SpringBootApplication
@EnableAdminServer
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
application.yml Configuration
spring:
application:
name: spring-boot-admin-server
server:
port: 8080
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
After starting the visit http://localhost:8080
you can see a UI interface
At this time applications and examples are, because we do not have registered clients, then we have to achieve the client.
Spring Boot Admin Client
Introducing clients rely [the pom.xml]
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.0</version>
</dependency>
Program entry where there is no particular need to be amended
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
application.yml Configuration
spring:
application:
name: spring-boot-admin-client
boot:
admin:
client:
url: http://localhost:8080
server:
port: 8081
At this time, then access localhost:8080
will be found to add an application example, as shown below:
So here we have a basic Spring Boot Admin application to configure a success.
Click on the menu bar wallboard
, then click on the application you want to view, you can access the application information, such as memory status and other information:
Three, SBA combined Spring Cloud registry
除了上面案例中,直接在客户端中配置相应的 SBA 配置外,还可以配合 Spring Cloud 的服务注册与发现应用(例如:Eureka, Consul) ,接下来演示如何配置:
新建统一的依赖管理
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jojo</groupId>
<artifactId>admin-demo-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>admin-demo-dependencies</name>
<description>Demo project for Spring Boot Admin</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<!-- Environment Settings -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- Spring Settings -->
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
搭载服务注册中心
引入依赖
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>
应用程序入口
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
application.yml
spring:
application:
name: admin-demo-eureka
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
service-url:
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka
register-with-eureka: false
fetch-registry: false
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
配置成功后启动应用程序,访问 localhost:8761
,可以看到类似下图的 UI 界面:
创建 SBA 服务端
引入依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
应用程序入口
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
application.yml 配置
spring:
application:
name: admin-demo-admin-server
server:
port: 8769
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
创建 SBA 客户端
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
applicaion.yml 配置文件
spring:
application:
name: admin-demo-admin-client
server:
port: 8762
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
应用程序入口
@SpringBootApplication
@EnableDiscoveryClient
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
将三个应用启动后,访问 localhost:8769
就可以监控各个应用的运行情况了:
四、集成邮件通知
SBA 中也可以集成邮件通知,当注册的服务下线、宕机时,向指定的邮箱发送信息邮件。其配置也十分容易,只需要在 yaml
配置文件中配置邮箱的发送方和邮件的收件方,以及在 pom
文件中引入 `` 的依赖即可。下面演示这段配置:
pom.xml配置:加入 mail 的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.yml:在配置文件中配置收件人以及发件方信息,并且开启邮箱提醒
spring:
boot:
admin:
notify:
mail:
enabled: true
from: [email protected]
...
spring.mail.host: smtp.example.com
spring.mail.username: [email protected]
spring.mail.password: # 授权码
spring.boot.admin.notify.mail.to: [email protected]
配置并启动完成后,可以尝试将 Client 项目中止:
然后打开收件方的邮箱,就可以看见 SBA 发送的邮件了
五、参考
SBA 官方文档:https://codecentric.github.io/spring-boot-admin/current/