Spring Boot Admin:微服务应用监控
摘要
Spring Boot Admin 可以对SpringBoot应用的各项指标进行监控,可以作为微服务架构中的监控中心来使用 。
Spring Boot Admin 简介
SpringBoot应用可以通过Actuator来暴露应用运行过程中的各项指标,Spring Boot Admin通过这些指标来监控SpringBoot应用,然后通过图形化界面呈现出来。Spring Boot Admin不仅可以监控单体应用,还可以和Spring Cloud的注册中心相结合来监控微服务应用。
Spring Boot Admin 可以提供应用的以下监控信息:
-
监控应用运行过程中的概览信息;
-
度量指标信息,比如JVM、Tomcat及进程信息;
-
环境变量信息,比如系统属性、系统环境变量以及应用配置信息;
-
查看所有创建的Bean信息;
-
查看应用中的所有配置信息;
-
查看应用运行日志信息;
-
查看JVM信息;
-
查看可以访问的Web端点;
-
查看HTTP跟踪信息。
创建admin-server模块
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.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.reno</groupId> <artifactId>springcloud.admin.server</artifactId> <version>1.0</version> <name>springcloud.admin.server</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR2</spring-cloud.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-server</artifactId> <version>2.1.0</version> </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-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <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> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
yml文件
#spring: # application: # name: admin-server #server: # port: 39301 spring: application: name: admin-server security: # 配置登录用户名和密码 user: name: test password: 12345 boot: # 不显示admin-server的监控信息 admin: discovery: ignored-services: ${spring.application.name} server: port: 39301 eureka: instance: hostname: 192.16.10.208 #hostname: localhost #设置是否将自己作为客户端注册到注册中心(缺省true) #这里为不需要,查看@EnableEurekaServer注解的源码,会发现它间接用到了@EnableDiscoveryClient instance-id: adminService-${spring.cloud.client.ipaddress}-${server.port} health-check-url-path: /actuator/health lease-renewal-interval-in-seconds: 10 metadata-map: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password} client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://192.16.10.208:38761/eureka/ registry-fetch-interval-seconds: 5 instance-info-replication-interval-seconds: 10 management: endpoint: health: show-details: always endpoints: web: exposure: include: '*'
创建admin-client模块
这里我们创建一个admin-client模块作为客户端注册到admin-server。
pom文件
<?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.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.reno</groupId> <artifactId>springcloud.admin.client</artifactId> <version>1.0</version> <name>springcloud.admin.client</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR2</spring-cloud.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> <version>2.1.0</version> </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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <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> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
yml文件
#spring: # application: # name: admin-client # boot: # admin: # client: # url: http://localhost:39301 #server: # port: 39305 #management: # endpoints: # web: # exposure: # include: '*' # endpoint: # health: # show-details: always #logging: # file: admin-client.log #添加开启admin的日志监控 spring: application: name: admin-client server: port: 39305 management: endpoints: web: exposure: include: '*' endpoint: health: show-details: always logging: file: admin-client.log #添加开启admin的日志监控 eureka: instance: hostname: 192.16.10.208 #hostname: localhost #设置是否将自己作为客户端注册到注册中心(缺省true) #这里为不需要,查看@EnableEurekaServer注解的源码,会发现它间接用到了@EnableDiscoveryClient instance-id: adminClient-${spring.cloud.client.ipaddress}-${server.port} health-check-url-path: /actuator/health client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://192.16.10.208:38761/eureka/ registry-fetch-interval-seconds: 5 instance-info-replication-interval-seconds: 10
监控信息演示
-
访问如下地址打开Spring Boot Admin的主页:http://localhost:9301
本文参考自MacroZheng链接 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。