SpringBoot健康监控——SpringBoot Admin


项目源码地址https://github.com/laolunsi/spring-boot-examples/tree/master/05-spring-boot-admin-demo

一、SpringBoot Admin概要

SpringBoot Admin用于监控SpringBoot程序,一个SpringBoot程序通过向SpringBoot Admin Server注册或使用@DiscoveryClient等微服务方式,可以将自身注册到SpringBoot Admin Server。

SpringBoot Admin UI是一个基于SpringBoot Actuator endpoints上的Vue.js应用。

我们可以先预览一下效果图:

下面我们来基于SpringBoot创建监控程序和示例:


二、第一个Spring Boot Admin Server与Client

2.1 创建Admin Server

采用SpringBoot 2.1.8.RELEAE和SpringBoot Admin 2.1.5

 	<properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.1.5</spring-boot-admin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

application类启用AdminServer:

@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {

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

}

配置:

server:
  port: 9333
spring:
  application:
    name: admin-server
  boot:
    admin:
      client:
      	enabled: true
        url: http://localhost:${server.port} # 将自身注册到admin-server

2.2 创建SpringBoot应用并注册到Admin Server

SpringBoot应用注册到Admin Server有两种方式:

  1. 引入admin-client依赖,启用admin client并配置admin-server的地址即可
  2. 微服务环境,使用@EnableDiscoveryClient或其他类似注解

这里演示第一种方法:

依赖:

SpringBoot 2.1.8.RELEASE和SpringBoot Admin 2.1.5

	<properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.1.5</spring-boot-admin.version>
    </properties>

    <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>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置:

server:
  port: 9334
spring:
  application:
    name: client-demo
  boot:
    admin:
      client:
      	enabled: true
        url: http://localhost:9333
# 这里加一个暴露所有应用信息的配置
management:
  endpoints:
    web:
      exposure:
        include: '*'

这个应用不需要修改Application类,默认的就行。

2.3 启动和测试SpringBoot Admin

启动这两个应用,打开浏览器,输入admin-server的地址:http://localhost:9333

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sLMKI3a6-1571227440950)(03-SpringBootAdmin.assets/1571201234879.png)]

默认会进入上面的application页面,我们可以看到admin-server和client-demo两个SpringBoot已经注册到admin-server的监控中了。

打开wallboard页面:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Rul1nZrY-1571227440951)(03-SpringBootAdmin.assets/1571201332785.png)]

点开journal,可以看到这些应用的记录:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZPffN4AO-1571227440951)(03-SpringBootAdmin.assets/1571201383109.png)]

点开application中client-demo应用,可以看到这个应用的面板:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VsIjRhLz-1571227440952)(03-SpringBootAdmin.assets/1571201450604.png)]

在这里我们可以看到当前服务器、当前应用的详细信息。

到此为止,我们介绍了如何利用SpringBoot Admin来管理SpringBoot应用,下一讲我们将继续了解,如何在SpringCloud的微服务体系中,利用SpringBoot Admin来监控微服务。

猜你喜欢

转载自blog.csdn.net/qq_28379809/article/details/102593592