SpringBoot应用监控 您所在的位置:网站首页 英文名jay的寓意怎么写 SpringBoot应用监控

SpringBoot应用监控

2023-09-25 07:35| 来源: 网络整理| 查看: 265

点击关注公众号,实用技术文章及时了解

来源:blog.csdn.net/u013087026/

article/details/109536552

概述

微服务作为一项在云中部署应用和服务的新技术是当下比较热门话题,而微服务的特点决定了功能模块的部署是分布式的,运行在不同的机器上相互通过服务调用进行交互,业务流会经过多个微服务的处理和传递,在这种框架下,微服务的监控显得尤为重要。

而Actuator正是Spring Boot提供的对应用系统的监控和管理的集成功能,可以查看应用配置的详细信息,例如自动化配置信息、创建的Spring beans信息、系统环境变量的配置信息以及Web请求的详细信息等。如果使用不当或者一些不经意的疏忽,可能造成信息泄露等严重的安全隐患。

Actuator使用

Actuator应用监控使用只需要添加spring-boot-starter-actuator依赖即可,如下:

   org.springframework.boot    spring-boot-starter-actuator

可以在application.properties中指定actuator的访问端口、访问路径等信息:

# 访问示例:http://localhost:9595/monitor management:   endpoints:     web:       # actuator的访问路径,替换默认/actuator       base-path: /monitor       # 设置是否暴露端点 默认只有health和info可见       exposure:         # include: env   # 方式1: 暴露端点env,配置多个以,隔开         include: "*"     # 方式2: 包括所有端点,注意需要添加引号         # 排除端点         exclude: shutdown   server:     port: 9595  #新开监控端口,不和应用用同一个端口   endpoint:     health:       show-details: always # 显示db、redis、rabbti连接情况等     shutdown:       enabled: true  #默认情况下,除shutdown以外的所有端点均已启用。手动开启

此时,运行示例,访问/monitor/即可查看所有端点信息,再访问/monitor/env即可查看该应用全部环境属性,如图:

Endpoints(端点)介绍

Endpoints 是 Actuator 的核心部分,它用来监视应用程序及交互,spring-boot-actuator中已经内置了非常多的Endpoints(health、info、beans、httptrace、shutdown等等),同时也允许我们扩展自己的端点。

Endpoints 分成两类:原生端点和用户自定义端点;自定义端点主要是指扩展性,用户可以根据自己的实际应用,定义一些比较关心的指标,在运行期进行监控。

原生端点是在应用程序里提供的众多 restful api 接口,通过它们可以监控应用程序运行时的内部状况。原生端点又可以分成三类:

应用配置类: 可以查看应用在运行期间的静态信息:例如自动配置信息、加载的spring bean信息、yml文件配置信息、环境信息、请求映射信息;

度量指标类: 主要是运行期间的动态信息,例如堆栈、请求连、一些健康指标、metrics信息等;

操作控制类: 主要是指shutdown,用户可以发送一个请求将应用的监控功能关闭。

Actuator 默认提供了以下接口,具体如下表所示:

安全措施

如果上述请求接口不做任何安全限制,安全隐患显而易见。实际上Spring Boot也提供了安全限制功能。比如要禁用/env接口,则可设置如下:

endpoint:  env:     enabled: false

另外也可以引入spring-boot-starter-security依赖

    org.springframework.boot     spring-boot-starter-security

在application.properties中开启security功能,配置访问权限验证,这时再访问actuator功能时就会弹出登录窗口,需要输入账号密码验证后才允许访问。

spring:  security:     user:       password: 123456       name: jaler

为了只对actuator功能做权限验证,其他应用接口不做认证,我们可以重新定制下SpringSecurity。另外,关注Java知音公众号,回复“后端面试”,送你一份面试题宝典!

package com.jaler.common.common.config;   import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;   @Configuration @EnableWebSecurity public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {     @Autowired   Environment env;     @Override   protected void configure(HttpSecurity security) throws Exception {           String contextPath = env.getProperty("management.endpoints.web.base-path");         if(StringUtils.isEmpty(contextPath)) {             contextPath = "";         }         security.csrf().disable();         security.authorizeRequests()                 .antMatchers("/**"+contextPath+"/**")                 .authenticated()                 .anyRequest()                 .permitAll()                 .and()                 .httpBasic();        } }

再次访问http://localhost:9595/monitor,此时需要进行权限验证,如下图:

安全建议

只开放某些无敏感信息的端点。

打开安全限制并进行身份验证,访问Actuator接口时需要登录。

Actuator访问接口使用独立端口,并配置不对外网开放。

●【练手项目】基于SpringBoot的ERP系统,自带进销存+财务+生产功能

●分享一套基于SpringBoot和Vue的企业级中后台开源项目,代码很规范!

●能挣钱的,开源 SpringBoot 商城系统,功能超全,超漂亮!

PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我们吧!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有