SpringBoot - Identify and stop the process that‘s listening on port 8080解决方案

写在前面

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("后端服务启动成功, 正在监听中...");
    }
}
②. 方式二,通过参数配置:
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("后端服务启动成功, 正在监听中...");
    }
}
# SPRING相关的配置
spring:
  main:
    web-application-type: none
  application:
    # 应用名称
    name: my-test-service

启动方式

// new SpringApplicationBuilder(MyApplication.class).web(WebApplicationType.NONE).run(args);
WebApplicationType的类型
类型描述备注
NONE非WEB应用程序应用程序以非WEB应用程序的方式运行,当然不会启动嵌入式的WEB容器
SERVLET基于SERVLET的WEB应用应用基于SERVLET的WEB应用程序的方式运行,同时启动嵌入式的SERVLET的WEB容器,如TOMCAT/JETTY/UNDERTOW等
REACTIVE基于反应式的WEB应用应用基于反应式的WEB应用程序的方式运行,同时启动嵌入式的反应式的WEB容器,如NETTY等
public enum WebApplicationType {
    NONE,     // 非WEB应用程序
    SERVLET,  // 基于SERVLET的WEB应用
    REACTIVE; // 基于反应式的WEB应用

    private static final String[] SERVLET_INDICATOR_CLASSES = new String[]{"javax.servlet.Servlet", "org.springframework.web.context.ConfigurableWebApplicationContext"};
    private static final String WEBMVC_INDICATOR_CLASS = "org.springframework.web.servlet.DispatcherServlet";
    private static final String WEBFLUX_INDICATOR_CLASS = "org.springframework.web.reactive.DispatcherHandler";
    private static final String JERSEY_INDICATOR_CLASS = "org.glassfish.jersey.servlet.ServletContainer";
    private static final String SERVLET_APPLICATION_CONTEXT_CLASS = "org.springframework.web.context.WebApplicationContext";
    private static final String REACTIVE_APPLICATION_CONTEXT_CLASS = "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext";

    private WebApplicationType() {
    }

    static WebApplicationType deduceFromClasspath() {
        if (ClassUtils.isPresent("org.springframework.web.reactive.DispatcherHandler", (ClassLoader)null) && !ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet", (ClassLoader)null) && !ClassUtils.isPresent("org.glassfish.jersey.servlet.ServletContainer", (ClassLoader)null)) {
            return REACTIVE;
        } else {
            String[] var0 = SERVLET_INDICATOR_CLASSES;
            int var1 = var0.length;

            for(int var2 = 0; var2 < var1; ++var2) {
                String className = var0[var2];
                if (!ClassUtils.isPresent(className, (ClassLoader)null)) {
                    return NONE;
                }
            }

            return SERVLET;
        }
    }

    static WebApplicationType deduceFromApplicationContext(Class<?> applicationContextClass) {
        if (isAssignable("org.springframework.web.context.WebApplicationContext", applicationContextClass)) {
            return SERVLET;
        } else {
            return isAssignable("org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext", applicationContextClass) ? REACTIVE : NONE;
        }
    }

    private static boolean isAssignable(String target, Class<?> type) {
        try {
            return ClassUtils.resolveClassName(target, (ClassLoader)null).isAssignableFrom(type);
        } catch (Throwable var3) {
            return false;
        }
    }
}

在当前的工程中不引入spring-boot-starter-web可以吗?

如果无需REST服务,无需端口,则在POM.XML中去掉了如下配置即可:

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

那为什么又有WEB容器(默认端口8080)的启动呢?仔细分析了一下POM.XML文件,发现引入的其他模块中添加了spring-boot-starter-web组件导致的该问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cloneme01

谢谢您的支持与鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值