AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

超贊!Spring Boot 3.3 自帶 Controller 接口監控,大家趕緊用起來

來源: 責編: 時間:2024-09-10 09:50:59 99觀看
導讀在現代應用開發中,系統的實時監控和維護變得至關重要。Spring Boot 3.3 的 Actuator 模塊為開發者提供了一整套強大的監控功能,使得應用程序的健康狀況、性能指標、用戶行為及安全性得以全方位掌握和管理。本文將詳細介

在現代應用開發中,系統的實時監控和維護變得至關重要。Spring Boot 3.3 的 Actuator 模塊為開發者提供了一整套強大的監控功能,使得應用程序的健康狀況、性能指標、用戶行為及安全性得以全方位掌握和管理。本文將詳細介紹如何配置和使用 Actuator,并展示如何將這些監控數據在前端頁面中可視化。xg428資訊網——每日最新資訊28at.com

Spring Boot 3.3 的 Actuator 模塊提供了一整套全面的監控功能,幫助開發者更好地管理和維護應用程序。主要功能包括:xg428資訊網——每日最新資訊28at.com

  1. 健康檢查:實時監控應用的健康狀態,快速發現和處理系統問題,確保應用穩定運行。
  2. 性能指標:監控應用的性能數據,如請求處理時間和響應時間,幫助識別和解決性能瓶頸。
  3. 環境屬性:展示應用的環境配置信息,為調試和環境管理提供支持。
  4. 請求追蹤:記錄和分析 HTTP 請求的詳細信息,幫助追蹤和定位問題源頭。
  5. 日志管理:動態調整日志級別,方便進行問題排查和監控。
  6. 線程轉儲:提供 JVM 線程的詳細轉儲信息,幫助分析線程狀態和優化性能。

這些功能使得開發者能夠實時獲取應用的運行狀態,優化系統性能,提升用戶體驗,并增強系統的安全性。接下來,我們將展示如何配置 Actuator 及其功能,并如何在前端頁面中展示這些監控數據。xg428資訊網——每日最新資訊28at.com

運行效果:xg428資訊網——每日最新資訊28at.com

圖片圖片xg428資訊網——每日最新資訊28at.com

若想獲取項目完整代碼以及其他文章的項目源碼,且在代碼編寫時遇到問題需要咨詢交流,歡迎加入下方的知識星球。xg428資訊網——每日最新資訊28at.com

項目依賴配置

首先,確保你的 pom.xml 文件中包含 Actuator 和 Thymeleaf 的依賴。xg428資訊網——每日最新資訊28at.com

<?xml versinotallow="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">	<modelVersion>4.0.0</modelVersion>	<parent>		<groupId>org.springframework.boot</groupId>		<artifactId>spring-boot-starter-parent</artifactId>		<version>3.3.3</version>		<relativePath/> <!-- lookup parent from repository -->	</parent>	<groupId>com.icoderoad</groupId>	<artifactId>actuator-demo</artifactId>	<version>0.0.1-SNAPSHOT</version>	<name>actuator-demo</name>	<description>Demo project for Spring Boot</description>		<properties>		<java.version>17</java.version>	</properties>	<dependencies>				<!-- Spring Boot Starter Web -->	    <dependency>	        <groupId>org.springframework.boot</groupId>	        <artifactId>spring-boot-starter-web</artifactId>	    </dependency>	    	    <!-- Spring Boot Actuator -->	    <dependency>	        <groupId>org.springframework.boot</groupId>	        <artifactId>spring-boot-starter-actuator</artifactId>	    </dependency>		    <!-- Thymeleaf for HTML templates -->	    <dependency>	        <groupId>org.springframework.boot</groupId>	        <artifactId>spring-boot-starter-thymeleaf</artifactId>	    </dependency>		<dependency>			<groupId>org.springframework.boot</groupId>			<artifactId>spring-boot-starter-test</artifactId>			<scope>test</scope>		</dependency>	</dependencies>	<build>		<plugins>			<plugin>				<groupId>org.springframework.boot</groupId>				<artifactId>spring-boot-maven-plugin</artifactId>			</plugin>		</plugins>	</build></project>

