ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

【运维监控】Prometheus+grafana监控spring boot 3运行情况

【运维监控】Prometheus+grafana监控spring boot 3运行情况 运维监控系列文章入口【运维监控】系列文章汇总索引最近在研究 AI BI智能数据分析 的落地实践。敬请期待后续专题实战系列《从零手把手教你搭建 AI 驱动的 BI 系统》将覆盖 Text2SQL、多轮对话、语义层、权限治理、生产级部署全链路代码可落地、坑点全复盘。文章目录一、prometheus二、grafana三、创建java应用及配置1、pom.xml2、application.properties3、java 类4、验证四、grafana集成prometheus监控springboot应用1、修改prometheus配置及重启2、导入grafana模板3、验证本示例通过spring boot自带的监控信息暴露出来然后将数据收集到prometheus中最后通过grafana的dashboard导入模板进行可视化。本示例分为四个部分即prometheus、grafana部署、springboot示例和最后的集成。说明本示例中的部署环境没有要求即应用部署的机器根据实际情况而定只要网络连通即可。关于java应用的监控本系列有文章如下【运维监控】influxdb 2.0telegraf 监控tomcat 8.5运行情况【运维监控】influxdb 2.0grafana 监控java 虚拟机以及方法耗时情况【运维监控】Prometheusgrafana监控tomcat运行情况【运维监控】Prometheusgrafana监控spring boot 3运行情况一、prometheus参考【运维监控】prometheusnode exportergrafana 监控linux机器运行情况关于prometheus的部署。二、grafana参考【运维监控】prometheusnode exportergrafana 监控linux机器运行情况 关于grafana的部署。三、创建java应用及配置本处使用的是一个简单spring boot 3的应用对外开启2个restful接口同时引入prometheus的包。1、pom.xml仅列出引入的版本、必须的依赖和编译器根据你自己的环境而定。propertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.0.0/version/parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdscopeprovided/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencydependencygroupIdio.micrometer/groupIdartifactIdmicrometer-registry-prometheus/artifactIdversion1.12.5/version/dependency/dependencies2、application.properties仅列出和本示例有关的内容其他忽略或默认。management.endpoints.web.exposure.include* management.endpoint.prometheus.enabledtrue3、java 类App.javapackagecom.win;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplicationpublicclassApp{//url/abc.jsonpublicstaticvoidmain(String[]args){SpringApplication.run(App.class,args);}}TestController.javapackagecom.win.test;importlombok.extern.slf4j.Slf4j;importorg.springframework.web.bind.annotation.*;importjava.sql.Date;importjava.text.SimpleDateFormat;Slf4jRestControllerRequestMapping(/test)CrossOriginpublicclassTestController{SimpleDateFormatdateFormatnewSimpleDateFormat(yyyy-MM-dd HH:mm:ss-SSSS);// http://localhost:8080/test/example// http://localhost:8080/test/example?messagetestGetMapping(/example)publicStringcompletion(RequestParam(valuemessage,defaultValue讲个笑话)Stringmessage){try{Thread.sleep(100);}catch(InterruptedExceptione){thrownewRuntimeException(e);}log.info(当前时间{},方法名称{},dateFormat.format(newDate(System.currentTimeMillis())),newObject(){}.getClass().getEnclosingMethod().getName());returnmessage;}// http://localhost:8080/test/example/test.json?messagetestGetMapping(/example/test.json)publicStringcompletion2(RequestParam(valuemessage,defaultValue讲个笑话)Stringmessage){try{Thread.sleep(100);}catch(InterruptedExceptione){thrownewRuntimeException(e);}log.info(当前时间{},方法名称{},dateFormat.format(newDate(System.currentTimeMillis())),newObject(){}.getClass().getEnclosingMethod().getName());returnmessage;}}4、验证启动应用查看http://localhost:8080/actuator/prometheus链接是否可获取到相关监控的信息即可。出现如下图即表示完成。四、grafana集成prometheus监控springboot应用1、修改prometheus配置及重启在prometheus的配置文件prometheus.yml中添加如下配置部分- job_name:springboot_node_3.105metrics_path:/actuator/prometheus# metrics_path defaults to /metrics# scheme defaults to http.static_configs: - targets:[192.168.3.105:8080]重启prometheus后并验证是否收集到监控信息。查看Prometheus是否监控到运行的spring boot应用如下图。查看prometheus是否收集到监控信息如下图查询jvm_memory_max_bytes2、导入grafana模板导入过程不再赘述参考文章【运维监控】Prometheusgrafana监控tomcat8.5 运行情况本示例导入的模板ID是12900和21319。3、验证以上所涉及的服务均能正常的运行以及验证都通过。本示例为了体现有更多的数据可验证以便能更清楚的看到效果建议自己多做一点接口的调用查看运行情况否则不能直观的体现出监控的结果。以上就完成了grafana集成prometheus监控springboot应用。至此关于java应用的监控本系列有文章如下【运维监控】influxdb 2.0telegraf 监控tomcat 8.5运行情况【运维监控】influxdb 2.0grafana 监控java 虚拟机以及方法耗时情况【运维监控】Prometheusgrafana监控tomcat运行情况【运维监控】Prometheusgrafana监控spring boot 3运行情况
返回列表