34. 应用监控【监控端点配置】




       当一个 Spring Boot 项目运行时,开发者需要对 Spring Boot 项目进行实时监控来获取项目的运行情况,在项目出错时能够实现自动报警等。 Spring Boot 提供了actuator 来帮助开发者获取应用程序的实时运行数据。开发者可以选择使用 HTTP 端点或JMX来管理和监控应用程序,获取应用程序的运行数据,包括健康状况、应用信息、内存使用情况等。

1、开启端点

       在Spring Boot 中开启应用监控非常容易 ,只需要添spring-boot-starter-actuator 依赖即可。依赖如下:

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

       开发者可以使用执行器中的端点( EndPoints )对应用进行监控或者与应用进行交互,Spring Boot 默认包含许多端点 ,如下图所示:
在这里插入图片描述
       如果系统是一个web应用的话,还会有其他端点:
在这里插入图片描述
       这些端点大部分都是默认开启的, 只有 shutdown 端点未开启,如果需要开启,可以在 application.properties 文件中添加以下配置信息:

#开启shutdown端点
management.endpoint.shutdown.enabled=true

       如果开发者不想暴露这么多端点,那么可以关闭默认配置,然后手动指定需要开启哪些端点, 如下配置表示关闭所有端点,只开启 info 端点:

#关闭所有端点,只开启 info 端点
management.endpoints.enabled-by-default=false 
management.endpoint.info.enabled=true

2、暴露端点

       由于有的端点包含敏感信息,因此端点启用和暴露是两码事,以下图展示了默认暴露的断点情况。
在这里插入图片描述
       在web应用中默认暴露了health和info两个端点,以下是启动日志:
在这里插入图片描述
       通过日志提供的路径访问该地址:localhost:8080/actuator
在这里插入图片描述
       我们可以在配置文件中自定义需要暴露哪些端点,例如要暴露 mappings、metrics 端点,添加如下配置即可:

#暴露指定端点
management.endpoints.web.exposure.include=mappings,metrics

在这里插入图片描述

#暴露所有端点
management.endpoints.web.exposure.include=*

在这里插入图片描述

3、端点保护

       如果这些端点需要对外提供服务,那么最好能够将这些端点保护起来,若 classpath 中存在 Spring Security ,则默认使用 Spring Security 保护,使用 Spring Security 保护的步骤很简单,首先添加 Spring Security 依赖:

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

Java配置文件

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception{
        httpSecurity.requestMatcher(EndpointRequest.toAnyEndpoint())
                .authorizeRequests()
                .anyRequest().hasAnyRole("admin")
                .and()
                .httpBasic();
    }
}

       在 HttpSecurity 中配置 Endpoint 都需要具有 admin 角色才能访问,同时开启 httpBasic 认证。注意, endpointRequest.toAnyEndpoint() 表示匹配所有的 endpoint,例如 shutdown、mappings、health 等,但是不包括开发者通过@RequestMapping 注解定义的接口,这里为了演示方便, Spring Security 就不连接数据 了, 直接在 appIication. properties 定义一个用户进行测试,代码如下:

spring.security.user.name=admin
spring.security.user.password=123456
spring.security.user.roles=admin

       定义完成后启动 Spring Boot 项目 再去访问 health 端点,需要登录后才可以访问
在这里插入图片描述

4、端点晌应缓存

       对于一些不带参数的端点请求会自动进行缓存,我们可以通过如下方式配置缓存时间。这个配置表示 beans 端点的缓存时间为 100s ,如果要配置其他端点,只需将 beans 修改为其他端点名称即可。注意,如果端点添加了 Spring Security 保护 ,此时 Principal 会被视为端点的输入,因此端点响应将不被缓存。

management.endpoint.beans.cache.time-to-live=100s

5、路径映射

       默认情况下 ,所有端点都暴露在/actuator路径下,例如 health 端点的访问路径是/actuator/health ,如果开发者需要对端点路径进行定制,可以通过如下配置进行,访问地址变为:localhost:8080/healthcheck

