随着微服务架构的普及,SpringBoot成为了越来越多开发者的首选。然而,随着应用规模的扩大,如何有效地监控和管理这些服务成为了一个挑战。为了解决这个问题,本文将介绍如何使用SpringBoot可视化监控平台来提升应用性能。

什么是SpringBoot可视化监控平台?

SpringBoot可视化监控平台是一个基于SpringBoot的Actuator模块和Prometheus监控系统的开源项目。它可以帮助开发者实时监控SpringBoot应用的运行状态,包括CPU、内存、磁盘、网络等资源使用情况,以及服务的响应时间、错误率等关键指标。通过这些信息,开发者可以快速定位和解决性能问题,提高应用的稳定性和可用性。

如何使用SpringBoot可视化监控平台?

服务端使用

Admin服务端坐标

            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-server</artifactId>
                <version>2.7.4</version>
            </dependency>

Admin服务端配置

 

   server:
      port: 8080

设置启用Spring-Admin(@EnableAdminServer)

@SpringBootApplication
@EnableAdminServer
public class SpringbootAdminServerApplication {

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

}

客户端使用

Admin客户端坐标

           

             <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
                <version>2.7.4</version>
            </dependency>

Admin客户端配置

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

端点功能的使用

 actuator

启用指定端点

   

 management:
        endpoint:
            health:  # 端点名称
                enabled: true
                show_details: always
            beans:
                enabled:true

启用所有端点

 

   management:
        endpoints:
            enabled-by-default: true

info端口指标控制

为info端点添加自定义指标(配置版)---需要重新配置一下info端点,要不然不会显示
spring:
  boot:
    admin:
      client:
        url: http://localhost:8080
server:
  port: 80

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"
  info: #配置info信息
    env:
      enabled: true
info:
  appName: @project.artifactId@
  version: @project.version@
  author: ww
为info端点添加自定义指标(编程版)
@Component
public class infoConfig implements InfoContributor {
    @Override
    public void contribute(Info.Builder builder) {
        Map<String, Object> infoMap = new HashMap<>();
        infoMap.put("buildTime","2023");
        builder.withDetail("runTime",System.currentTimeMillis()).withDetail("company","公司");
        builder.withDetails(infoMap);
    }
}

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()).withDetail("company","公司");
            Map<String, Object> infoMap = new HashMap<>();
            infoMap.put("buildTime","2023");
            builder.withDetails(infoMap);
        }else {
            builder.status(Status.OUT_OF_SERVICE);
            builder.withDetail("上线了吗?","你做梦");
        }
    }
}

metrics端点指标控制

@Service
public class BookServiceImpl 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;
    }
}


 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