1. 创建项目

创建一个最基本的spring boot 项目,远程debug方式启动

1. 创建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.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <packaging>jar</packaging>
    <groupId>com.eleven</groupId>
    <artifactId>eleven_test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eleven_test</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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.eleven.ElevenTestApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2. 创建启动类

package com.eleven;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ElevenTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(ElevenTestApplication.class, args);
    }

}

3. 创建返回实体类

  • Result.java
package com.eleven.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author zhaojinhui
 * @date 2021/7/6 14:04
 * @apiNote
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {

    private Integer code;

    private Object data;

    private String message;
}
  • ResultFactory.java
package com.eleven.common;


/**
 * @author zhaojinhui
 * @date 2021/7/6 14:06
 * @apiNote
 */
public class ResultFactory {
    private static Integer FAIL = 500;
    private static Integer SUCCESS = 200;

    public static Result buildSuccessResult(Object data){
        return new Result(SUCCESS,data,null);
    }

    public static Result buildFailureResult(String message){
        return new Result(FAIL,null,message);
    }

    public static Result buildNewResult(Integer code,Object data,String message){
        return new Result(code,data,message);
    }
}

4. 创建Controller

package com.eleven.controller;

import com.eleven.common.Result;
import com.eleven.common.ResultFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhaojinhui
 * @date 2021/7/6 12:08
 * @apiNote
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/sum")
    public Result test(){
        int i = 0;
        int j = 1;
        int sum = i+ j;
        System.out.println(sum);
        return ResultFactory.buildSuccessResult(sum);
    }
}

5. 打包

mvn package;

生成的包路径为

2. 上传服务器并启动

  1. 将jar包上传至服务器

使用xftp或者是sftp工具上传远程服务器

或者使用scp命令上传到远程服务器

cd target scp eleven_test-0.0.1-SNAPSHOT.jar root@192.168.153.130:/opt

  1. 启动远程服务器的jar包

使用xshell等工具连接远程服务器。

windows 自带的cmd或者是git也都是可以的。命令如下

ssh root@192.168.153.130

启动服务器jar包

java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5005,suspend=n -jar eleven_test-0.0.1-SNAPSHOT.jar

3. 开启远程服务器的防火墙

如果是阿里云服务器的话,需要放开5005端口(刚才配置的端口)
修改iptables的规则

# 编辑iptables vim /etc/sysconfig/iptables # 添加下列内容 -A INPUT -m state --state NEW -m tcp -p tcp --dport 5005 -j ACCEPT # 保存更改 #service iptables save #重启iptables service iptables save && service iptables restart

4. IDEA配置

  1. 点击Edit configuration
  2. spring项目IDEA配置远程DEBUG_maven

  3. 点击+ 然后选择remote
  4. spring项目IDEA配置远程DEBUG_maven_02

  5. 填写主机和端口号, 端口号是刚才启动的时候指定的
  6. spring项目IDEA配置远程DEBUG_spring_03

  7. IDEA 启动远程debug
    点击绿色的小虫子,正常打断点。访问远程项目就能进到本地IDEA的断点了,提示下边的信息就是成功了
  8. spring项目IDEA配置远程DEBUG_maven_04

浏览器访问接口就能到本地调试了

spring项目IDEA配置远程DEBUG_maven_05