SpringBoot web环境检测:WebApplicationType分析

一、问题:

启动springboot项目后,定时启动项目中一个main方法,报错端口号被占用,kafka自启

2021-12-28 10:08:59.014 ERROR 801973 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 18828 was already in use.

Action:

Identify and stop the process that's listening on port 18828 or configure this application to listen on another port.

2021-12-28 10:08:59.023  INFO 801973 --- [ntainer#0-0-C-1] o.a.k.c.c.internals.AbstractCoordinator  : [Consumer clientId=test-1, groupId=test] (Re-)joining group
2021-12-28 10:08:59.024  INFO 801973 --- [ntainer#0-3-C-1] o.a.k.c.c.internals.AbstractCoordinator  : [Consumer clientId=test-4, groupId=test] (Re-)joining group

二、原因:

yml配置文件中:

spring:
  main:
    web-application-type: SERVLET

  kafka:    
    listener:   
      auto-startup: true

三、异常详解:

1、定时启动main方法时,项目启动

web环境检测:WebApplicationType分析
先来看SpringBoot支持那些Web类型:

ClassUtils.isPresent(String className, ClassLoader classLoader) 方法并不是简简单单看看classLoader之中有没有出现过传参的class。它的底层调用的是java.lang.Class#forName(java.lang.String, boolean, java.lang.ClassLoader)。这个方法会加载你指定的className到jvm,如果给回你要的class给你,常常在反射中使用。所以

public enum WebApplicationType {

	/**
	 * 不启动内嵌的WebServer,不是运行web application
	 */
	NONE,

	/**
	 * 启动内嵌的基于servlet的web server
	 */
	SERVLET,

	/**
	 * 启动内嵌的reactive web server,这个application是一个reactive web application
	 */
	REACTIVE;
    
    private static final String[] SERVLET_INDICATOR_CLASSES = { "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";

    
    static WebApplicationType deduceFromClasspath() {
        // 尝试加载org.springframework.web.reactive.DispatcherHandler,如果成功并且加载org.springframework.web.servlet.DispatcherServlet和org.glassfish.jersey.servlet.ServletContainer失败,则这个application是WebApplicationType.REACTIVE类型。
		if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null) && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
				&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
			return WebApplicationType.REACTIVE;
		}
		for (String className : SERVLET_INDICATOR_CLASSES) {
            // 如果ClassLoader里面同时加载这两个 javax.servlet.Servlet和 org.springframework.web.context.ConfigurableWebApplicationContext成功。则application是WebApplicationType.NONE 类型。
			if (!ClassUtils.isPresent(className, null)) { 
				return WebApplicationType.NONE;
			}
		}
        // application是 WebApplicationType.SERVLET 类型。
		return WebApplicationType.SERVLET;
	}

	static WebApplicationType deduceFromApplicationContext(Class<?> applicationContextClass) {
		if (isAssignable(SERVLET_APPLICATION_CONTEXT_CLASS, applicationContextClass)) {
			return WebApplicationType.SERVLET;
		}
		if (isAssignable(REACTIVE_APPLICATION_CONTEXT_CLASS, applicationContextClass)) {
			return WebApplicationType.REACTIVE;
		}
		return WebApplicationType.NONE;
	}
    //省略其他代码

可以看到启动springboot项目之后,读取配置文件 web-application-type: SERVLET,启动内嵌的基于servlet的web server,当再次执行main方法时,加载配置文件,项目又被重复启动,所有会报端口号冲突的错误。

2.kafka自动启动

yml中配置kafka:listener: auto-startup: true,加载配置文件,kafka自动开始监听

四、解决方案

1、取消yml配置文件中的这两个参数
2、在启动命令中指定参数

/usr/bin/java  -jar 
-Dlogging.file.name=/home/dev/logs/test.log
-Dspring.kafka.listener.auto-startup=true 
-Dspring.main.web-application-type=SERVLET 
/home/dev/test.jar 
> /dev/null

3、再次定时启动main方法时,不指定这两个参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值