1.编写Spring工程案例
1.1.准备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启动类代码
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的内容如下
server.port=8060
server.tomcat.uri-encoding=utf-8
注意:
1、容器内部使用http://localhost:8060/hello即可访问到接口。
2、由于笔者使用的是docker容器部署的,宿主机上的18060和容器内部的8060端口映射,所以在浏览器上可以使用:http://localhost:18060/hello进行访问。
启动之后的效果如下:
2.打包部署
最终打的包是:demo-0.0.1-SNAPSHOT.jar,假设放在docker容器里面的/root/workspace位置。如下图:
2.1.编写start.sh
内容如下:
cd /root/workspace
source /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就是实际用于调试用的端口(docker宿主机上的15000和容器内部的5000映射,具体命令略)。
2.2.访问验证
启动之后,在本地的浏览器中访问:http://localhost:18060/hello,效果图如下:
3.在idea中配置远程调试端口
3.1.远程配置
远程端口配置方式: