SpringBoot之Actuator监控

一、引入依赖

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

二、监控配置

management:
  endpoints:
    enabled-by-default: false    # 用于开关监控端点,默认为true暴露所有端点信息
    web:
      exposure:
        include: '*' # 表示以web方式暴露所有监控端点 
  endpoint:    # 可以先禁用所有的监控端点,再打开单个端点,自定义的端点是默认打开的
    health:
      enabled: true    # 表示开关单个监控端点
      show-details: always # 展示详细信息
    info:
      enabled: true
    metrics:
      enabled: true

支持的暴露方式:

  • HTTP:默认只暴露healthinfo的端点
  • JMX:默认暴露所有端点
  • 除过health和info,剩下的端点都应该进行保护访问。如果引入SpringSecurity,则会默认配置安全访问规则

三、查看

访问 http://localhost:8080/actuator/**查看所有端点监控信息

四、自定义健康信息

继承AbstractHealthIndicator类,重写doHealthCheck方法

/**
 * 定制检查健康信息
 * @author cwj
 * @date 2021/9/10
 */
@Component
public class MyComHealthIndicator extends AbstractHealthIndicator{

    /**
     * 真实的检查方法
     * @param builder
     * @throws Exception
     */
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        //获取连接进行测试
        Map<String,Object> map = new HashMap<>();
        if(1 == 1){
            builder.up();
            map.put("count",1);
            map.put("ms",100);
        }else{
            builder.status(Status.OUT_OF_SERVICE);
            map.put("err","连接超时");
            map.put("ms",3000);
        }
        //withDetail方法用于携带返回数据,可将数据写入端口信息中
        builder.withDetail("code",100).withDetails(map);

    }
}

五、自定义端点

使用@Endpoin注解表示该类是端点类

@Component
@Endpoint(id = "container") //container表示端点名
public class DockerEndpoint {

    //DockerInfo表示属性,所有方法不允许有参数
    @ReadOperation //表示端点的读操作
    public Map getDockerInfo(){
        return Collections.singletonMap("info","docker started.......");
    }

    @WriteOperation //表示端点的写操作
    public void writerDockerInfo(){
        System.out.println("docker restarted.....");
    }
}

测试: 端点自定义成功

 六、监控的可视化工具

https://github.com/codecentric/spring-boot-admin

服务端

导入依赖

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

在启动类上加入@EnableAdminServer注解即可

访问服务端:http://localhost:9000/wallboard

客户端

导入依赖

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

将客户端注册到服务端中

  boot:
    admin:
      client:
        url: http://localhost:9000    # 服务端的端口号
        instance:
          prefer-ip: true # 使用ip名注册
  application:
    name: springboot-thymeleaf # 设置客服端名称

展示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot项目中,可以使用Actuator提供的监控功能来监控应用程序的运行状态。为了实现Vue的监控配置,可以按照以下步骤操作: 1. 配置Actuator:在Spring Boot项目中添加Actuator依赖,并在application.properties或application.yml文件中配置Actuator的端点和安全性等相关参数。 2. 配置CORS:由于Vue应用程序是通过浏览器访问的,因此需要在Spring Boot项目中配置CORS允许跨域访问。 3. 编写Vue组件:在Vue应用程序中编写一个组件,通过Axios等库调用Actuator的API获取监控数据,并将其展示在页面上。 4. 部署到同一服务器:为了避免CORS的问题,最好将Vue应用程序部署到与Spring Boot项目同一服务器上。 具体实现可以参考以下代码: 1. 配置Actuator: 在application.yml文件中添加如下配置: ```yaml management: endpoints: web: exposure: include: "*" endpoint: health: show-details: always security: enabled: false ``` 2. 配置CORS: 在Spring Boot项目中添加CORS配置类: ```java @Configuration public class CorsConfig { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("*") .allowedHeaders("*"); } }; } } ``` 3. 编写Vue组件: ```vue <template> <div> <h2>Actuator监控信息</h2> <table> <thead> <tr> <th>指标</th> <th>值</th> </tr> </thead> <tbody> <tr v-for="(value, key) in metrics" :key="key"> <td>{{ key }}</td> <td>{{ value }}</td> </tr> </tbody> </table> </div> </template> <script> import axios from 'axios'; export default { data() { return { metrics: {}, }; }, created() { axios.get('/actuator/metrics').then((res) => { this.metrics = res.data; }).catch((err) => { console.error(err); }); }, }; </script> ``` 以上代码实现了一个简单的Vue组件,通过Axios库调用Actuator的/metrics端点获取监控数据,并将其展示在表格中。 4. 部署到同一服务器: 将Vue应用程序部署到与Spring Boot项目同一服务器上,并通过nginx等服务器软件配置反向代理,将Vue应用程序的请求转发到Spring Boot项目中。这样可以避免CORS的问题,同时提高了应用程序的安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值