远程调试方法
1.編寫Spring工程案例
1.1.準(zhǔn)備SpringBoot工程
pom.xml定義:
<?xml version="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>2.3.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><releases><enabled>false</enabled></releases></repository></repositories><pluginRepositories><pluginRepository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></pluginRepository><pluginRepository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><releases><enabled>false</enabled></releases></pluginRepository></pluginRepositories></project>1.2.springBoot啟動(dòng)類代碼
package com.example.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}1.3.案例demo
package com.example.demo;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RequestMapping("/") @RestController public class HelloController {private static final Logger logger = LoggerFactory.getLogger(HelloController.class);@RequestMapping("/hello")public String hello() {logger.info("111");logger.info("222");logger.info("333");logger.info("444");return "Hello Spring Boot!";}}1.4.application.properties的內(nèi)容如下
server.port=8060 server.tomcat.uri-encoding=utf-8注意:
1、容器內(nèi)部使用http://localhost:8060/hello即可訪問到接口。
2、由于筆者使用的是docker容器部署的,宿主機(jī)上的18060和容器內(nèi)部的8060端口映射,所以在瀏覽器上可以使用:http://localhost:18060/hello進(jìn)行訪問。
啟動(dòng)之后的效果如下:
2.打包部署
最終打的包是:demo-0.0.1-SNAPSHOT.jar,假設(shè)放在docker容器里面的/root/workspace位置。如下圖:
2.1.編寫start.sh
內(nèi)容如下:
cd /root/workspacesource /etc/profile nohup java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5000 demo-0.0.1-SNAPSHOT.jar > demo.log 2>&1 &注意,主要添加:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5000其中5000就是實(shí)際用于調(diào)試用的端口(docker宿主機(jī)上的15000和容器內(nèi)部的5000映射,具體命令略)。
2.2.訪問驗(yàn)證
啟動(dòng)之后,在本地的瀏覽器中訪問:http://localhost:18060/hello,效果圖如下:
3.在idea中配置遠(yuǎn)程調(diào)試端口
3.1.遠(yuǎn)程配置
遠(yuǎn)程端口配置方式:
3.2.打斷點(diǎn)
3.3.訪問頁面
3.4.看idea
總結(jié)
- 上一篇: 医美贷不还会怎么样
- 下一篇: Hyper-V 网络设置 虚拟机固定Ip