实用开发篇-Spring-Boot-Admin监控

监控的意义

  • 监控服务状态是否宕机
  • 监控服务运行指标(内存、虚拟机、线程、请求等)
  • 监控日志
  • 管理服务(服务下线)
  • 监控的实施方式
  • 显示监控信息的服务器:用于获取服务信息,并显示对应的信息
  • 运行的服务:启动时主动上报,告知监控服务器自己需要收到监控

在这里插入图片描述

可视化监控平台

  • SpringBoot Admin,开源社区项目,用于管理和监控springboot应用程序。客户端注册到服务器后,通过HTTP请求方式,服务端定期从客户端获取对应的信息,并通过UI界面展示对应信息。
  • Admin服务端(与spring-boot-starter-parent,version版本一致)
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.6.6</version>
        </dependency>
  • Admin客户端(与spring-boot-starter-parent,version版本一致)
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.6.6</version>
        </dependency>
  • Admin服务端配置
server:
  port: 8080
  • 设置启用Spring-Admin
@SpringBootApplication
@EnableAdminServer
public class SpringBoot25AdminServerApplication {

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

}
  • Admin客户端配置
server:
  port: 80

spring:
  boot:
    admin:
      client:
        url: http://localhost:8080
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: '*'


在这里插入图片描述

监控原理

Actuator

  • Actuator提供了SpringBoot生产就绪功能,通过端点的配置与访问,获取端点信
  • 端点描述了一组监控信息,SpringBoot提供了多个内置端点,也可以根据需要自定义端点信息
  • 访问当前应用所有端点信息:/actuator
  • 访问端点详细信息:/actuator/端点名称

端点功能开启与关闭

ID描述默认启动
auditevents暴露当前程序的审计事件信息
beans显示应用程序中所有Spring bean的完整列表
caches暴露可用的缓存
conditions显示在匹配和自动配置类上评估的条件以及它们匹配或不匹配的原因
configprops显示所有@ConfigurationProperties的校对清单
env暴露Spring ConfigurableEnvironment中的属性
flyway显示已应用的Flyway数据库迁移
health显示应用程序健康信息
httptrace显示HTTP追踪信息(默认情况下,最后100个HTTP请求/响应交换)
info显示应用程序信息
integrationgraph显示Spring Integration图
loggers显示和修改应用程序中日志记录器的配置
liquibase显示已应用Liquibase数据库迁移
metrics显示当前应用程序的指标量信息
mappings显示所有@RequestMapping路径的整理清单
sessions允许从Spring Session支持会话存储中检索和删除用户会话。当使用Spring Seesion的响应式Web应用程序支持时不可用
shutdown正常关闭应用程序
threaddump执行线程dump
heapdump返回一个hprof堆dump文件
jolokia通过HTTP暴露JMX bean(当Jolokia在classpath上时,不适用于WebFlux)
logfile返回日志文件的内容(如果已设置logging.file或logging.path属性)。支持使用HTTP Range头来检索部分日志文件的内容
prometheus以可以由Prometheus服务器抓取的格式暴露指标
  • 启用指定端点
management:
  endpoint:
    health: # 端点名称
      enabled: true
      show-details: always
    beans: 	#端点名称
      enabled: true

  • 启用所有端点
management:
  endpoint:
    enabled-by-default: true

端点功能暴露

  • 暴露端点功能
              ♦ 端点中包含的信息存在敏感信息,需要对外暴露端点功能时手动设定指定端点信息
属性默认
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include*
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.includeinfo,health
IDJMXWeb
auditevents
beans
caches
conditions
configprops
env
flyway
health
heapdumpN/A
httptrace
info
integrationgraph
jolokiaN/A
logfileN/A
liquibase
metrics
mappings
prometheusN/A
scheduledtasks
sessions
shutdown
threaddump

自定义监控指标

  • 为info端点添加自定义指标
info:
 appName: @project.artifactId@
 version: @project.version@
 author: itheima
@Component
public class infoConfig implements InfoContributor {
    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("runtime", System.currentTimeMillis())
        .withDetail("company", "xxxx");
        Map infoMap = new HashMap();
        infoMap.put("buildTime", "2006");
        builder.withDetails(infoMap);
    }
}
  • 为Health端点添加自定义指标
@Component
public class HealthConfig extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        boolean condition = true;
        if (true) {
            builder.withDetail("runtime", System.currentTimeMillis());
            Map infoMap = new HashMap();
            infoMap.put("buildTime", "2006");
            builder.withDetails(infoMap);
            //builder.up();
        } else {
            builder.status(Status.DOWN);
            builder.withDetail("上线了吗?", "你做梦");
            //builder.down();
            builder.status(Status.UP);
        }
    }
}
  • 为Metrics端点添加自定义指标
@Service
public class BookServerImpl extends ServiceImpl<BookDao,Book> implements IBookService{
	private Counter counter;
	public BookServiceImpl(MeterRegistry meterRegistry){
		counter = meterRegistry.counter("用户付费操作次数:");
	}
	@Override
	public boolean delete(Integer id){
		counter.increment();
		return bookDao.deleteById(id) > 0; 
	}
}
  • 自定义端点
@Component
@Endpoint(id = "pay", enableByDefault = true)
public class payEndpoint {

    @ReadOperation
    public Object getPay() {
        //调用业务操作,获取支付相关信息结果,最终return出去
        Map payMap = new HashMap();
        payMap.put("level 1", "300");
        payMap.put("level 2", "291");
        payMap.put("level 3", "666");
        return payMap;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

As_theWind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值