SpringBootAdmin超详细教程以及端点指标控制
文章目录
- 1.服务端开发
- 2.客户端开发
- 配置多个客户端
- 3.端点指标控制
- actuator
- 1.info端点描述了当前应用的基本信息,可以通过两种形式快速配置info端点的信息
- 2.health端点指标控制
- 3.metrics端点指标控制
- 4.自定义端点
- 小结:
- 总结:
Spring Boot Admin,这是一个开源社区项目,用于管理和监控SpringBoot应用程序
1.服务端开发
创建一个sprinboot工程
步骤①:导入springboot admin对应的starter,版本与当前使用的springboot版本保持一致,并将其配置成web工程
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
步骤②:在引导类上添加注解@EnableAdminServer,声明当前应用启动后作为SpringBootAdmin的服务器使用
@SpringBootApplication
@EnableAdminServer //要加上这个注解
public class SpringbootServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootServerApplication.class, args);
}
}
注解一定要加上
步骤③:启动项目,在页面输入访问地址访问服务器
http://localhost:8080/applications
2.客户端开发
创建一个spingboot工程,勾选下图坐标
步骤①:导入springboot admin对应的starter,版本与当前使用的springboot版本保持一致,并将其配置成web工程
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
步骤②:设置当前客户端将信息上传到哪个服务器上,通过yml文件配置
server:
port: 80 #修改端口号,以免与服务端冲突
spring:
boot:
admin:
client:
url: http://localhost:8080
上述配置的意思就是该客户端的信息传到localhost:8080的服务器上
步骤③:客户端启动,启动后再次访问服务端程序,界面如下
当前监控了1个程序,点击进去查看详细信息。
由于当前没有设置开放哪些信息给监控服务器,所以目前看不到什么有效的信息。下面需要做两组配置。
- 开放指定信息给服务器看
- 允许服务器以HTTP请求的方式获取对应的信息
配置如下:
server:
port: 80
spring:
boot:
admin:
client:
url: http://localhost:8080
management:
endpoint:
health:
show-details: always #开放所有的健康信息明细
endpoints:
web:
exposure:
include: "*" #开启查阅的内容项,使用*表示查阅全部。记得带引号,默认带health的内容项
配置多个客户端
可以通过配置客户端的方式在其他的springboot程序中添加客户端坐标,在yml中添加配置,这样当前服务器就可以监控多个客户端程序了。每个客户端展示不同的监控信息。
添加yml配置
spring:
boot:admin:
client:
url: http://localhost:8080
management:
endpoint:health:
show-details: always #开放所有的健康信息明细
endpoints:
web:
exposure:
include: "*" #开启查阅的内容项,使用*表示查阅全部。记得带引号,默认带health的内容项
添加客户端坐标,要与当前springboot工程版本号一致
de.codecentric
spring-boot-admin-starter-client
2.7.4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
进入监控面板,如果你加载的应用具有功能,在监控面板中可以看到3组信息展示的与之前加载的空工程不一样。
- 类加载面板中可以查阅到开发者自定义的类,如下图
- 映射中可以查阅到当前应用配置的所有请求
- 性能指标中可以查阅当前应用独有的请求路径统计数据
- 端点功能开启与关闭
启动个别端点用endpoint,health默认必须开启
management:
endpoint:
health: # 端点名称
show-details: always
info: # 端点名称
enabled: true # 是否开放,对单一端点的开启与关闭
为了方便开发者快速配置端点,springboot admin设置了13个较为常用的端点作为默认开放的端点,如果需要控制默认开放的端点的开放状态,可以通过配置设置,如下:
启动所有端点用endpoints
management:
endpoints:
enabled-by-default: true # 是否开启默认端点,默认值true
- 暴露端点功能
JMX默认都可以访问,而Web默认只有两个,health和info
management:
endpoints:
web:
exposure:
include: "*"
整体上来说,对于端点的配置有两组信息,一组是endpoints开头的,对所有端点进行配置;一组是endpoint开头的,对具体端点进行配置;web需要设置暴露端点,才可以在Web页面访问到
management:
endpoint: # 具体端点的配置health:
show-details: always
info:
enabled: true
endpoints: # 全部端点的配置
web: #web可以访问端点开启
exposure:
include: "*"
enabled-by-default: true
总结:
- 开发监控服务端需要导入坐标,然后在引导类上添加注解@EnableAdminServer,并将其配置成web程序即可
- 开发被监控的客户端需要导入坐标,然后配置服务端服务器地址,并做开放指标的设定即可
- 在监控平台中可以查阅到各种各样被监控的指标,前提是客户端开放了被监控的指标
3.端点指标控制
actuator
1.Actuator提供了SpringBoot生产就绪功能,通过端点的配置与访问,获取端点信息
2.端点描述了一组监控信息,SpringBoot提供了多个内置端点,也可以根据需要自定义端点信息
3.访问当前应用所有端点信息:/actuator
4.访问端点详细信息:/actuator/ 端点名称
1.info端点描述了当前应用的基本信息,可以通过两种形式快速配置info端点的信息
配置形式
在yml文件中通过设置info节点的信息就可以快速配置端点信息,版本高的需要加一下配置在yml中
management:
endpoint:
health:
show-details: always
info:
enabled: true
endpoints:
web:
exposure:
include: "*"
enabled-by-default: true
info:
env:
enabled: true #高版本的需要自己加一下,management.info.env.enabled设置为true
info:
author: QGC
school: ccut
编程形式
@Component
public class InfoConfig implements InfoContributor {//实现这个接口
@Override
public void contribute(Info.Builder builder) {
//添加单个信息
builder.withDetail("runTime", new SimpleDateFormat("yyyy年MM月dd日 HH
ss").format(new Date()));
Map infoMap = new HashMap();
infoMap.put("buildTime", "2022:10:17");
builder.withDetails(infoMap); //添加一组信息
}
}
页面效果
2.health端点指标控制
health端点描述当前应用的运行健康指标,即应用的运行是否成功。通过编程的形式可以扩展指标信息。
@Component
public class HealthConfig extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
boolean condition = true;
if(condition) {
builder.status(Status.UP); //设置运行状态为启动状态
builder.withDetail("runTime", System.currentTimeMillis());
Map infoMap = new HashMap();
infoMap.put("buildTime", "2022");
builder.withDetails(infoMap);
}else{
builder.status(Status.OUT_OF_SERVICE); //设置运行状态为不在服务状态
builder.withDetail("上线了吗","你做梦");
}
}
}
- 条件成功时
- 条件失败时
当任意一个组件状态不为UP时,整体应用对外服务状态为非UP状态。
3.metrics端点指标控制
metrics端点描述了性能指标,除了系统自带的监控性能指标,还可以自定义性能指标。
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
@Autowired
private BookDao bookDao;
private Counter counter;
public BookServiceImpl(MeterRegistry meterRegistry){
counter = meterRegistry.counter("QGC用户付费操作次数:");
}
public Boolean insert(Book book) {
return bookDao.insert(book) > 0;
}
public Boolean modify(Book book) {
return bookDao.updateById(book) > 0;
}
public Boolean delete(Integer id) {
//每次执行删除业务等同于执行了付费业务
counter.increment();
return bookDao.deleteById(id) > 0;
}
在性能指标中就出现了自定义的性能指标监控项
4.自定义端点
可以根据业务需要自定义端点,方便业务监控
@Component
@Endpoint(id="pay",enableByDefault = true)
public class PayEndpoint {
@ReadOperation
public Object getPay(){
Map payMap = new HashMap();
payMap.put("level 1","226");
payMap.put("level 2","912");
payMap.put("level 3","0820");
return payMap;
}
}
于此端点数据spirng boot admin无法预知该如何展示,所以通过界面无法看到此数据,通过HTTP请求路径可以获取到当前端点的信息,但是需要先开启当前端点对外功能,或者设置当前端点为默认开发的端点。
这里用postman来模拟请求
小结:
1.端点的指标可以自定义,但是每种不同的指标根据其功能不同,自定义方式不同
2.info端点通过配置和编程的方式都可以添加端点指标
3.health端点通过编程的方式添加端点指标,需要注意要为对应指标添加启动状态的逻辑设定
4.metrics指标通过在业务中添加监控操作设置指标
5.可以自定义端点添加更多的指标
总结:
1.监控的意义
2.可视化监控平台————Spring Boot Admin
3.监控原理————Actuator
4.自定义监控指标
- 系统端点添加监控指标
- 自定义端点
还没有评论,来说两句吧...