SpringBoot Admin监控

本文介绍了监控服务状态和运行指标的重要性,展示了如何使用SpringBootAdmin进行可视化监控,包括其部署和配置,以及利用Actuator的端点机制实现生产就绪功能,包括自定义监控指标的方法。
摘要由CSDN通过智能技术生成

监控的意义

监控服务状态是否宕机。
监控服务运行指标(内存、虚拟机、线程、请求等)。
监控日志。
管理服务(服务下线)。

监控的实施方式:
1.显示监控信息的服务器:用于获取服务信息,并显示对应的信息。
2.运行的服务:启动时主动上报,告知监控服务器自己需要受到监控。

(可以控制台输入jconsole看Java程序的状态)

可视化监控平台

Spring Boot Admin,开源社区项目,用于管理和监控SpringBoot应用程序。 客户端注册到服务端后,通过HTTP请求方式,服务端定期从客户端获取对应的信息,并通过UI界面展示对应信息。

Admin服务端

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-dependencies</artifactId>
            <version>${spring-boot-admin.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Admin客户端

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-dependencies</artifactId>
            <version>${spring-boot-admin.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Admin服务端

server:
  port: 8080

设置启动Spring-Admin

@SpringBootApplication
@EnableAdminServer
public class Springboot25AdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot25AdminApplication.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提供了SpringBoot生产就绪功能,通过端点的配置与访问,获取端点信息。
端点描述了一组监控信息,SpringBoot提供了多个内置端点,也可以根据需要自定义端点信息。
访问当前应用所有端点信息:/actuator。
访问端点详细信息:/actuator/端点名称。

启动指定端点

management:  
  endpoint:    
  health:   # 端点名称      
    enabled: true     
    show-details: always    
  beans:    # 端点名称      
    enabled: true

启动所有端点

management:  
  endpoints:    
  enabled-by-default: true
为info端点添加自定义指标
info:  
  appName: @project.artifactId@  
  version: @project.version@  
  author: itheima

为info端点添加自定义指标

@Component
public class AppInfoContributor implements InfoContributor {    
    @Override    
    public void contribute(Info.Builder builder) {        
        Map<String,Object> infoMap = new HashMap<>();        
        infoMap.put("buildTime","2006");        
        builder.withDetail("runTime",System.currentTimeMillis())
                .withDetail("company","传智教育");        
        builder.withDetails(infoMap);    
    }
}

为Health端点添加自定义指标

@Component
public class AppHealthContributor extends AbstractHealthIndicator {    
    @Override    
    protected void doHealthCheck(Health.Builder builder) throws Exception {        
        boolean condition = true;        
        if(condition){            
            Map<String,Object> infoMap = new HashMap<>();            
            infoMap.put("buildTime","2006");            
            builder.withDetail("runTime",System.currentTimeMillis())
                   .withDetail("company","传智教育");            
            builder.withDetails(infoMap);            
            builder.status(Status.UP);        
       }else{           
            builder.status(Status.DOWN);        
       }    
   }
}

自定义监控指标

为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;    
    }
}

自定义端点

@Component
@Endpoint(id="pay")
public class PayEndPoint {    
    @ReadOperation    
    public Object getPay(){        
        //调用业务操作,获取支付相关信息结果,最终return出去        
        Map payMap = new HashMap();           
        payMap.put("level 1",103);        
        payMap.put("level 2",315);        
        payMap.put("level 3",666);        
        return payMap;    
   }
}
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值