欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Boot Admin

程序员文章站 2022-05-22 08:23:42
...

Spring Boot Admin 是一个针对spring-boot的actuator接口进行UI美化封装的监控工具。

<?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.example</groupId>
    <artifactId>admin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>admin-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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-admin.version>1.5.7</spring-boot-admin.version>
        <spring-cloud.version>Edgware.SR3</spring-cloud.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-server-ui-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui-turbine</artifactId>
        </dependency>

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui-activiti</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
            <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>

添加 @EnableAdminServer

@EnableAdminServer
@EnableDiscoveryClient
@SpringBootApplication
public class AdminServerApplication {

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

application.yml

server:
  port: 9001
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka-server/eureka/
spring:
  application:
    name: admin-server
  boot:
    admin:
      context-path: /monitor
#      turbine:
#        clusters: default
#        location: turbine
      routes:
        endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream
      monitor:
#      更新应用信息的频率,单位毫秒
        period:  1000
#      被监控的应用信息的过期时间,单位毫秒
        status-lifetime: 1000
management:
  security:
    enabled: false
endpoints:
  logfile:
#   配置了 external-file 才能在监控页面上看到 Log 这一列
    external-file: C:\Users\市丸 银\AppData\Local\Temp\spring.log

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration >

    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <!-- 开启JMX监控 -->
    <jmxConfigurator/>

</configuration> 

只要有服务注册到 Eureka 上,Spring Boot Admin 就能自动获取它的监控信息,无需多余的配置

注意:如果服务配置了server.context-path,一定还要配置下面2个,否则Spring Boot Admin无法获取该服务完整的监控数据(eureka server 会维护一份只读的服务清单返回给客户端,同时该缓存清单默认会每隔30s更新一次,metadataMap 中的信息会包含在清单里面)

    /**
     * Gets the metadata name/value pairs associated with this instance. This information
     * is sent to eureka server and can be used by other instances.
     */
     private Map<String, String> metadataMap = new HashMap<>();
eureka.instance.metadata-map.management.context-path=${server.context-path}
eureka.instance.metadata-map.management.context-port=${server.port}

Spring Boot Admin 的监控页面
Spring Boot Admin

Spring Boot Admin 有很多定时任务,当有新的服务注册到 Eureka了,会自动通知你,监控页面的信息也会自动更新
Spring Boot Admin

如果监控的服务使用了Hystrix,Spring Boot Admin只要添加依赖spring-boot-admin-server-ui-hystrix ,就可以监控到

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui-hystrix</artifactId>
        </dependency>

Spring Boot Admin

详细的监控数据
Spring Boot Admin
Spring Boot Admin

这里可以查看和更改环境变量
Spring Boot Admin

这里可以修改日志级别,会立即生效
Spring Boot Admin

相关标签: spring cloud