如何给程序添加监控?

11 篇文章 0 订阅
5 篇文章 0 订阅

监控程序

监控内容:
在程序里面写一个get方法,返回当前版本、服务器和端口号、运行时长,目前程序在做的事情数据、MySQL连接是否正常。

代码示范:

//Controller层
@GetMapping(value="/getHeartbeat",produces="application/json;charset=utf-8")
    @ApiOperation(value="获取程序的心跳", notes="获取程序的心跳")
    public AjaxResult getHeartbeat(){
        try {
            return xxxService.getHeartbeat();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
//xxxServiceImpl

    public  String vers="v1202";
    @Value("${server.port}")
    public  String serverPort;
    
 @Override
    public AjaxResult getHeartbeat() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("vers",vers);
        map.put("tmct","xxx:"+serverPort);
        SimpleDateFormat format2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = format2.format(new Date());
        String s = dateDiff(getServerStartTime(), date);
        map.put("runing",s);
        //业务数据
        map.put("run_ok/no",数据大小或值);
        map.put("mysql",MysqlListenerUtil.mysqlMonitor());
        return AjaxResult.success(map);
    }


    /*
     * 	获取程序启动时间
     */
    public String getServerStartTime(){
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        SimpleDateFormat format2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = format2.format(new Date(time));
        return date;
    }

    /**
     *
     * @param startTime
     * @param endTime
     * @return 运行时间 天,时,分,秒
     */
    public String dateDiff(String startTime, String endTime) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        String result = "0";
        // 获得两个时间的毫秒时间差异
        try {
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            day = diff / nd;// 计算差多少天
            hour = diff % nd / nh + day * 24;// 计算差多少小时
            min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
            sec = diff % nd % nh % nm / ns;// 计算差多少秒
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String str  = day+"d."+(hour - day * 24)+"h."+(min - day * 24 * 60) +"m."+sec+"s";
        log.info("时间相差:" + day + "天" + (hour - day * 24) + "小时"
                + (min - day * 24 * 60) + "分钟" + sec + "秒。");
        return str;
    }

//mysql监控类
public class MysqlListenerUtil {

    public static boolean mysqlMonitor() {
        //数据库加载器
        String driverName="com.mysql.cj.jdbc.Driver";
        //端口号以及数据库名称
        String url="jdbc:mysql://localhost:3306/xxxxx?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
        //用户名
        String userName="root";
        //用户密码
        String userPwd="root";
        //数据库连接成功
        boolean isSuccess = false;
        try {
            Class.forName(driverName);
            Connection dbConn = DriverManager.getConnection(url,userName,userPwd);
            isSuccess = true;
            System.out.println("连接数据库成功"+dbConn);
        }catch (Exception e) {
            if(e.toString().contains("Too many connections")){
                String msg = "Too many connections";
                System.out.println("数据库连接失败:"+msg);
            }

        }
        return isSuccess;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值