SpringBootSpring Boot Admin 微服务应用监控
Posted H__D
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBootSpring Boot Admin 微服务应用监控相关的知识,希望对你有一定的参考价值。
一、什么是Spring Boot Admin?
Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册(通过HTTP)或使用SpringCloud注册中心(例如Eureka,Consul)发现。 UI是的Vue.js应用程序,展示Spring Boot Admin Client的Actuator端点上的一些监控。服务端采用Spring WebFlux + Netty的方式。
- 显示健康状况
- 显示详细信息,例如
- JVM和内存指标
- micrometer.io指标
- 数据源指标
- 缓存指标
- 显示构建信息编号
- 关注并下载日志文件
- 查看jvm system-和environment-properties
- 查看Spring Boot配置属性
- 支持Spring Cloud的postable / env-和/ refresh-endpoint
- 轻松的日志级管理
- 与JMX-beans交互
- 查看线程转储
- 查看http-traces
- 查看auditevents
- 查看http-endpoints
- 查看计划任务
- 查看和删除活动会话(使用spring-session)
- 查看Flyway / Liquibase数据库迁移
- 下载heapdump
- 状态变更通知(通过电子邮件,Slack,Hipchat,......)
- 状态更改的事件日志(非持久性)
二、入门
1. 创建 Spring Boot Admin Server
这里我们创建一个admin-server模块来作为监控中心演示其功能。
1.1、新建一个SpringBoot项目,
1.2、引入spring-boot-admin-server依赖
1 <!-- spring-boot-admin --> 2 <dependency> 3 <groupId>de.codecentric</groupId> 4 <artifactId>spring-boot-admin-starter-server</artifactId> 5 <version>2.2.3</version> 6 </dependency> 7 8 <dependency> 9 <groupId>org.springframework.boot</groupId> 10 <artifactId>spring-boot-starter-web</artifactId> 11 </dependency>
完整pom如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.example</groupId> 8 <artifactId>test-springboot-admin-server</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.2.5.RELEASE</version> 15 </parent> 16 17 <properties> 18 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 19 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 20 <java.version>1.8</java.version> 21 </properties> 22 23 <dependencies> 24 25 <!-- spring-boot-admin --> 26 <dependency> 27 <groupId>de.codecentric</groupId> 28 <artifactId>spring-boot-admin-starter-server</artifactId> 29 <version>2.2.3</version> 30 </dependency> 31 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-web</artifactId> 35 </dependency> 36 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-test</artifactId> 40 <scope>test</scope> 41 </dependency> 42 43 </dependencies> 44 45 46 <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 --> 47 <build> 48 <plugins> 49 <plugin> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-maven-plugin</artifactId> 52 </plugin> 53 </plugins> 54 </build> 55 </project>
1.3、编辑配置文件application.yml
1 spring: 2 application: 3 name: admin-server 4 server: 5 port: 9301
1.4、编辑启动类
1 @EnableAdminServer 2 @SpringBootApplication 3 public class Application { 4 public static void main(String[] args) { 5 SpringApplication.run(Application.class, args); 6 } 7 }
1.5、启动项目,访问地址:http://localhost:9301/
2. 创建 Spring Boot Admin Server
2.1、新建一个SpringBoot项目,
2.2、引入spring-boot-admin-client依赖
1 <!-- spring-boot-admin --> 2 <dependency> 3 <groupId>de.codecentric</groupId> 4 <artifactId>spring-boot-admin-starter-client</artifactId> 5 <version>2.2.3</version> 6 </dependency> 7 8 <dependency> 9 <groupId>org.springframework.boot</groupId> 10 <artifactId>spring-boot-starter-web</artifactId> 11 </dependency>
完整pom如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.example</groupId> 8 <artifactId>test-springboot-admin-client</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <parent> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-parent</artifactId> 13 <version>2.2.5.RELEASE</version> 14 </parent> 15 16 <properties> 17 18 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 19 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 20 <java.version>1.8</java.version> 21 </properties> 22 23 <dependencies> 24 25 <!-- spring-boot-admin --> 26 <dependency> 27 <groupId>de.codecentric</groupId> 28 <artifactId>spring-boot-admin-starter-client</artifactId> 29 <version>2.2.3</version> 30 </dependency> 31 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-web</artifactId> 35 </dependency> 36 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-actuator</artifactId> 40 </dependency> 41 42 <dependency> 43 <groupId>org.springframework.boot</groupId> 44 <artifactId>spring-boot-starter-test</artifactId> 45 <scope>test</scope> 46 </dependency> 47 48 </dependencies> 49 50 51 <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 --> 52 <build> 53 <plugins> 54 <plugin> 55 <groupId>org.springframework.boot</groupId> 56 <artifactId>spring-boot-maven-plugin</artifactId> 57 </plugin> 58 </plugins> 59 </build> 60 61 </project>
2.3、编辑配置文件application.yml
1 spring: 2 application: 3 name: admin-client 4 boot: 5 admin: 6 client: 7 url: http://localhost:9301 #配置admin-server地址 8 server: 9 port: 9305 10 management: 11 endpoints: 12 web: 13 exposure: 14 include: \'*\' 15 endpoint: 16 health: 17 show-details: always 18 logging: 19 file: admin-client.log #添加开启admin的日志监控
2.4、编辑启动类
1 @SpringBootApplication 2 public class Application { 3 public static void main(String[] args) { 4 SpringApplication.run(Application.class, args); 5 } 6 }
2.5、启动项目,继续访问admin-server的Web地址:http://localhost:9301/
监控信息演示
- 访问如下地址打开Spring Boot Admin的主页:http://localhost:9301(同上)
- 选中admin-client客户端查看,监控信息概览
- 度量指标信息,比如JVM、Tomcat及进程信息;
- 环境变量信息,比如系统属性、系统环境变量以及应用配置信息;
- 查看所有创建的Bean信息;
- 查看应用中的所有配置信息;
- 查看日志信息,需要添加以下配置才能开启;
1 logging: 2 file: admin-client.log #添加开启admin的日志监控
- 查看JVM信息;
- 查看可以访问的Web端点;
一、集成 Eureka
1、创建Eureka服务,参考:【SpringCloud】快速入门(一)
2、修改admin-server
在pom.xml中添加相关依赖:
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 4 </dependency>
在application-eureka.yml中进行配置,只需添加注册中心配置即可:
1 spring: 2 application: 3 name: admin-server 4 server: 5 port: 9301 6 eureka: 7 client: 8 register-with-eureka: true 9 fetch-registry: true 10 service-url: 11 defaultZone: http://localhost:8761/eureka/
在启动类上添加@EnableDiscoveryClient来启用服务注册功能:
1 @EnableDiscoveryClient 2 @EnableAdminServer 3 @SpringBootApplication 4 public class Application { 5 public static void main(String[] args) { 6 SpringApplication.run(Application.class, args); 7 } 8 }
3、修改admin-client,
在pom.xml中添加相关依赖:同上
在application-eureka.yml中进行配置,只需添加注册中心配置即可,且去掉配置spring.boot.admin.client.url属性
1 spring: 2 application: 3 name: admin-client 4 # boot: 5 # admin: 6 # client: 7 # url: http://localhost:9301 #配置admin-server地址 8 server: 9 port: 9305 10 management: 11 endpoints: 12 web: 13 exposure: 14 include: \'*\' 15 endpoint: 16 health: 17 show-details: always 18 19 eureka: 20 client: 21 register-with-eureka: true 22 fetch-registry: true 23 service-url: 24 defaultZone: http://localhost:8761/eureka/ 25 26 logging: 27 file: admin-client.log #添加开启admin的日志监控
在启动类上添加@EnableDiscoveryClient来启用服务注册功能:
1 @EnableDiscoveryClient 2 @SpringBootApplication 3 public class Application { 4 public static void main(String[] args) { 5 SpringApplication.run(Application.class, args); 6 } 7 }
4、功能演示
启动Eureka注册中心,启动admin-server、admin-client
1)查看注册中心发现服务均已注册:http://localhost:8761
2)查看Spring Boot Admin 主页发现可以看到服务信息:http://localhost:9301
四、集成 Spring Security
这里我们创建一个admin-server模块来与Spring Security集成
1、新建一个SpringBoot项目
2、pom引入Spring Security依赖
完整pom如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.example</groupId> 8 <artifactId>test-springboot-admin-sc-server</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.2.5.RELEASE</version> 15 </parent> 16 17 <properties> 18 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 19 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 20 <java.version>1.8</java.version> 21 </properties> 22 23 <dependencies> 24 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-security</artifactId> 28 </dependency> 29 30 <dependency> 31 <groupId>org.springframework.cloud以上是关于SpringBootSpring Boot Admin 微服务应用监控的主要内容,如果未能解决你的问题,请参考以下文章Springbootspring-boot-starter-redis包报错 :unknown
SpringBootSpring Boot Configuration Annotation Processor not found in classpath
Spring boot spring.batch.job.enabled=false 无法识别