spring-boot-starter-actuator监控接口详解

2019/05/09 16:51
阅读数 5

<div data-note-content="" class="show-content"> <div class="show-content-free"> <h1>spring-boot-starter-actuator 是什么</h1> <p>一句话,actuator是监控系统健康情况的工具。</p> <h2>- 怎么用?</h2> <h2>1. 添加 POM依赖</h2> <pre class="hljs xml"><code class="xml"> <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-actuator<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span> </code></pre> <h2>2. 启动的时候就会有下面这些提示.</h2> <pre class="hljs xml"><code class="xml">Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<span class="hljs-tag">&lt;<span class="hljs-name">java.uti</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">error</span>],<span class="hljs-attr">produces</span>=<span class="hljs-string">[text/html]}</span>" <span class="hljs-attr">onto</span> <span class="hljs-attr">public</span> <span class="hljs-attr">org.springframework.web.ser</span> <span class="hljs-attr">Mapped</span> <span class="hljs-attr">URL</span> <span class="hljs-attr">path</span> [/<span class="hljs-attr">webjars</span>/**] <span class="hljs-attr">onto</span> <span class="hljs-attr">handler</span> <span class="hljs-attr">of</span> <span class="hljs-attr">type</span> [<span class="hljs-attr">class</span> <span class="hljs-attr">org.springframework.we</span> <span class="hljs-attr">Mapped</span> <span class="hljs-attr">URL</span> <span class="hljs-attr">path</span> [/**] <span class="hljs-attr">onto</span> <span class="hljs-attr">handler</span> <span class="hljs-attr">of</span> <span class="hljs-attr">type</span> [<span class="hljs-attr">class</span> <span class="hljs-attr">org.springframework.web.servle</span> <span class="hljs-attr">Mapped</span> <span class="hljs-attr">URL</span> <span class="hljs-attr">path</span> [/**/<span class="hljs-attr">favicon.ico</span>] <span class="hljs-attr">onto</span> <span class="hljs-attr">handler</span> <span class="hljs-attr">of</span> <span class="hljs-attr">type</span> [<span class="hljs-attr">class</span> <span class="hljs-attr">org.springframewor</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">metrics</span>/{<span class="hljs-attr">name:.</span>*}],<span class="hljs-attr">methods</span>=<span class="hljs-string">[GET],produces</span>=<span class="hljs-string">[application/json]}</span>" <span class="hljs-attr">onto</span> <span class="hljs-attr">p</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">metrics</span> || /<span class="hljs-attr">metrics.json</span>],<span class="hljs-attr">methods</span>=<span class="hljs-string">[GET],produces</span>=<span class="hljs-string">[application/json]}</span>" <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">trace</span> || /<span class="hljs-attr">trace.json</span>],<span class="hljs-attr">methods</span>=<span class="hljs-string">[GET],produces</span>=<span class="hljs-string">[application/json]}</span>" <span class="hljs-attr">ont</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">configprops</span> || /<span class="hljs-attr">configprops.json</span>],<span class="hljs-attr">methods</span>=<span class="hljs-string">[GET],produces</span>=<span class="hljs-string">[application</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">autoconfig</span> || /<span class="hljs-attr">autoconfig.json</span>],<span class="hljs-attr">methods</span>=<span class="hljs-string">[GET],produces</span>=<span class="hljs-string">[application/j</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">info</span> || /<span class="hljs-attr">info.json</span>],<span class="hljs-attr">methods</span>=<span class="hljs-string">[GET],produces</span>=<span class="hljs-string">[application/json]}</span>" <span class="hljs-attr">onto</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">dump</span> || /<span class="hljs-attr">dump.json</span>],<span class="hljs-attr">methods</span>=<span class="hljs-string">[GET],produces</span>=<span class="hljs-string">[application/json]}</span>" <span class="hljs-attr">onto</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">mappings</span> || /<span class="hljs-attr">mappings.json</span>],<span class="hljs-attr">methods</span>=<span class="hljs-string">[GET],produces</span>=<span class="hljs-string">[application/json]</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">health</span> || /<span class="hljs-attr">health.json</span>],<span class="hljs-attr">produces</span>=<span class="hljs-string">[application/json]}</span>" <span class="hljs-attr">onto</span> <span class="hljs-attr">public</span> <span class="hljs-attr">jav</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">beans</span> || /<span class="hljs-attr">beans.json</span>],<span class="hljs-attr">methods</span>=<span class="hljs-string">[GET],produces</span>=<span class="hljs-string">[application/json]}</span>" <span class="hljs-attr">ont</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">env</span>/{<span class="hljs-attr">name:.</span>*}],<span class="hljs-attr">methods</span>=<span class="hljs-string">[GET],produces</span>=<span class="hljs-string">[application/json]}</span>" <span class="hljs-attr">onto</span> <span class="hljs-attr">publi</span> <span class="hljs-attr">Mapped</span> "{[/<span class="hljs-attr">env</span> || /<span class="hljs-attr">env.json</span>],<span class="hljs-attr">methods</span>=<span class="hljs-string">[GET],produces</span>=<span class="hljs-string">[application/json]}</span>" <span class="hljs-attr">onto</span> <span class="hljs-attr">pu</span> </span></code></pre> <h3>- actuator 的端点分为3类</h3> <ol> <li><strong>应用配置类</strong></li> </ol> <blockquote> <p>/configprops /autoconfig /beans /env /info /mappings</p> </blockquote> <ol start="2"> <li><strong>度量指标类</strong></li> </ol> <blockquote> <p>/dump /health</p> </blockquote> <ol start="3"> <li><strong>操作控制类</strong></li> </ol> <ul> <li> <h3>下面找几个来解释</h3> <ul> <li> <h5>/autoconfig</h5> </li> </ul> </li> </ul> <p>自动化配置报告,可以看出一些自动化配置为什么没有生效</p> <br> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 572px; background-color: transparent;"> <div class="image-view" data-width="933" data-height="572"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-02fd1309ccced1d9.png" data-original-width="933" data-original-height="572" data-original-format="image/png" data-original-filesize="60025" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-02fd1309ccced1d9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/933/format/webp"></div> </div> <div class="image-caption">image.png</div> </div> <ul> <li> <h5>/beans</h5> </li> </ul> <p>可以看到定义的所有的bean</p> <br> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 621px; background-color: transparent;"> <div class="image-view" data-width="969" data-height="621"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-6c39a9695df30828.png" data-original-width="969" data-original-height="621" data-original-format="image/png" data-original-filesize="55910" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-6c39a9695df30828.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/969/format/webp"></div> </div> <div class="image-caption">image.png</div> </div> <ul> <li> <h5>/configprops</h5> </li> </ul> <p>可以看到application.properties里面的信息</p> <br> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 580px; background-color: transparent;"> <div class="image-view" data-width="930" data-height="580"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-7b75c7b27e34cea9.png" data-original-width="930" data-original-height="580" data-original-format="image/png" data-original-filesize="53991" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-7b75c7b27e34cea9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/930/format/webp"></div> </div> <div class="image-caption">image.png</div> </div> <ul> <li> <h5>/env</h5> </li> </ul> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 580px; background-color: transparent;"> <div class="image-view" data-width="865" data-height="580"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-852d1f9105a166f7.png" data-original-width="865" data-original-height="580" data-original-format="image/png" data-original-filesize="74536" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-852d1f9105a166f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/865/format/webp"></div> </div> <div class="image-caption">image.png</div> </div> <ul> <li> <h5>/mappings</h5> </li> </ul> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 563px; background-color: transparent;"> <div class="image-view" data-width="901" data-height="563"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-91db57ca42a730a2.png" data-original-width="901" data-original-height="563" data-original-format="image/png" data-original-filesize="68070" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-91db57ca42a730a2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/901/format/webp"></div> </div> <div class="image-caption">image.png</div> </div> <ul> <li> <h5>/info</h5> </li> </ul> <p>返回application.properties文件中info开头的配置信息,如:</p> <pre class="hljs bash"><code class="bash"><span class="hljs-comment"># /info端点信息配置</span> info.app.name=spring-boot-hello info.app.version=v1.0.0 </code></pre> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 376px; background-color: transparent;"> <div class="image-view" data-width="848" data-height="376"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-c5644531cabf5726.png" data-original-width="848" data-original-height="376" data-original-format="image/png" data-original-filesize="22014" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-c5644531cabf5726.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/848/format/webp"></div> </div> <div class="image-caption">image.png</div> </div> <hr> <p><strong>下面是度量指标类</strong></p> <ul> <li> <h5>/metrics</h5> </li> </ul> <p></p><div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 565px; background-color: transparent;"> <div class="image-view" data-width="765" data-height="565"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-7802726678e69cc0.png" data-original-width="765" data-original-height="565" data-original-format="image/png" data-original-filesize="49002" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-7802726678e69cc0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/765/format/webp"></div> </div> <div class="image-caption">image.png</div> </div><br> 我们也可以自定实现 <code>CounterService</code> 接口来实现count指标.<br> <div class="image-package"> <div class="image-container" style="max-width: 516px; max-height: 270px; background-color: transparent;"> <div class="image-view" data-width="516" data-height="270"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-bbcb505a057208c2.png" data-original-width="516" data-original-height="270" data-original-format="image/png" data-original-filesize="30110" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-bbcb505a057208c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/516/format/webp"></div> </div> <div class="image-caption">image.png</div> </div><br> 也可以用 [/metrics/{name:.*}] 如: /metrics/mem.free 来获取单个指标信息<br> <div class="image-package"> <div class="image-container" style="max-width: 657px; max-height: 326px; background-color: transparent;"> <div class="image-view" data-width="657" data-height="326"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-3478e728717daeae.png" data-original-width="657" data-original-height="326" data-original-format="image/png" data-original-filesize="16524" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-3478e728717daeae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/657/format/webp"></div> </div> <div class="image-caption">image.png</div> </div><p></p> <ul> <li> <h5>/health</h5> </li> </ul> <p>可以通过实现 <code>HealthIndicator</code> 接口来实现健康检查,返回值的状态信息在<code>org.springframework.boot.actuate.health.Status</code>内<br> </p><div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 478px; background-color: transparent;"> <div class="image-view" data-width="1071" data-height="478"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-843372a992e100ea.png" data-original-width="1071" data-original-height="478" data-original-format="image/png" data-original-filesize="567898" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-843372a992e100ea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp"></div> </div> <div class="image-caption">image.png</div> </div><br> <div class="image-package"> <div class="image-container" style="max-width: 387px; max-height: 420px; background-color: transparent;"> <div class="image-view" data-width="387" data-height="420"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-e253daa117f154bc.png" data-original-width="387" data-original-height="420" data-original-format="image/png" data-original-filesize="19746" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-e253daa117f154bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/387/format/webp"></div> </div> <div class="image-caption">image.png</div> </div><p></p> <ul> <li> <h5>/dump</h5> </li> </ul> <p>调用 <code>java.lang.management.ThreadMXBean</code>的<br> <code>public ThreadInfo[] dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers);</code> 方法来返回活动线程的信息<br> </p><div class="image-package"> <div class="image-container" style="max-width: 457px; max-height: 384px; background-color: transparent;"> <div class="image-view" data-width="457" data-height="384"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-8e9ba27da0ec84c2.png" data-original-width="457" data-original-height="384" data-original-format="image/png" data-original-filesize="44141" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-8e9ba27da0ec84c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/457/format/webp"></div> </div> <div class="image-caption">image.png</div> </div><br> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 509px; background-color: transparent;"> <div class="image-view" data-width="769" data-height="509"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-8e046b9f2e7cd8b8.png" data-original-width="769" data-original-height="509" data-original-format="image/png" data-original-filesize="44618" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-8e046b9f2e7cd8b8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/769/format/webp"></div> </div> <div class="image-caption">image.png</div> </div><p></p> <ul> <li> <h5>操作控制类</h5> </li> </ul> <p>如:/shutdown ,通过在application.properties文件中添加<br> <code>endpoints.shutdown.enabled=true</code><br> 来开启<br> </p><div class="image-package"> <div class="image-container" style="max-width: 647px; max-height: 382px; background-color: transparent;"> <div class="image-view" data-width="647" data-height="382"><img data-original-src="//upload-images.jianshu.io/upload_images/9211971-306dc38223319b4e.png" data-original-width="647" data-original-height="382" data-original-format="image/png" data-original-filesize="21508" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/9211971-306dc38223319b4e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/647/format/webp"></div> </div> <div class="image-caption">image.png</div> </div><br> <del><strong>貌似上面命令直接把应用关了.....</strong>.</del><p></p>

      </div>
    </div>
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部