最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java后端本地調(diào)試實(shí)用方法總結(jié)大全

 更新時(shí)間:2026年01月14日 09:44:46   作者:廋到被風(fēng)吹走  
這篇文章主要介紹了Java后端本地調(diào)試實(shí)用方法總結(jié)大全的相關(guān)資料,每個(gè)部分都提供了實(shí)用的技巧和工具推薦,幫助開發(fā)者提高調(diào)試效率,文中介紹的非常詳細(xì),需要的朋友可以參考下

一、IDE 調(diào)試基礎(chǔ)技巧

1.IDEA 斷點(diǎn)調(diào)試(必備)

核心功能

  • 行斷點(diǎn):單擊 gutter 設(shè)置斷點(diǎn)
  • 條件斷點(diǎn):右鍵斷點(diǎn)設(shè)置條件表達(dá)式(如 userId != null && userId > 100
  • 日志斷點(diǎn):不暫停程序,僅打印日志(右鍵 Suspend 取消勾選)
  • 異常斷點(diǎn):Debug 窗口 → View Breakpoints → + Java Exception Breakpoints

實(shí)用技巧

  • Alt + F8:Evaluate Expression 動(dòng)態(tài)執(zhí)行代碼
  • Ctrl + F8:快速切換行斷點(diǎn)
  • Ctrl + Shift + F8:管理所有斷點(diǎn)
  • Drop Frame:回退到方法調(diào)用前(堆棧幀),重新執(zhí)行

二、日志與監(jiān)控

2.SLF4J + Logback 動(dòng)態(tài)日志級別

場景:生產(chǎn)環(huán)境無法重啟,需動(dòng)態(tài)調(diào)整日志排查問題。

配置

# application.yml
logging:
  level:
    root: INFO
    com.example.service: DEBUG
    com.example.mapper: TRACE

動(dòng)態(tài)修改(無需重啟):

// 通過 Actuator 端點(diǎn)
@RestController
public class LogController {
    @Autowired
    private LoggingSystem loggingSystem;
    
    @PostMapping("/log/level")
    public void setLogLevel(@RequestParam String logger, @RequestParam String level) {
        loggingSystem.setLogLevel(logger, LogLevel.valueOf(level));
    }
}

訪問:POST /actuator/loggers/com.example.service + Body {"configuredLevel": "DEBUG"}

3.Spring Boot Actuator 監(jiān)控

關(guān)鍵端點(diǎn)

management:
  endpoints:
    web:
      exposure:
        include: health,info,beans,conditions,env,metrics,mappings,loggers

常用調(diào)試場景

  • /actuator/beans:查看 Bean 是否加載
  • /actuator/conditions:查看自動(dòng)配置生效情況
  • /actuator/env:檢查配置文件加載值
  • /actuator/mappings:驗(yàn)證 Controller 映射
  • /actuator/metrics:JVM 內(nèi)存、線程、HTTP 請求統(tǒng)計(jì)

三、本地環(huán)境模擬

4.Docker Compose 一鍵模擬中間件

docker-compose.yml 示例

version: '3.8'
services:
  mysql:
    image: mysql:8.0
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test
    volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
  
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
  
  kafka:
    image: confluentinc/cp-kafka:latest
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
  
  nacos:
    image: nacos/nacos-server:v2.2.3
    ports:
      - "8848:8848"
    environment:
      MODE: standalone

啟動(dòng)命令docker-compose up -d

優(yōu)勢:團(tuán)隊(duì)協(xié)作環(huán)境一致,CI/CD 可直接復(fù)用。

5.內(nèi)存數(shù)據(jù)庫快速測試

場景:單元測試無需連接真實(shí)數(shù)據(jù)庫。

// 測試類配置
@SpringBootTest
@TestPropertySource(locations = "classpath:application-test.yml")
public class UserServiceTest {
    // 使用 H2 內(nèi)存數(shù)據(jù)庫
}

// application-test.yml
spring:
  datasource:
    url: jdbc:h2:mem:testdb
    driver-class-name: org.h2.Driver
  sql:
    init:
      mode: always
      schema-locations: classpath:schema-h2.sql

四、代碼增強(qiáng)與熱部署

6.Spring Boot DevTools 熱部署

配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

特性

  • 自動(dòng)重啟:classpath 文件變動(dòng)觸發(fā)應(yīng)用重啟(比冷啟動(dòng)快 80%)
  • LiveReload:瀏覽器/前端資源自動(dòng)刷新
  • 遠(yuǎn)程調(diào)試:支持遠(yuǎn)程應(yīng)用熱更新

7.JRebel 商業(yè)熱部署(終極方案)

優(yōu)勢真正的熱交換,無需重啟,支持類結(jié)構(gòu)修改(增刪方法、字段)。

使用步驟

  1. 安裝 IDEA 插件
  2. 激活許可證
  3. 啟動(dòng)時(shí)選擇 JRebel Debug
  4. 修改代碼后 Ctrl + Shift + F9 編譯當(dāng)前文件即時(shí)生效

五、單元測試與 Mock

8.Spring Boot Test 最佳實(shí)踐

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class UserControllerTest {
    @Autowired
    private MockMvc mockMvc;
    
    @MockBean
    private UserService userService; // 自動(dòng)注入 Mock 對象
    
    @Test
    void testGetUser() throws Exception {
        // 打樁
        when(userService.getUser(1L)).thenReturn(new User("test"));
        
        mockMvc.perform(get("/api/users/{id}", 1L))
               .andExpect(status().isOk())
               .andExpect(jsonPath("$.name").value("test"));
        
        verify(userService, times(1)).getUser(1L);
    }
}

9.Mock 中間件(Embedded)

@TestConfiguration
public class TestRedisConfig {
    @Bean
    public RedisServer redisServer() {
        return RedisServer.newRedisServer().start(); // 嵌入式 Redis
    }
}

支持組件

  • Embedded Rediscom.github.kstyrc:embedded-redis
  • Embedded Kafkaspring-kafka-test
  • Embedded MongoDBde.flapdoodle.embed:de.flapdoodle.embed.mongo

六、高級調(diào)試技巧

10.遠(yuǎn)程調(diào)試(Remote Debug)

場景:調(diào)試部署在測試環(huán)境/容器中的應(yīng)用。

啟動(dòng) JVM 參數(shù)

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar app.jar

IDEA 配置

  1. Run → Edit Configurations → + Remote JVM Debug
  2. Host: 測試服務(wù)器IP Port: 5005
  3. 點(diǎn)擊 Debug 圖標(biāo)連接

Docker 中開啟

FROM openjdk:17
EXPOSE 5005
CMD ["java", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005", "-jar", "app.jar"]

11.JMX 監(jiān)控與 JVisualVM

啟動(dòng)參數(shù)

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

JVisualVM 連接jvisualvm → 右鍵 Local → JMX Connection → localhost:9999

功能

  • 查看堆內(nèi)存、線程、類加載
  • Sampler:CPU/內(nèi)存采樣分析
  • Profiler:性能剖析
  • MBeans:查看 Spring Beans 狀態(tài)

12.Arthas 線上診斷神器

場景:生產(chǎn)環(huán)境無法遠(yuǎn)程調(diào)試,需動(dòng)態(tài)診斷。

安裝使用

curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar # 選擇 Java 進(jìn)程

核心命令

  • watch:查看方法入?yún)?返回值
    watch com.example.service.UserService getUser '{params, returnObj}' -x 2
    
  • trace:方法內(nèi)部調(diào)用耗時(shí)追蹤
    trace com.example.service.UserService getUser -n 5 --skipJDKMethod false
    
  • jad:反編譯類查看源碼
    jad com.example.service.UserService
    
  • ognl:執(zhí)行 SpEL 表達(dá)式(查看/修改字段)
    ognl '@com.example.config.GlobalConfig@STATIC_FIELD'
    
  • dashboard:實(shí)時(shí)系統(tǒng)監(jiān)控

七、多線程與異步調(diào)試

13.線程 Dump 分析

命令行

# 查看 Java 進(jìn)程
jps

# 生成線程 Dump
jstack <pid> > thread.dump

在線分析:https://fastthread.io/ 或 https://jstack.review/

關(guān)鍵狀態(tài)

  • BLOCKED:等待鎖,需定位死鎖
  • WAITING:等待條件喚醒
  • TIMED_WAITING:Sleep 或定時(shí)等待

14.異步任務(wù)調(diào)試

@Service
public class AsyncService {
    @Async
    public CompletableFuture<User> asyncGetUser(Long id) {
        // 斷點(diǎn)會命中,但線程名是 task-1/task-2
        return CompletableFuture.completedFuture(userMapper.selectById(id));
    }
}

IDEA 調(diào)試技巧

  • All:查看所有線程堆棧
  • Thread:僅查看當(dāng)前線程(避免斷點(diǎn)干擾其他線程)
  • Mute Breakpoints:臨時(shí)禁用斷點(diǎn),讓程序運(yùn)行到特定位置

八、網(wǎng)絡(luò)與 HTTP 調(diào)試

15.MockServer 模擬外部 API

@BeforeEach
void setUp() {
    mockServer = ClientAndServer.startClientAndServer(1080);
    
    // 配置 Mock 響應(yīng)
    mockServer.when(
        request()
            .withMethod("POST")
            .withPath("/api/payment")
    ).respond(
        response()
            .withStatusCode(200)
            .withBody("{\"status\":\"success\"}")
    );
}

16.Wireshark/TCPDump 抓包分析

# 抓取 8080 端口的 HTTP 請求
sudo tcpdump -i any -w capture.pcap port 8080

# 用 Wireshark 打開分析
wireshark capture.pcap

九、專項(xiàng)調(diào)試工具

17.內(nèi)存溢出(OOM)分析

啟動(dòng)參數(shù)保留堆 Dump

-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/path/to/dump.hprof
-XX:+PrintGCDetails
-XX:+PrintGCDateStamps
-Xloggc:/path/to/gc.log

分析工具

  • Eclipse MAT:分析堆 Dump,定位內(nèi)存泄漏
  • jhat:JDK 自帶輕量分析
    jhat dump.hprof
    

18.SQL 調(diào)試

MyBatis 日志

logging:
  level:
    com.example.mapper: DEBUG # 打印 SQL 和參數(shù)

p6spy 監(jiān)控

<dependency>
    <groupId>p6spy</groupId>
    <artifactId>p6spy</artifactId>
</dependency>

自動(dòng)打印 SQL 執(zhí)行時(shí)間,識別慢查詢。

十、調(diào)試效率提升技巧

19.IDEA 書簽與斷點(diǎn)技巧

  • F11:匿名書簽
  • Ctrl + F11:帶數(shù)字的書簽(1-9)
  • Ctrl + 1-9:跳轉(zhuǎn)到數(shù)字書簽
  • 斷點(diǎn)分組:右鍵斷點(diǎn) → Move to group,管理復(fù)雜場景

20.Spring Boot 調(diào)試模式

# 開啟 DEBUG 日志
java -jar app.jar --debug

# 查看自動(dòng)配置報(bào)告
java -jar app.jar --debug > auto-config-report.txt

# 啟動(dòng)時(shí)打印 Bean 創(chuàng)建過程
-Dlogging.level.org.springframework.beans.factory=DEBUG

總結(jié):調(diào)試方法論

問題類型首選工具備選方案
業(yè)務(wù)邏輯錯(cuò)誤IDEA 斷點(diǎn)調(diào)試日志 + 單元測試
環(huán)境問題Docker ComposeEmbedded 組件
性能問題JVisualVM/ArthasJProfiler(商業(yè))
生產(chǎn)問題Arthas遠(yuǎn)程 Debug
并發(fā)問題jstack + 線程分析JMC(Java Mission Control)
內(nèi)存泄漏Eclipse MATJProfiler

黃金法則先在單元測試中復(fù)現(xiàn),再用斷點(diǎn)/日志定位,最后工具分析。調(diào)試是系統(tǒng)性工程,結(jié)合多種手段才能事半功倍。

到此這篇關(guān)于Java后端本地調(diào)試實(shí)用方法總結(jié)大全的文章就介紹到這了,更多相關(guān)Java后端本地調(diào)試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Dubbo新版本zk注冊中心連接問題及解決

    Dubbo新版本zk注冊中心連接問題及解決

    這篇文章主要介紹了Dubbo新版本zk注冊中心連接問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • springboot結(jié)合maven實(shí)現(xiàn)多模塊打包

    springboot結(jié)合maven實(shí)現(xiàn)多模塊打包

    本文主要介紹了springboot借助maven完成多模塊打包,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 詳解AngularJs與SpringMVC簡單結(jié)合使用

    詳解AngularJs與SpringMVC簡單結(jié)合使用

    本篇文章主要介紹了AngularJs與SpringMVC簡單結(jié)合使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • Windows下后端如何啟動(dòng)SpringBoot的Jar項(xiàng)目

    Windows下后端如何啟動(dòng)SpringBoot的Jar項(xiàng)目

    這篇文章主要介紹了Windows下后端如何啟動(dòng)SpringBoot的Jar項(xiàng)目問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • SpringBoot無法連接redis的解決方案

    SpringBoot無法連接redis的解決方案

    這篇文章主要介紹了SpringBoot無法連接redis的解決方案,文中通過代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-08-08
  • Java日志組件間關(guān)系詳解

    Java日志組件間關(guān)系詳解

    在本文里我們給大家整理了關(guān)于Java日志組件間關(guān)系相關(guān)基礎(chǔ)知識,需要的朋友們跟著學(xué)習(xí)下。
    2019-02-02
  • springboot實(shí)現(xiàn)返回視圖而不是string的方法

    springboot實(shí)現(xiàn)返回視圖而不是string的方法

    這篇文章主要介紹了springboot實(shí)現(xiàn)返回視圖而不是string的方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java 生成隨機(jī)字符串?dāng)?shù)組的實(shí)例詳解

    Java 生成隨機(jī)字符串?dāng)?shù)組的實(shí)例詳解

    這篇文章主要介紹了Java 生成隨機(jī)字符串?dāng)?shù)組的實(shí)例詳解的相關(guān)資料,主要是利用Collections.sort()方法對泛型為String的List 進(jìn)行排序,需要的朋友可以參考下
    2017-08-08
  • 簡談java并發(fā)FutureTask的實(shí)現(xiàn)

    簡談java并發(fā)FutureTask的實(shí)現(xiàn)

    這篇文章主要介紹了簡談java并發(fā)FutureTask的實(shí)現(xiàn),FutureTask都是用于獲取線程執(zhí)行的返回結(jié)果。文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下
    2019-06-06
  • Java 測試URL地址是否能正常連接的代碼

    Java 測試URL地址是否能正常連接的代碼

    本文給大家分享兩段代碼分別是java測試URL地址是否能正常連接和Java檢測URL是否可用或者可打開的代碼,代碼都很簡單,有需要的朋友可以參考下
    2016-08-08

最新評論

皮山县| 顺义区| 闽侯县| 治县。| 山西省| 仙桃市| 武鸣县| 乐至县| 禄丰县| 营口市| 温泉县| 天水市| 修武县| 宿迁市| 高邮市| 凤阳县| 宝清县| 郎溪县| 鄂托克旗| 高平市| 清新县| 鄱阳县| 利辛县| 和田市| 琼结县| 武冈市| 万宁市| 定远县| 和静县| 噶尔县| 黔西| 凯里市| 光泽县| 花莲县| 松滋市| 贡山| 将乐县| 新晃| 临汾市| 莱阳市| 枣阳市|