当前位置: 代码网 > it编程>编程语言>Java > 监控Spring Boot 项目运行情况操作方法

监控Spring Boot 项目运行情况操作方法

2024年08月28日 Java 我要评论
导语在实际开发中,经常会遇到想要获取到服务器应用的运行情况的场景。在微服务架构下对于每个应用运行情况的监控是保证系统高可用的关键。下面就来介绍一下,如何实现在spring boot的jar包中对系统的

导语

在实际开发中,经常会遇到想要获取到服务器应用的运行情况的场景。在微服务架构下对于每个应用运行情况的监控是保证系统高可用的关键。

下面就来介绍一下,如何实现在spring boot的jar包中对系统的运行情况进行监控操作。

添加依赖

  首先需要在项目的pom文件中引入如下的依赖

<!-- 获取系统信息 -->
        <dependency>
            <groupid>com.github.oshi</groupid>
            <artifactid>oshi-core</artifactid>
            <version>${oshi.version}</version>
        </dependency>
         <oshi.version>5.7.4</oshi.version>

获取信息

  接下来就是定义需要获取的系统信息内容有哪些

  • cpu
  • 内存
  • jvm
  • 系统
  • 系统文件目录

cpu

  对于cpu主要关注的如下一些参数

/**
     * 核心数
     */
    private int cpunum;
    /**
     * cpu总的使用率
     */
    private double total;
    /**
     * cpu系统使用率
     */
    private double sys;
    /**
     * cpu用户使用率
     */
    private double used;
    /**
     * cpu当前等待率
     */
    private double wait;
    /**
     * cpu当前空闲率
     */
    private double free;

jvm

/**
     * 当前jvm占用的内存总数(m)
     */
    private double total;
    /**
     * jvm最大可用内存总数(m)
     */
    private double max;
    /**
     * jvm空闲内存(m)
     */
    private double free;
    /**
     * jdk版本
     */
    private string version;
    /**
     * jdk路径
     */
    private string home;

内存

/**
     * 内存总量
     */
    private double total;
    /**
     * 已用内存
     */
    private double used;
    /**
     * 剩余内存
     */
    private double free;

系统情况

/**
     * 服务器名称
     */
    private string computername;
    /**
     * 服务器ip
     */
    private string computerip;
    /**
     * 项目路径
     */
    private string userdir;
    /**
     * 操作系统
     */
    private string osname;
    /**
     * 系统架构
     */
    private string osarch;

文件资源

/**
     * 盘符路径
     */
    private string dirname;
    /**
     * 盘符类型
     */
    private string systypename;
    /**
     * 文件类型
     */
    private string typename;
    /**
     * 总大小
     */
    private string total;
    /**
     * 剩余大小
     */
    private string free;
    /**
     * 已经使用量
     */
    private string used;
    /**
     * 资源的使用率
     */
    private double usage;

  这些都是对于系统应用的基础性的监测,在实际使用的时候还需要对有些信息进行深入的挖掘,在使用的过程中,在对需求量较小的情况下,这是一种比较可行的方式。但是这些内容都是来自实例内部的回报,并不能解决实例宕机,或者网络中断等场景中出现的问题。所以,只能是用来监控在实例正常运行的状态下的一些信息。

是如何获取到这些信息的

cpu

 /**
     * 设置cpu信息
     */
    private void setcpuinfo(centralprocessor processor)
    {
        // cpu信息
        long[] prevticks = processor.getsystemcpuloadticks();
        util.sleep(oshi_wait_second);
        long[] ticks = processor.getsystemcpuloadticks();
        long nice = ticks[centralprocessor.ticktype.nice.getindex()] - prevticks[centralprocessor.ticktype.nice.getindex()];
        long irq = ticks[centralprocessor.ticktype.irq.getindex()] - prevticks[centralprocessor.ticktype.irq.getindex()];
        long softirq = ticks[centralprocessor.ticktype.softirq.getindex()] - prevticks[centralprocessor.ticktype.softirq.getindex()];
        long steal = ticks[centralprocessor.ticktype.steal.getindex()] - prevticks[centralprocessor.ticktype.steal.getindex()];
        long csys = ticks[centralprocessor.ticktype.system.getindex()] - prevticks[centralprocessor.ticktype.system.getindex()];
        long user = ticks[centralprocessor.ticktype.user.getindex()] - prevticks[centralprocessor.ticktype.user.getindex()];
        long iowait = ticks[centralprocessor.ticktype.iowait.getindex()] - prevticks[centralprocessor.ticktype.iowait.getindex()];
        long idle = ticks[centralprocessor.ticktype.idle.getindex()] - prevticks[centralprocessor.ticktype.idle.getindex()];
        long totalcpu = user + nice + csys + idle + iowait + irq + softirq + steal;
        cpu.setcpunum(processor.getlogicalprocessorcount());
        cpu.settotal(totalcpu);
        cpu.setsys(csys);
        cpu.setused(user);
        cpu.setwait(iowait);
        cpu.setfree(idle);
    }

内存信息

/**
     * 设置内存信息
     */
    private void setmeminfo(globalmemory memory)
    {
        mem.settotal(memory.gettotal());
        mem.setused(memory.gettotal() - memory.getavailable());
        mem.setfree(memory.getavailable());
    }

服务器信息

 /**
     * 设置服务器信息
     */
    private void setsysinfo()
    {
        properties props = system.getproperties();
        sys.setcomputername(iputils.gethostname());
        sys.setcomputerip(iputils.gethostip());
        sys.setosname(props.getproperty("os.name"));
        sys.setosarch(props.getproperty("os.arch"));
        sys.setuserdir(props.getproperty("user.dir"));
    }

jvm信息

/**
     * 设置java虚拟机
     */
    private void setjvminfo() throws unknownhostexception
    {
        properties props = system.getproperties();
        jvm.settotal(runtime.getruntime().totalmemory());
        jvm.setmax(runtime.getruntime().maxmemory());
        jvm.setfree(runtime.getruntime().freememory());
        jvm.setversion(props.getproperty("java.version"));
        jvm.sethome(props.getproperty("java.home"));
    }

磁盘信息

/**
     * 设置磁盘信息
     */
    private void setsysfiles(operatingsystem os)
    {
        filesystem filesystem = os.getfilesystem();
        list<osfilestore> fsarray = filesystem.getfilestores();
        for (osfilestore fs : fsarray)
        {
            long free = fs.getusablespace();
            long total = fs.gettotalspace();
            long used = total - free;
            sysfile sysfile = new sysfile();
            sysfile.setdirname(fs.getmount());
            sysfile.setsystypename(fs.gettype());
            sysfile.settypename(fs.getname());
            sysfile.settotal(convertfilesize(total));
            sysfile.setfree(convertfilesize(free));
            sysfile.setused(convertfilesize(used));
            sysfile.setusage(arith.mul(arith.div(used, total, 4), 100));
            sysfiles.add(sysfile);
        }
    }

  有了这些信息之后,就可以对实例的运行情况进行实时的监控,当实例出现问题的时候,这些数据就不会出现,所以说,在一般的使用场景下这种方式还是比较有用的。如果可以对这些信息进行采集分析,对了解系统整体运行稳定性有一定的帮助。

到此这篇关于如何监控spring boot 项目运行情况的文章就介绍到这了,更多相关监控spring boot 项目运行内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com