• 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>
    
    • 1
    • 2
    • 3
    • 4

    错误描述

    启动工程时报错(注意上面的场景描述):
    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("后端服务启动成功, 正在监听中...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    ②. 方式二,通过参数配置:
    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("后端服务启动成功, 正在监听中...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # SPRING相关的配置
    spring:
      main:
        web-application-type: none
      application:
        # 应用名称
        name: my-test-service
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    启动方式

    // new SpringApplicationBuilder(MyApplication.class).web(WebApplicationType.NONE).run(args);
    
    • 1
    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;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

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

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4

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

  • 相关阅读:
    NLP的数据增强技术总结
    Springboot毕设项目办公物资管理系统6304w(java+VUE+Mybatis+Maven+Mysql)
    Kafka系列之:详细介绍部署Kafka Connect分布式集群
    概率深度学习建模数据不确定性
    基于蜉蝣优化的BP神经网络(分类应用) - 附代码
    LeetCode 0263. 丑数
    蓝桥每日一题 (差分)3月3号
    学习-Java数组之foreach遍历数组之正负数数量统计
    阿里P7爆款《K8s+Jenkins》技术笔记,读了后确实有实质性的帮助
    2020年12月 C/C++(二级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • 原文地址:https://blog.csdn.net/goodjava2007/article/details/126425865