admin管理员组文章数量:1654276
写在前面
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组件导致的该问题。
本文标签: 解决方案StopidentifySpringBootProcess
版权声明:本文标题:SpringBoot - Identify and stop the process that‘s listening on port 8080解决方案 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1729648608a1208767.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论