1、spring boot admin简介
Spring Boot Actuator 提供了对单个 Spring Boot 应用的监控,信息包含应用状态、内存、线程、堆栈等,比较全面的监控了 Spring Boot 应用的整个生命周期。
但是这样监控也有一些问题:
第1:所有的监控都需要调用固定的接口来查看,如果全面查看应用状态需要调用很多接口,并且接口返回的 JSON 信息不方便运营人员理解;
第2:如果 Spring Boot 应用集群非常大,每个应用都需要调用不同的接口来查看监控信息,操作非常繁琐低效。在这样的背景下,就诞生了另外一个开源软件:Spring Boot Admin。
Spring Boot Admin 是一个管理和监控 Spring Boot 应用程序的开源软件,每个应用都认为是一个客户端,通过 HTTP 或者使用 Eureka 注册到 admin server 中进行展示,Spring Boot Admin 分为服务端和客户端,服务端其实就是一个监控后台用来汇总展示所有的监控信息,客户端就是我们的应用,使用时需要先启动服务端,在启动客户端的时候打开 Actuator 的接口,并指向服务端的地址,这样服务端会定时读取相关信息以达到监控的目的。
2、使用spring boot admin
2.1、构建spring boot admin服务端
step1:创建一个spring boot项目,然后在pom.xml文件内容如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wzy</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-admin-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
spring boot admin server依赖包
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
step2:在启动类上添加注解@EnableAdminServer:
@SpringBootApplication
@EnableAdminServer
public class SpringBootAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminServerApplication.class, args);
}
}
step3:application.properties配置spring boot admin服务端的端口
server.port=9090
spring.application.name=spring-boot-admin -server
step4:启动spring boot admin server
step5:访问spring boot admin server
浏览器输入http://localhost:9090 输出如下:
2.2、创建一个spring boot admin client
在开发的过程种,我们写的每一个spring boot应用都可以理解为一个spring boot admin client
step1:创建一个spring boot项目,pom.xml文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wzy</groupId>
<artifactId>spring-boot-baseused</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-baseused</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
actuator 包是必须要的,spring boot admin server就是利用actuator进行监控数据获取的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
springboot-admin client依赖包
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
step2:配置spring boot admin client , 在application.properties中如下配置
server.port=8080
#配置spring boot应用的名称
spring.application.name=boot-baseused
#配置当前spring boot admin client 需要注册到那个spring boot admin server上
spring.boot.admin.client.url=http://localhost:9090
#暴露所有被激活的端点到web
management.endpoints.web.exposure.include=*
#配置health端点始终显示详细
management.endpoint.health.show-details=always
step3:启动类详细
@SpringBootApplication
public class SpringBootBaseusedApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootBaseusedApplication.class, args);
}
}
step4:启动项目
step5:去spring boot admin server端提供的管理页面查看client信息
boot-baseused应用的详细信息:
后台管理里面功能还不很多的,可自行研究。
3、在后期学习spring-security的时候会将spring boot admin进行整合以实现安全控制。