chapter15:springboot与监控管理

这篇具有很好参考价值的文章主要介绍了chapter15:springboot与监控管理。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Spring Boot与监控管理视频

1. 简介

通过引入spring-boot-starter-actuator, 可以使用SpringBoot为我们提供的准生产环境下的应用监控和管理功能。我们可以通过http, jmx, ssh协议来进行操作,自动得到审计、健康及指标信息等。

步骤:

  • 引入spring-boot-starter-actuator
  • 通过http方式访问监控端点;
  • 可进行shutdown (post提交,此端点默认关闭);

监控和管理端点:

端点名 描述
autoconfig 所有自动配置信息
auditevents 审计事件
beans 所有Bean的信息
configprops 所有配置属性
dump 线程状态信息
env 当前环境信息
health 应用健康状况
info 当前应用信息
metrics 应用的各项指标
mappings 应用的@RequestMapping映射路径
shutdown 关闭当前应用(默认关闭)
trace 追踪信息(最新的http请求)

http://localhost:8080/beans

http://localhost:8080/health

http://localhost:8080/trace

http://localhost:8080/metrics

http://localhost:8080/mappings

http://localhost:8080/info

2. shutdown端点的使用

首先在application中开启shutdown端点,默认是关闭的。

# 关闭认证
management.security.enabled=false
# 开启shutdown端点
endpoints.shutdown.enabled=true

启动应用后,使用postman发送post请求 : http://localhost:8080/shutdown; 发现报跨域访问限制异常。

{
    "timestamp": 1691418900478,
    "status": 403,
    "error": "Forbidden",
    "message": "Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.",
    "path": "/shutdown"
}

