一.Springboot监控中心概述
1.什么是Springboot监控中心
- 针对于微服务的服务状态、Http请求资源监控
- 简述:针对微服务服务器监控,服务器内存变化(堆内存变化、线程变化、日志管理等),检测服务配置连接地址是否可用(比如MySQL等的连接可能是懒加载形式的,加载的时候没有报错,当访问的时候才会报错,实现的原理采用模拟访问)、统计现在有多少bean(是Spring容器中的bean)、统计SpringMVCRequestMapping(即统计http接口)
- Actuator监控应用(没有界面,返回json格式)
- AdminUI底层使用Actuator监控应用实现可视化界面
- 应用场景:生产环境
- 默认情况下:监控中心提供三个接口的权限【
,/actuator
,/actuator/info
】,需要添加properties中的启动端点配置/actuator/health
- Springboot2.0之后,监控中心接口地址发生了变化
- 在2.0之前接口,没有
作为前缀,比如/actuator/
在2.0之前就是/actuator/beans
/beans
- 在2.0之前接口,没有
2.为什么要用监控中心
- Actuator是Springboot的一个附加功能,可帮助你在应用程序生产环境时监控和管理应用程序。可以使用HTTP的各种请求来监管,审计,收集应用的运行情况,特别对于微服务管理十分有意义,缺点:没有可视化界面
二.监控中心之搭建Actuator监控中心
1.项目搭建
- 引入依赖[build.gradle]
//web组件依赖 implementation 'org.springframework.boot:spring-boot-starter-web' //Actuator依赖 implementation 'org.springframework.boot:spring-boot-starter-actuator'
- 配置文件[application.yml]
# 通过下面的配置启动所有的监控端点,默认情况下这些断点是禁用的[启动项目的时候监控的端点] management: endpoints: web: exposure: include: * #在properties中是management.endpoints.web.exposure.include=* spring: profiles: active:prod datasource: driver-class-name:com.mysql.jdbc.Driver url:jdbc:mysql://127.0.0.1:3306/test username:root password:root
- 运行效果如下,只有REST方式访问形式
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLzEFROVTSE9EMNpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL1MTO3EDO0IjMyAzMwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
三.SpringbootActuator监控接口
1.注意事项
- 如果mysql账号或密码错误,在项目启动的时候是不会报错的(底层采用了懒加载的机制),可以使用/actuator/health进行检测
2.Actuator常用访问路径
通过actuator/+端点名获取相应的信息
路径 | 作用 |
---|---|
/actuator/beans | 显示应用程序中所有Springbean的完成列表 |
/actuator/configprops | 显示所有配置信息 |
/actuator/env | 陈列所有的环境变量 |
/actuator/mappings | 显示所有@RequestMapping url整理列表 |
/actuator/health | 显示应用程序运行状况信息,up表示成功,down失败 |
/actuator/info | 查看自定义应用信息【相当于在配置文件中配置info开头的配置信息】 |
- 当访问/actuator/health检测服务器配置返回为down时,表示相关配置信息有错误,为up时表示配置信息都可以跑通【原理:实际就是读取配置文件,模拟发送信息访问】
- 当访问/actuator/configprops显示系统的所有配置信息
- 当访问/actuator/info是查看自定义应用信息,在配置文件中配置的info开头的配置信息,如:
- 添加properties信息
info.name=jack
- 访问/actuator/info获得的返回信息
- 添加properties信息
四.AdminUI平台原理&AdminUIServer端搭建
1.AdminUI简介
- AdminUI底层使用Actuator监控应用实现可视化界面
2.AdminUI原理
- 将所有服务(都继承Actuator监控)的监控中心管理存放在adminUI上
- 服务分布图
3.实例代码
- 应用中添加依赖
implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'de.codecentric:spring-boot-admin-starter-server' implementation 'org.jolokia:jolokia-core' implementation 'com.googlecode.json-simple:json-simple:1.1'
- 启动文件添加注解
package com.example.AdminUIServer; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @EnableAdminServer public class AdminUiServerApplication { public static void main(String[] args) { SpringApplication.run(AdminUiServerApplication.class, args); } }
- 运行项目截图
五.SpringBootAdminUIClient使用
1.Client实例代码
- application.properties[将服务启动在8080端口上,并通过
的设置定义所有端点内容都被暴露,通过management.endpoints.web.exposure.include=*
的设置定义server的路径]spring.boot.admin.client.url=http://localhost:8081
spring.boot.admin.client.url=http://localhost:8081 management.endpoints.web.exposure.include=* server.port=8080
- build.gradle引入admin-starter-client和starter-actuator
plugins { id 'org.springframework.boot' version '2.1.3.RELEASE' id 'java' } apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'de.codecentric:spring-boot-admin-starter-client:2.1.3' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
- application.java启动项目
package com.example.actuatorDemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ActuatorDemoApplication { public static void main(String[] args) { SpringApplication.run(ActuatorDemoApplication.class, args); } }
2.Server实例代码
- application.properties
server.port=8081
- build.gradle引入spring-boot-admin-starter-server
plugins { id 'org.springframework.boot' version '2.1.3.RELEASE' id 'java' } apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() } ext { set('springBootAdminVersion', '2.1.3') } dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'de.codecentric:spring-boot-admin-starter-server' implementation 'de.codecentric:spring-boot-admin-server-ui' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.projectreactor:reactor-test' } dependencyManagement { imports { mavenBom "de.codecentric:spring-boot-admin-dependencies:${springBootAdminVersion}" } }
- 启动类上添加@EnableAdminServer注解启动Server
package com.example.AdminUIServer; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableAdminServer public class AdminUiServerApplication { public static void main(String[] args) { SpringApplication.run(AdminUiServerApplication.class, args); } }