springboot实战demo
构建工程
第一步:进入https://start.spring.io/
可以选maven或者Gradle方式,我这里选的是maven,然后把group和artitact。
导入到IDEA中
说明一下:我的jdk是1.8,另外上面的springboot的版本我选择的是
org.springframework.boot
spring-boot-starter-parent
1.5.20.RELEASE
我这里添加了web,然后下载到本地,--->解压---->使用工具打开,我这里使用的是idea
可以看到它已经给我们创建了一个类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; } }
运行
JVM
通用(General)
在application.properties文件中添加
management.port = 8081
这里的springboot版本
org.springframework.boot
spring-boot-starter-parent
1.4.0.RELEASE
org.springframework.boot
spring-boot-starter-actuator
主要是为了演示,避免高版本涉及到授权的问题
然后启动项目
表示启动成功
访问以下http://localhost:8080/hello
正常访问
再访问http://localhost:8081/health
状态是UP即就是服务正常
线程(Threading)
访问http://localhost:8081/dump
以上就可以查看线程日志
然后我们可以使用jdk下面的jconsole.exe
再把这个线程名称复制到上面的线程日志里找找
通过线程日志可以找到线程id,线程状态等信息。
内存(Memory)
访问http://localhost:8081/metrics
格式调整一下
{
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
: ,
:
}
集合上面的jconsole.exe运行情况,可以看出两边描述的是一样的,(这里两个数据不一样是因为不是同一时刻的数据)
指标(Metrics)
Counter: 计数器, 事件发生的总次数, 会员登录的总次数, cache请求的总次数等.Gauge: 计量表, 某一时刻的数据,如仪表盘上的温度,速度等,网络上如内存,cpu,网络等Meters: 事件发生的频率, 如1分钟,5分钟和15分钟总共发生多少次. 如1分钟我们消费了多少消息, 5分钟发送了多少消息, 如linux下的uptime和top工具.Histogram: 柱状图, 主要是处理事件值的区间值, 如最大,最小,平均, 95%的标准偏差. 通常和处理的时长或个数有关联,如处理某一类任务耗费的事件,单位事件处理的任务数等.Timers: timer是发生频率和柱状图的组合,如QPS,我们要统计请求的频率,同时在统计每次请求的时间等.
日志(Logging)
方法http://localhost:8081/mappings
{
: {
:
},
: {
:
},
: {
:
},
: {
: ,
:
},
: {
: ,
:
},
: {
: ,
:
}
}
刚好是我们项目启动时候的mappings日志
访问<http://localhost:8081/heapdump
把对日志下载下来
解压后为heapdump2019-04-06-14-07-live6212508457651263790.hprof日志文件
然后就可以使用比如说MAT等工具进行分析。
访问http://localhost:8081/trace
可以查看系统被访问的情况
[{
: ,
: {
: ,
: ,
: {
: {
: ,
: ,
: ,
: ,
: ,
: ,
:
},
: {
: ,
:
}
}
}
}, {
: ,
: {
: ,
: ,
: {
: {
: ,
: ,
: ,
: ,
: ,
: ,
:
},
: {
: ,
: ,
: ,
: ,
:
}
}
}
}]
其他管理相关
可以在官网找到对应的
https://docs.spring.io/spring-boot/docs/1.4.0.BUILD-SNAPSHOT/reference/htmlsingle/
可以修改为你的版本号进行访问
另外在项目启动时候也是可以冲日志中看到