配置 Actuator 監控功能

在 application.yml 中配置 Actuator 端點,啟用所需的監控功能。xg428資訊網——每日最新資訊28at.com

server:  port: 8080    management:  endpoints:    web:      exposure:        include: health, metrics, info, mappings, env, conditions, loggers, threaddump  endpoint:    health:      show-details: always    metrics:      enable:        all: true    info:      enabled: true    mappings:      enabled: true    env:      enabled: true    conditions:      enabled: true    loggers:      enabled: true    threaddump:      enabled: true  trace:    http:      enabled: true

創建 Actuator 監控 Controller

創建一個 MonitoringController,用于展示 Actuator 的監控數據。xg428資訊網——每日最新資訊28at.com

package com.icoderoad.actuator_demo.controller;import org.springframework.boot.actuate.env.EnvironmentEndpoint;import org.springframework.boot.actuate.health.HealthEndpoint;import org.springframework.boot.actuate.info.InfoEndpoint;import org.springframework.boot.actuate.logging.LoggersEndpoint;import org.springframework.boot.actuate.management.ThreadDumpEndpoint;import org.springframework.boot.actuate.metrics.MetricsEndpoint;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;@RestController@RequestMapping("/monitoring")public class MonitoringController {    private final HealthEndpoint healthEndpoint;    private final MetricsEndpoint metricsEndpoint;    private final InfoEndpoint infoEndpoint;    private final EnvironmentEndpoint environmentEndpoint;    private final LoggersEndpoint loggersEndpoint;    private final ThreadDumpEndpoint threadDumpEndpoint;    public MonitoringController(HealthEndpoint healthEndpoint, MetricsEndpoint metricsEndpoint,                                InfoEndpoint infoEndpoint, EnvironmentEndpoint environmentEndpoint,                                LoggersEndpoint loggersEndpoint, ThreadDumpEndpoint threadDumpEndpoint) {        this.healthEndpoint = healthEndpoint;        this.metricsEndpoint = metricsEndpoint;        this.infoEndpoint = infoEndpoint;        this.environmentEndpoint = environmentEndpoint;        this.loggersEndpoint = loggersEndpoint;        this.threadDumpEndpoint = threadDumpEndpoint;    }    @GetMapping    public Map<String, Object> getMonitoringData() {        Map<String, Object> monitoringData = new HashMap<>();                monitoringData.put("health", healthEndpoint.health());        monitoringData.put("metrics", metricsEndpoint.listNames());        monitoringData.put("info", infoEndpoint.info());        monitoringData.put("environment", environmentEndpoint.environment(null)); // 返回環境屬性描述符        monitoringData.put("loggers", loggersEndpoint.loggers());        monitoringData.put("threaddump", threadDumpEndpoint.threadDump());        return monitoringData;    }}

創建用戶菜單接口

創建一個新的接口 /menu,用于提供用戶菜單數據。xg428資訊網——每日最新資訊28at.com

package com.icoderoad.actuator_demo.controller;import java.util.List;import java.util.Map;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/api")public class UserController {    @GetMapping("/menu")    public List<Map<String, String>> getMenu() {        // 模擬返回用戶菜單        return List.of(            Map.of("name", "首頁", "url", "/home"),            Map.of("name", "用戶管理", "url", "/users"),            Map.of("name", "設置", "url", "/settings")        );    }}

視圖控制器

package com.icoderoad.actuator_demo.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;@Controllerpublic class IndexController {    @GetMapping("/")    public String index() {        return "index";    }}

創建 Thymeleaf 模板

在 src/main/resources/templates 目錄下創建或更新 monitoring.html 文件,以展示 Actuator 監控數據和測試接口結果。xg428資訊網——每日最新資訊28at.com

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>接口監控</title>    <link rel="stylesheet" >    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>    <style>    	.container {    		width: 40%;    		margin: 40px auto;    	}        pre {            white-space: pre-wrap; /* Allow text to wrap within the element */            word-wrap: break-word; /* Break long words */        }    </style></head><body>    <div class="container mt-5">        <h1>接口監控</h1>                <!-- 接口健康狀態 -->        <div class="mt-3">            <h3>接口健康狀態</h3>            <button class="btn btn-info" onclick="toggleContent('health', fetchHealth)">顯示健康狀態</button>            <pre id="health" style="display: none;"></pre>        </div>                <!-- 可用指標 -->        <div class="mt-3">            <h3>可用指標</h3>            <button class="btn btn-info" onclick="toggleContent('metrics', fetchMetrics)">顯示可用指標</button>            <pre id="metrics" style="display: none;"></pre>        </div>                <!-- 環境屬性 -->        <div class="mt-3">            <h3>環境屬性</h3>            <button class="btn btn-info" onclick="toggleContent('environment', fetchEnvironment)">顯示環境屬性</button>            <pre id="environment" style="display: none;"></pre>        </div>                <!-- 日志級別 -->        <div class="mt-3">            <h3>日志級別</h3>            <button class="btn btn-info" onclick="toggleContent('loggers', fetchLoggers)">顯示日志級別</button>            <pre id="loggers" style="display: none;"></pre>        </div>                <!-- 線程轉儲 -->        <div class="mt-3">            <h3>線程轉儲</h3>            <button class="btn btn-info" onclick="toggleContent('threaddump', fetchThreadDump)">顯示線程轉儲</button>            <pre id="threaddump" style="display: none;"></pre>        </div>        <!-- 用戶菜單 -->        <div class="mt-3">            <h3>用戶菜單</h3>            <a href="#" class="btn btn-primary" onclick="fetchUserMenu()">訪問用戶菜單接口</a>            <div id="menu-result" class="mt-2"></div>        </div>    </div>    <script>        function fetchMonitoringData() {            fetch('/monitoring')                .then(response => response.json())                .then(data => {                    // Store the data in the global scope for later use                    window.monitoringData = data;                })                .catch(error => console.error('Error fetching monitoring data:', error));        }        function fetchHealth() {            if (!window.monitoringData) {                fetchMonitoringData();                return;            }            return JSON.stringify(window.monitoringData.health, null, 2);        }        function fetchMetrics() {            if (!window.monitoringData) {                fetchMonitoringData();                return;            }            return JSON.stringify(window.monitoringData.metrics, null, 2);        }        function fetchEnvironment() {            if (!window.monitoringData) {                fetchMonitoringData();                return;            }            return JSON.stringify(window.monitoringData.environment, null, 2);        }        function fetchLoggers() {            if (!window.monitoringData) {                fetchMonitoringData();                return;            }            return JSON.stringify(window.monitoringData.loggers, null, 2);        }        function fetchThreadDump() {            if (!window.monitoringData) {                fetchMonitoringData();                return;            }            return JSON.stringify(window.monitoringData.threaddump, null, 2);        }        function fetchUserMenu() {            fetch('/api/menu')                .then(response => response.json())                .then(data => {                    document.getElementById('menu-result').textContent = JSON.stringify(data, null, 2);                })                .catch(error => console.error('Error fetching user menu:', error));        }        function toggleContent(id, fetchFunction) {            const content = document.getElementById(id);            const button = event.target;                        if (content.style.display === 'none') {                // Display content                content.textContent = fetchFunction();                content.style.display = 'block';                button.textContent = `隱藏${button.textContent.substring(2)}`;            } else {                // Hide content                content.style.display = 'none';                button.textContent = `顯示${button.textContent.substring(2)}`;            }        }        // Trigger fetchMonitoringData on page load        window.onload = function() {            fetchMonitoringData();        }    </script></body></html>

運行并測試

  1. 啟動 Spring Boot 應用程序。
  2. 訪問 http://localhost:8080 頁面,查看 Actuator 提供的監控數據。
  3. 點擊“訪問用戶菜單接口”按鈕,查看用戶菜單接口的響應數據。

通過以上步驟,你可以輕松驗證 Actuator 的 HTTP 請求追蹤功能,并在頁面上測試用戶菜單接口。這樣不僅能夠展示 Actuator 的強大功能,還可以檢查和調試接口的響應數據。這篇文章希望能幫助大家有效地實現和利用 Spring Boot 的監控功能,并提升系統的管理和維護能力。xg428資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-112780-0.html超贊!Spring Boot 3.3 自帶 Controller 接口監控,大家趕緊用起來

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: PHP異步非阻塞MySQL客戶端連接池

下一篇: 如何使用 CGLIB 在 Spring Boot 3.3 中實現動態代理

標簽:
  • 熱門焦點
  • 俄羅斯:將審查iPhone等外國公司設備 保數據安全

    iPhone和特斯拉都屬于在各自領域領頭羊的品牌,推出的產品也也都是數一數二的,但對于一些國家而言,它們的產品可靠性和安全性還是在限制范圍內。近日,俄羅斯聯邦通信、信息技術
  • K8S | Service服務發現

    一、背景在微服務架構中,這里以開發環境「Dev」為基礎來描述,在K8S集群中通常會開放:路由網關、注冊中心、配置中心等相關服務,可以被集群外部訪問;圖片對于測試「Tes」環境或者
  • 如何通過Python線程池實現異步編程?

    線程池的概念和基本原理線程池是一種并發處理機制,它可以在程序啟動時創建一組線程,并將它們置于等待任務的狀態。當任務到達時,線程池中的某個線程會被喚醒并執行任務,執行完任
  • 一文掌握 Golang 模糊測試(Fuzz Testing)

    模糊測試(Fuzz Testing)模糊測試(Fuzz Testing)是通過向目標系統提供非預期的輸入并監視異常結果來發現軟件漏洞的方法。可以用來發現應用程序、操作系統和網絡協議等中的漏洞或
  • WebRTC.Net庫開發進階,教你實現屏幕共享和多路復用!

    WebRTC.Net庫:讓你的應用更親民友好,實現視頻通話無痛接入! 除了基本用法外,還有一些進階用法可以更好地利用該庫。自定義 STUN/TURN 服務器配置WebRTC.Net 默認使用 Google 的
  • 猿輔導與新東方的兩種“歸途”

    作者|卓心月 出品|零態LT(ID:LingTai_LT)如何成為一家偉大企業?答案一定是對&ldquo;勢&rdquo;的把握,這其中最關鍵的當屬對企業戰略的制定,且能夠站在未來看現在,即使這其中的
  • 品牌洞察丨服務本地,美團直播成效幾何?

    來源:17PR7月11日,美團App首頁推薦位出現&ldquo;美團直播&rdquo;的固定入口。在直播聚合頁面,外賣&ldquo;神槍手&rdquo;直播間、美團旅行直播間、美團買菜直播間等均已上線,同時
  • 自研Exynos回歸!三星Galaxy S24系列將提供Exynos和驍龍雙版本

    年初,全新的三星Galaxy S23系列發布,包含Galaxy S23、Galaxy S23+和Galaxy S23 Ultra三個版本,全系搭載超頻版驍龍8 Gen 2,雖同樣采用臺積電4nm工藝制
  • 北京:科技教育體驗基地開始登記

      北京“科技館之城”科技教育體驗基地登記和認證工作日前啟動。首批北京科技教育體驗基地擬于2023年全國科普日期間掛牌,后續還將開展常態化登記。  北京科技教育體驗基
Top