#路径映射
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck

6、CORS 支持

       所有端点默认都没有开启跨域,开发者可以通过如下配置快速开启 CORS 支持进而实现跨域。这个配置表示允许端点处理来自 http://localhost:8081 地址的请求,允许的请求方法为 GET、POST

management.endpoints.web.cors.allowed-origins=http://localhost:8081
management.endpoints.web.cors.allowed-methods=GET,POST
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Spring Boot和Vue项目中配置应用监控,可以采取以下步骤: 1. 在Spring Boot项目中添加Spring Boot Actuator依赖,以便可以访问应用监控端点。可以在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` 2. 配置Spring Boot Actuator端点的访问路径和安全性。可以在application.properties或application.yml文件中添加以下配置: ``` # 配置Actuator端点的访问路径 management.endpoints.web.base-path=/actuator # 开启Actuator端点的安全认证 management.endpoint.health.show-details=always management.endpoints.web.exposure.include=health,info,metrics management.endpoint.info.enabled=true management.endpoint.metrics.enabled=true management.security.enabled=true management.security.roles=ACTUATOR_ADMIN ``` 3. 在Vue项目中添加Vue.js Devtools插件,以便可以实时查看Vue组件的状态和性能。可以在Chrome浏览器中安装Vue.js Devtools插件。 4. 配置Vue.js Devtools插件的选项,以便可以连接到本地的Vue.js应用程序。可以在Chrome浏览器中打开Vue.js Devtools插件,然后在选项中添加以下配置: ``` { "host": "localhost", "port": 8081, "https": false } ``` 其中,host是Vue.js应用程序的主机名,port是应用程序的端口号,https表示是否使用HTTPS协议。 5. 在Vue组件中添加性能监控代码,以便可以实时查看组件的性能数据。可以在Vue组件的mounted()方法中添加以下代码: ``` mounted() { this.$nextTick(() => { if (window.performance && window.performance.mark) { window.performance.mark('vue-app-mounted') } }) } ``` 其中,window.performance.mark()方法可以在浏览器的性能分析工具中创建一个时间戳,以便可以测量组件的渲染时间。 6. 在Spring Boot项目中添加Micrometer依赖,以便可以将Vue.js应用程序的性能数据发送到监控系统。可以在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.1.4</version> </dependency> ``` 7. 配置Micrometer的Prometheus注册表,以便可以将Vue.js应用程序的性能数据发送到Prometheus监控系统。可以在application.properties或application.yml文件中添加以下配置: ``` # 配置Prometheus注册表 management.metrics.export.prometheus.enabled=true management.metrics.export.prometheus.endpoint=/prometheus ``` 其中,management.metrics.export.prometheus.enabled表示是否启用Prometheus注册表,management.metrics.export.prometheus.endpoint表示Prometheus注册表的访问路径。 8. 在Prometheus监控系统中添加Vue.js应用程序的监控指标,以便可以实时查看应用程序的性能数据。可以在Prometheus的配置文件中添加以下配置: ``` - job_name: 'vue-app' metrics_path: '/prometheus' static_configs: - targets: ['localhost:8081'] ``` 其中,job_name是监控任务的名称,metrics_path是Prometheus注册表的访问路径,targets是Vue.js应用程序的地址和端口号。 9. 在Grafana监控系统中创建Vue.js应用程序的监控仪表板,以便可以实时查看应用程序的性能数据。可以在Grafana中添加以下数据源: ``` { "name": "Prometheus", "type": "prometheus", "url": "http://localhost:9090", "access": "proxy", "isDefault": true } ``` 其中,url是Prometheus监控系统的地址和端口号。 然后,可以在Grafana中创建一个新的仪表板,然后添加Vue.js应用程序的监控指标,并配置仪表板的数据展示方式和报警规则,以便可以及时发现应用程序的性能问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值