天天看點

springboot實戰運維入門

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/

可以修改為你的版本号進行通路

另外在項目啟動時候也是可以沖日志中看到