2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

「Spring Boot 2.4 新特性」啟動(dòng)耗時(shí)詳細(xì)監(jiān)控

 路人甲Java 2021-06-22

背景

Spring Boot 項(xiàng)目隨著項(xiàng)目開發(fā)過程中引入中間件數(shù)量的增加,啟動(dòng)耗時(shí)
逐漸增加。

  • Spring 生態(tài)復(fù)雜,非官方插件并未嚴(yán)格按官方標(biāo)準(zhǔn)實(shí)現(xiàn)。例如 @Configuration 注解提供了 proxyBeanMethods 屬性默認(rèn)開啟,建議常見情況手動(dòng)關(guān)閉提高性能。筆者在觀察大部分非官方插件 stater 并未引入此屬性。諸如此類的優(yōu)化策略很多(建議翻一下筆者歷史博客),但往往被開發(fā)者忽略,導(dǎo)致使用該插件會(huì)影響應(yīng)用啟動(dòng)效率。

如上兩點(diǎn),我認(rèn)為 SpringBoot 啟動(dòng)緩慢和框架本身沒有太大關(guān)系,取決于開發(fā)者的能力。如何能夠在開發(fā)中準(zhǔn)確的分析啟動(dòng)過程,定位到每個(gè)耗時(shí)操作? 單純從啟動(dòng)日志的維度是無法實(shí)現(xiàn),Spring Boot 2.4.0 提供了啟動(dòng)過程監(jiān)控的端點(diǎn),非常方便的讓開發(fā)者在開發(fā)過程中觀察每個(gè)組件的初始化過程、消耗時(shí)間等。

上手體驗(yàn)

  • 引入 actuator 依賴

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 配置暴露 startup 端點(diǎn)

management:
  endpoints:
    web:
      exposure:
        include: startup
  • Main 啟動(dòng)類聲明緩沖池,這里注意若應(yīng)用依賴較多,建議把 capacity 容量參數(shù)設(shè)置大些,盡可能的保留全部監(jiān)控日志。

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        // 建議僅在開發(fā)或者排除時(shí)開啟此配置
        new SpringApplicationBuilder(DemoApplication.class)
                .applicationStartup(new BufferingApplicationStartup(20480))
                .run(args);
    }
}
  • 獲取啟動(dòng)數(shù)據(jù) ,POST 請(qǐng)求 /actuator/startup 端點(diǎn)返回監(jiān)控?cái)?shù)據(jù)

?> ~ curl -XPOST http://localhost:8080/actuator/startup                 11:49:51
{"springBootVersion":"2.4.0","timeline":{"startTime":"2020-12-04T01:38:15.028114Z","events":[{"startupStep":{"name":"spring.event.invoke-listener","id":296,"parentId":0,"tags":[{"key":"event","value":"ServletRequestHandledEvent: url=[/actuator/startup]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[dispatcherServlet]; session=[null]; user=[null]; time=[83ms]; status=[OK]"},{"key":"listener","value":"org.springframework.boot.context.config.DelegatingApplicationListener@2053d869"}]},"startTime":"2020-12-04T01:38:28.402870279Z","endTime":"2020-12-04T01:38:28.402929390Z","duration":"PT0.000059111S"}]}}

測(cè)試案例

  • 新增 RestTemplate Bean,并模擬初始化耗時(shí)

@Configuration(proxyBeanMethods = false)
public class DemoConfiguration {
    @Bean
    public RestTemplate restTemplate() throws InterruptedException {
        // 模擬初始化過程中的耗時(shí)操作
        Thread.sleep(5000);
        return new RestTemplate();
    }
}
  • 獲取端點(diǎn)日志, 準(zhǔn)確輸出在啟動(dòng)過程中初始化 RestTemplate 耗時(shí)情況

根據(jù)耗時(shí)排序

端點(diǎn)接口并未提供相關(guān)的接口,而是按照啟動(dòng)加載順序展示。沒有必要手動(dòng)處理獲取這些數(shù)據(jù)排序,可以通過 https://www./json/jsonsort/ 在線格式化排序

選擇按照耗時(shí)排序即可

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多