SpringBoot(七)Admin监控和管理

简介

在之前的文章中介绍了SpringBoot项目中使用Spring Boot Actuator可以监控项目运行的状态(SpringBoot(六)Actuator),Spring Boot Admin则是在Actuator的基础上,提供了简洁的可视化WEB UI,更方便地让用户监控和管理我们的SpringBoot项目。

使用Spring Boot Admin时,需要启动一个Admin的服务端(Server),需要监控的应用程序则作为Spring Boot Admin Client向为Spring Boot Admin Server注册(通过HTTP)或使用SpringCloud注册中心(例如Eureka,Consul)发现。注册成功之后就可以在WEB界面来展示Client服务的Actuator端点(endpoint)监控信息了。

使用示例一:使用http注册方式

本节示例采用多module形式的Maven项目,项目pom.xml文件引入以下的依赖,其中Spring Boot版本为2.1.5.RELEASE,Spring Boot Admin的版本是2.1.4,完整代码可参考我的GitHub-Spring Boot

父工程的pom.xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-boot-starter.verion>2.1.5.RELEASE</spring-boot-starter.verion>
<spring-boot-admin.version>2.1.4</spring-boot-admin.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot-starter.verion}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-starter.verion}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Admin-Server

在Maven项目下创建admin-server子模块,并引入spring-boot-admin-starter-server依赖。

1
2
3
4
5
6
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
</dependencies>

在配置文件中配置端口和应用名:

1
2
3
4
5
spring:
application:
name: admin-server
server:
port: 8090

然后在工程的启动类AdminServerApplication加上@EnableAdminServer注解,开启AdminServer的功能,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.lzumetal.springboot.admin.server;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
@EnableAdminServer
public class AminServerApplication {

public static void main(String[] args) {
new SpringApplicationBuilder(AminServerApplication.class)
.run(args);
}

}

启动admin-server应用,并访问 http://localhost:8090 就能看到Admin的WEB界面了,默认会打开Applications的tab栏,只是此时还没有client应用注册到该服务。

Admin-Client

在Maven项目下创建admin-client子模块,并引入spring-boot-admin-starter-client的和spring-boot-starter-web依赖。

1
2
3
4
5
6
7
8
9
10
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
</dependencies>

在admin-client应用的配置文件中,配置应用名、tomcat端口、Admin-Server的http注册地址(http://localhost:8090)和需要监控的Actuator端点信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:8090 #指定Admin-Server服务的http地址
server:
port: 8070

management:
endpoints:
web:
exposure:
include: '*' #监控所有项目运行指标
endpoint:
health:
show-details: ALWAYS #显示详细信息,而不是仅显示概要

admin-client应用的启动类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.lzumetal.springboot.admin.client;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;


@SpringBootApplication
public class AdminClientApplication {

public static void main(String[] args) {
new SpringApplicationBuilder(AdminClientApplication.class)
.run(args);
}
}

启动admin-client服务,再打Admin的WEB页面就能看到注册的应用了。

点击应用的id后可以查看应用的各项运行指标:

使用示例二:使用Eureka注册中心方式

下面介绍Spring Admin集成Spring Cloud中的Eureka注册中心使用,这种方式需要启动三个应用服务:

应用服务名称 功能说明
eureka-server eureka注册中心服务端。
admin-cloud-server spring-boot-admin的服务端程序,类似与前面的admin-server。
admin-cloud-client spring-boot-admin的客户端程序,类似与前面的admin-client

本节示例采用多module形式的Maven项目,项目pom.xml文件引入以下的依赖,其中Spring Boot版本为2.1.5.RELEASE,Spring Boot Admin的版本是2.1.4,Spring Cloud版本为Greenwich.SR3。具体代码参考我的GitHub-Spring Cloud

父工程的pom.xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-boot-starter.verion>2.1.5.RELEASE</spring-boot-starter.verion>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
<spring-boot-admin.version>2.1.4</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot-starter.verion}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-starter.verion}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

eureka-server

先使用spring-cloud提供的eureka组件搭建一个简单的注册中心,搭建过程非常简单。在项目中创建eureka-server子模块,并引入依赖:

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

在配置文件中配置:

1
2
3
4
5
6
7
8
9
10
11
server:
port: 9110
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://localhost:9110/eureka #注册中心地址,其他程序通过此URL进行注册
register-with-eureka: false #当前应用就是Eureka Server,不需要注册到eureka
fetch-registry: false #因为euraka-server是单个节点,不需要同步其他节点的数据

启动类加上@EnableEurekaServer注解,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.lzumetal.springcloud.eureka.server;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

public static void main(String[] args) {
new SpringApplicationBuilder(EurekaServerApplication.class)
.run(args);
}

}

eureka-server搭建完毕。

admin-cloud-server

在项目中创建admin-cloud-server子模块,在pom.xml文件中引入依赖:

1
2
3
4
5
6
7
8
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

配置文件中配置如下:

1
2
3
4
5
6
7
8
9
server:
port: 9111
spring:
application:
name: admin-cloud-server
eureka:
client:
service-url:
defaultZone: http://localhost:9110/eureka #eureka注册中心地址

启动类加上@EnableAdminServer@EnableEurekaClient两个注解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.lzumetal.springcloud.admin.server;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class AdminCloudServerApplication {

public static void main(String[] args) {
new SpringApplicationBuilder(AdminCloudServerApplication.class)
.run(args);
}
}

至此,Spring Boot Admin的服务端搭建完成。

admin-cloud-client

在项目中创建admin-cloud-client子模块,并在pom.xml文件中引入依赖:

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</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-web</artifactId>
</dependency>

在配置文件中配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server:
port: 9112
spring:
application:
name: admin-cloud-client
eureka:
client:
service-url:
defaultZone: http://localhost:9110/eureka

management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always #显示详细信息,而不是仅显示概要

启动类上也要加上@EnableEurekaClient注解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.lzumetal.springcloud.admin.client;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class AdminCloudClientApplication {

public static void main(String[] args) {
new SpringApplicationBuilder(AdminCloudClientApplication.class)
.run(args);
}
}

Spring Boot Admin的客户端(通常就是我们实际的业务应用服务)搭建完毕,并且客户端使用了Euraka注册中心。

运行结果

运行eureka-sever、admin-cloud-server和admin-cloud-client三个项目,再访问http://localhost:9111,可以看到和上节类似的WEB界面。

------ 本文完 ------