什么是 CSRF ,这是一个 WEB 应用安全的问题,CSRF(Cross-site request forgery 跨站请求伪造,也被称为“One Click Attack” 或者Session Riding,攻击方通过伪造用户请求访问受信任站点。

因为我在应用中引入了spring-security, 它引入了CSRF,默认是开启。CSRF和RESTful技术有冲突。CSRF默认支持的方法: GET|HEAD|TRACE|OPTIONS,不支持POST。

其实跨域攻击操作过程比较简单,就是如果你不采取任何限制的时候,对 POST 相对风险系数比较高的访问,用户可以伪造请求,然后对服务器进行攻击和修改。比如说通过伪造 POST 请求,然后能够将用户的数据删除。

参考: Spring security CSRF 跨域访问限制问题 - 知乎 (zhihu.com)

我们只需要禁用CSRF就可以了。http.csrf().disable();

@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 定制请求的授权规则
     *
     * @param http the {@link HttpSecurity} to modify
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll()
                // VIP1角色的用户才能访问level1的页面,其他同理
                .antMatchers("/level1/**").hasRole("VIP1")
                .antMatchers("/level2/**").hasRole("VIP2")
                .antMatchers("/level3/**").hasRole("VIP3");

        // 开启自动配置的登录功能
//        http.formLogin();
        http.formLogin().usernameParameter("uname").passwordParameter("pwd")
                .loginPage("/userlogin").loginProcessingUrl("/userlogin");
        // 1. 如果没有访问权限,转发到/login请求来到登录页;
        // 2. 重定向到/login?error表示登录失败。 更多详细规定
        // 3. 默认post形式的/login代表处理登录提交
        // 4.如果定制loginPage,那么loginPage的post请求就是登录提交

        // 开启自动配置的注销功能, 访问/logout表示用户注销,清空session; 注销成功后来到首页;
        http.logout().logoutSuccessUrl("/");
        // 开启记住我的功能, 将cookie发给浏览器保存,以后登录带上这个cookie,只要通过服务器端的验证就可以免登录
        // 如果点击”注销“,也会删除这个cookie
//        http.rememberMe();
        http.rememberMe().rememberMeParameter("remember");

        // 禁用csrf
        http.csrf().disable();
    }

    /**
     * 定制认证规则
     *
     * @param auth the {@link AuthenticationManagerBuilder} to use
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("zhangsan").password("123456").roles("VIP1", "VIP2")
                .and().withUser("lisi").password("123456").roles("VIP2", "VIP3")
                .and().withUser("wangwu").password("123456").roles("VIP1", "VIP3");
    }
}

再次访问,应用被关闭。使用 curl -X POST http://localhost:8080/shutdown 也可以。

{
    "message": "Shutting down, bye..."
}

3. 定制端点信息

  • 定制端点一般通过endpoints + 端点名 + 属性名来设置;
  • 修改端点id (endpoints.beans.id=mybeans);
  • 开启远程应用关闭功能 (endpoints.shutdown.enabled=true);
  • 关闭端点(endpoints.beans.enabled=false);
  • 开启所需端点
    • endpoints.enabled=false;
    • endpoints.beans.enabled=true;
  • 定制端点访问路径 management.context-path=/manage;
  • 关闭http端点 management.port=-1;
management.security.enabled=false
info.app.id=springboot-security
info..app.version=1.0
#endpoints.metrics.enabled=false
#endpoints.autoconfig.enabled=false
endpoints.shutdown.enabled=true
# http://localhost:8080/bean
endpoints.beans.id=mybean
endpoints.beans.path=/bean
# http://localhost:8080/du
endpoints.dump.path=/du
# 关闭所有端点访问
endpoints.enabled=false
# 开启指定端点访问
endpoints.beans.enabled=true
# 所有端点访问根路径 http://localhost:8080/manage/bean
management.context-path=/manage
# 所有端点访问端口 http://localhost:8181/manage/bean
management.port=8181

4. 健康状态监控

开放health端点访问

endpoints.health.enabled=true

http://localhost:8181/manage/health 响应:

{
    status: "UP",
    diskSpace: {
        status: "UP",
        total: 362095308800,
        free: 336869535744,
        threshold: 10485760
    }
}

现在来模拟服务失败的场景,比如引入redis依赖

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

然后配置reids主机地址

# redis
spring.redis.host=192.168.111.129

因为我虚拟机上的redis服务没有启动,应用是无法连接redis的。我们来查看health端点状态:

http://localhost:8181/manage/health文章来源地址https://www.toymoban.com/news/detail-633840.html

{
    status: "DOWN",
    diskSpace: {
    status: "UP",
    total: 362095308800,
    free: 336868065280,
    threshold: 10485760
},
redis: {
    status: "DOWN",
    error: "org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool"
    }
}

启动远程redis服务后,再次查看health端点, 其实是RedisHealthIndicator提供了监控支持。

{
    status: "UP",
    diskSpace: {
    status: "UP",
    total: 362095308800,
    free: 335793897472,
    threshold: 10485760
},
redis: {
    status: "UP",
    version: "6.2.6"
    }
}

5. 自定义健康状态指示器

编写一个指示器,实现HealthIndicator接口

@Component
public class MyAppHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
//        Health.up().build()  成功
        // 自定义检查,直接返回失败
        return Health.down().withDetail("msg", "服务异常").build();
    }
}

启动应用后,查看健康端点

http://localhost:8181/manage/health

{
    status: "DOWN",
    myApp: {
    status: "DOWN",
    msg: "服务异常"
},
diskSpace: {
    status: "UP",
    total: 362095308800,
    free: 335793758208,
    threshold: 10485760
},
redis: {
    status: "UP",
    version: "6.2.6"
    }
}

到了这里,关于chapter15:springboot与监控管理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Boot指标监控及日志管理

    目录 一、添加Actuator功能 二、SpringBoot指标监控 Spring Boot Admin 1. 创建Spring Boot Admin服务端项目 2. 连接Spring Boot Admin项目 三、SpringBoot日志管理 Spring Boot Actuator可以帮助程序员监控和管理SpringBoot应用,比如健康检查、内存使用情况统计、线程使用情况统计等。我们在SpringBoot项目

    2024年02月06日
    浏览(31)
  • spring(15) SpringBoot启动过程

    最好的学习方式就是带着问题学习,在分析 SpringBoot 的启动过程前,先问大家 两个问题: 在启动过程中,SpringBoot 是在哪一步实例化 Bean 的? 答案: 在 SpringApplicatio 的 refresh() 方法刷新上下文的时候实例化的,对应本文的第 16 节。 ApplicationContext 作为一个 IOC 容器,底层是通

    2024年02月12日
    浏览(26)
  • React.js前端 + Spring Boot后端员工管理

    该项目是一个员工管理系统,前端使用 React.js 构建,后端使用 Spring Boot 和 Data JPA 和 Lombok 构建。它提供了有效管理员工信息的全面解决方案。 特征 响应式设计:响应式 UI 设计,确保跨各种设备的可用性。 数据验证:验证用户输入以确保数据完整性。 使用的技术 前端:R

    2024年04月28日
    浏览(41)
  • Spring Boot后端与Vue前端融合:构建高效旅游管理系统

    作者介绍: ✌️大厂全栈码农|毕设实战开发,专注于大学生项目实战开发、讲解和毕业答疑辅导。 🍅 获取源码联系方式请查看文末 🍅  推荐订阅精彩专栏 👇🏻 避免错过下次更新 Springboot项目精选实战案例 更多项目: CSDN主页YAML墨韵 学如逆水行舟,不进则退。学习如赶

    2024年04月28日
    浏览(49)
  • SpringBoot整理-Spring Boot配置

    Spring Boot 的配置系统是其核心功能之一,旨在简化 Spring 应用的配置过程。Spring Boot 提供了一种灵活的方式来配置你的应用,无论是通过外部配置文件,环境变量,命令行参数还是在代码中直接配置。以下是关于 Spring Boot 配置的几个重要方面: 配置文件 application.prop

    2024年01月25日
    浏览(42)
  • 【Spring Boot】SpringBoot 单元测试

    单元测试(unit testing),是指对软件中的最⼩可测试单元进⾏检查和验证的过程就叫单元测试。 1、可以⾮常简单、直观、快速的测试某⼀个功能是否正确。 2、使⽤单元测试可以帮我们在打包的时候,发现⼀些问题,因为在打包之前,所以的单元测试必须通过,否则不能打包

    2024年02月07日
    浏览(45)
  • SpringBoot教程(一)|认识Spring Boot

    安得广厦千万间,大庇天下寒士俱欢颜,风雨不动安如山,呜呼,何时眼前突兀见此屋,吾庐独破受冻死亦足! Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需

    2024年01月16日
    浏览(34)
  • 【Spring Boot】Spring Boot项目中如何查看springBoot版本和Spring的版本

    在项目中查看默认版本有两种方式如下 Spring Boot 的最新版本支持情况: 版本 发布时间 停止维护时间 停止商业支持 3.0.x 2022-11-24 2023-11-24 2025-02-24 2.7.x 2022-05-19 2023-11-18 2025-02-18 2.6.x 2021-12-17 2022-11-24 2024-02-24 2.5.x 2021-05-20 已停止 2023-08-24 2.4.x 2020-11-12 已停止 2023-02-23 2.3.x 2020-05-

    2024年02月11日
    浏览(86)
  • 课程项目设计--项目建立--宿舍管理系统--springboot后端

    前要 项目设计–宿舍管理系统 太长了,修改一下 暂时先加上下面依赖,还有的话以后在看 application.yml application-dev.yml 新加一个handler包,用于像mybatis自动填充的处理配置 具体配置mybaits-plus配置 注解 插件bean 这里先只配置分页插件 自动填充bean 这里有2种写法,注释掉的是低

    2024年02月12日
    浏览(30)
  • SpringBoot教程(三) | Spring Boot初体验

    上篇文章我们创建了SpringBoot 项目,并且进行了简单的启动。整个项目了里其实我们就动了两个文件,一个是pom.xml负责管理springboot的相关依赖,一个是springBoot的启动类。 pom文件中通过starter的形式大大简化了配置,不像以前一样需要引入大量的依赖配置,搞不好还得解决冲突

    2024年01月16日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包