0
点赞
收藏
分享

微信扫一扫

springboot实战运维入门

Raow1 2023-04-13 阅读 85


springboot实战demo

构建工程

第一步:进入https://start.spring.io/

springboot实战运维入门_spring

可以选maven或者Gradle方式,我这里选的是maven,然后把group和artitact。

springboot实战运维入门_spring_02

导入到IDEA中

说明一下:我的jdk是1.8,另外上面的springboot的版本我选择的是

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.20.RELEASE</version>
<relativePath/> 
</parent>

我这里添加了web,然后下载到本地,--->解压---->使用工具打开,我这里使用的是idea

springboot实战运维入门_html_03

可以看到它已经给我们创建了一个类SpringBootDemoApplication


package com.lawt.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.Map;


@RestController
@SpringBootApplication
public class SpringBootDemoApplication {


public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
@RequestMapping("/hello")
public Object hello(){
Map<String,String> resultMap=new HashMap<String, String>();
resultMap.put("11","aa");
resultMap.put("22","bb");
resultMap.put("33","cc");
return resultMap;
}


}


运行

springboot实战运维入门_html_04

JVM

通用(General)

在application.properties文件中添加

management.port = 8081

这里的springboot版本

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/>
</parent>
    <!-- actuator的依赖-->
    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

主要是为了演示,避免高版本涉及到授权的问题

然后启动项目

springboot实战运维入门_java_05

表示启动成功

访问以下http://localhost:8080/hello

springboot实战运维入门_html_06

正常访问

再访问http://localhost:8081/health

springboot实战运维入门_html_07

状态是UP即就是服务正常

线程(Threading)

访问http://localhost:8081/dump

springboot实战运维入门_spring_08

以上就可以查看线程日志

然后我们可以使用jdk下面的jconsole.exe

springboot实战运维入门_java_09

再把这个线程名称复制到上面的线程日志里找找

springboot实战运维入门_spring_10

通过线程日志可以找到线程id,线程状态等信息。

内存(Memory)

访问http://localhost:8081/metrics

springboot实战运维入门_spring_11

格式调整一下

{
"mem": 347977,
"mem.free": 189328,
"processors": 4,
"instance.uptime": 840521,
"uptime": 844145,
"systemload.average": -1.0,
"heap.committed": 303616,
"heap.init": 131072,
"heap.used": 114287,
"heap": 1839104,
"nonheap.committed": 45824,
"nonheap.init": 2496,
"nonheap.used": 44362,
"nonheap": 0,
"threads.peak": 44,
"threads.daemon": 40,
"threads.totalStarted": 71,
"threads": 43,
"classes": 6287,
"classes.loaded": 6287,
"classes.unloaded": 0,
"gc.ps_scavenge.count": 8,
"gc.ps_scavenge.time": 189,
"gc.ps_marksweep.count": 1,
"gc.ps_marksweep.time": 45,
"httpsessions.max": -1,
"httpsessions.active": 0,
"gauge.response.hello": 101.0,
"gauge.response.star-star": 11.0,
"counter.status.404.star-star": 1,
"counter.status.200.hello": 1
}

集合上面的jconsole.exe运行情况,可以看出两边描述的是一样的,(这里两个数据不一样是因为不是同一时刻的数据)

springboot实战运维入门_html_12

指标(Metrics)

Counter: 计数器, 事件发生的总次数, 会员登录的总次数, cache请求的总次数等.Gauge: 计量表, 某一时刻的数据,如仪表盘上的温度,速度等,网络上如内存,cpu,网络等Meters: 事件发生的频率, 如1分钟,5分钟和15分钟总共发生多少次. 如1分钟我们消费了多少消息, 5分钟发送了多少消息, 如linux下的uptime和top工具.Histogram: 柱状图, 主要是处理事件值的区间值, 如最大,最小,平均, 95%的标准偏差. 通常和处理的时长或个数有关联,如处理某一类任务耗费的事件,单位事件处理的任务数等.Timers: timer是发生频率和柱状图的组合,如QPS,我们要统计请求的频率,同时在统计每次请求的时间等.


日志(Logging)

方法http://localhost:8081/mappings

springboot实战运维入门_html_13

格式调整一下

{
"/webjars/**": {
"bean": "resourceHandlerMapping"
},
"/**": {
"bean": "resourceHandlerMapping"
},
"/**/favicon.ico": {
"bean": "faviconHandlerMapping"
},
"{[/hello]}": {
"bean": "requestMappingHandlerMapping",
"method": "public java.lang.Object com.lawt.springbootdemo.SpringBootDemoApplication.hello()"
},
"{[/error]}": {
"bean": "requestMappingHandlerMapping",
"method": "public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)"
},
"{[/error],produces=[text/html]}": {
"bean": "requestMappingHandlerMapping",
"method": "public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)"
}
}

刚好是我们项目启动时候的mappings日志

springboot实战运维入门_html_14

访问<http://localhost:8081/heapdump

把对日志下载下来

springboot实战运维入门_java_15

解压后为heapdump2019-04-06-14-07-live6212508457651263790.hprof日志文件

然后就可以使用比如说MAT等工具进行分析。

访问http://localhost:8081/trace

可以查看系统被访问的情况

[{
"timestamp": 1554529487384,
"info": {
"method": "GET",
"path": "/health",
"headers": {
"request": {
"host": "localhost:8080",
"connection": "keep-alive",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9"
},
"response": {
"X-Application-Context": "application",
"status": "404"
}
}
}
}, {
"timestamp": 1554529437620,
"info": {
"method": "GET",
"path": "/hello",
"headers": {
"request": {
"host": "localhost:8080",
"connection": "keep-alive",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9"
},
"response": {
"X-Application-Context": "application",
"Content-Type": "application/json;charset=UTF-8",
"Transfer-Encoding": "chunked",
"Date": "Sat, 06 Apr 2019 05:43:57 GMT",
"status": "200"
}
}
}
}]

其他管理相关

可以在官网找到对应的

https://docs.spring.io/spring-boot/docs/1.4.0.BUILD-SNAPSHOT/reference/htmlsingle/

可以修改为你的版本号进行访问

springboot实战运维入门_java_16

另外在项目启动时候也是可以冲日志中看到

springboot实战运维入门_html_17


通过以上这些东东咱们就可以解决一些线上或者测试环境的问题,比如死锁、内存溢出等,这样通过页面的方式就能查到相关信息。以上就是一些简单操作。


举报

相关推荐

0 条评论