前言
springcloudalibaba项目启动报错,如下:
[2022-03-15 21:50:46] [INFO ] [com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory:176] -- The Dubbo GenericService ReferenceBeans are destroying...
[2022-03-15 21:50:46] [INFO ] [org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor:307] -- class org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor was destroying!
[2022-03-15 21:50:46] [ERROR] [org.springframework.boot.SpringApplication:826] -- Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at com.ifly.vvm.ExportApplication.main(ExportApplication.java:27)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:203)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
... 8 common frames omitted
分析
报这个错,是由于启动的spring项目没有引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
解决
我们可以引入spring-boot-starter-web来解决报错问题,但是项目是启动在固定tomcat端口的。
另外,
随着微服务架构的流行,想要启动一个微服务架构项目就要开启好多端口,有时候一台机器上部署的项目多的时候,端口资源就比较紧张了,其实有的微服务组件仅仅只是提供RPC服务,可以不用占用web启动的端口,此时spring boot 不占用web端口的方式就派上用场了,但是spring boot 1.x与spring boot 2.x的配置是有区别的,在使用时一定要注意一下自己所使用的版本
spirngboot 2.x之前(代码方式实现):
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder().sources(Application.class).web(false).run(args);
}
}
spinrboot 2.x之前(另外一种代码实现方式)
@Configuration
@EnableAutoConfiguration
public class MyClass{
public static void main(String[] args) throws JAXBException {
SpringApplication app = new SpringApplication(MyClass.class);
app.setWebEnvironment(false);
ConfigurableApplicationContext ctx = app.run(args);
}
}
spinrboot 2.x之前(配置方式)
spinrboot 2.x之前(配置方式)
springboot 2.x之后(代码方式)
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class)
.web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
.run(args);
}
}
springboot 2.x之后(配置方式)
spring.main.web-application-type=none
结果
因为我的程序是springboot2.2.10版本的,为了方便就直接添加配置spring.main.web-application-type=none就启动正常啦!
希望能够帮到更多的人