admin管理员组

文章数量:1130349

写在前面

SpringBoot很适合WEB应用的开发,该框架可以使用嵌入的TOMCAT/JETTY/UNDERTOW或者NETTY来创建self-contained HTTP server。大部分WEB应用使用spring-boot-starter-web来快速开发,也可以使用spring-boot-starter-webflux 来开发基于反应式的WEB应用。如果使用SpringBoot框架,但不是WEB应用呢?

场景描述

创建了一个基于SpringBoot框架的工程,但是在当前模块中并不需要对外提供REST服务,也不需要端口,针对这个场景,则在POM.XML中去掉了如下配置(如果这样的话,系统中没有常驻线程/监听的话,启动之后就会自动关闭),但是在该场景下启动依然会报错:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

错误描述

启动工程时报错(注意上面的场景描述):
Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port.

问题分析

由于引入了其他的模块,其他模块中将spring-boot-starter-web引入了,而当前模块中又未配置端口,所以启动时SPRINGBOOT会默认使用端口8080,而部署环境中其他项目已将8080占用,所以启动时系统报端口被占用。

当然模块本身需要端口,而端口又被占用时的处理方式与该场景不一样。

如何解决

SPRING BOOT采用不占端口的方式启动。

①. 方式一,通过API指定:
public class MyApplication {
   
   
    public static void main(String[] args) {
   
   
        new SpringApplicationBuilder(MyApplication.class).web(WebApplicationType.NONE).run(args);
        // SpringApplication.run(MyApplication.class, args);
        log.info("后端服务启动成功, 正在监听中...");
    }
}
②. 方式二,通过参数配置:

                    
                    

写在前面

SpringBoot很适合WEB应用的开发,该框架可以使用嵌入的TOMCAT/JETTY/UNDERTOW或者NETTY来创建self-contained HTTP server。大部分WEB应用使用spring-boot-starter-web来快速开发,也可以使用spring-boot-starter-webflux 来开发基于反应式的WEB应用。如果使用SpringBoot框架,但不是WEB应用呢?

场景描述

创建了一个基于SpringBoot框架的工程,但是在当前模块中并不需要对外提供REST服务,也不需要端口,针对这个场景,则在POM.XML中去掉了如下配置(如果这样的话,系统中没有常驻线程/监听的话,启动之后就会自动关闭),但是在该场景下启动依然会报错:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

错误描述

启动工程时报错(注意上面的场景描述):
Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port.

问题分析

由于引入了其他的模块,其他模块中将spring-boot-starter-web引入了,而当前模块中又未配置端口,所以启动时SPRINGBOOT会默认使用端口8080,而部署环境中其他项目已将8080占用,所以启动时系统报端口被占用。

当然模块本身需要端口,而端口又被占用时的处理方式与该场景不一样。

如何解决

SPRING BOOT采用不占端口的方式启动。

①. 方式一,通过API指定:
public class MyApplication {
   
   
    public static void main(String[] args) {
   
   
        new SpringApplicationBuilder(MyApplication.class).web(WebApplicationType.NONE).run(args);
        // SpringApplication.run(MyApplication.class, args);
        log.info("后端服务启动成功, 正在监听中...");
    }
}
②. 方式二,通过参数配置:

本文标签: 解决方案StopIdentifySpringBootprocess