当我启动eureka服务是遇到了一个问题:
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.
环境:
#eureka server服务端口
server:
port: 8761
spring:
application:
name: lagou-cloud-eureka-server # 应用名称,应用名称会在Eureka中作为服务名称
# eureka 客户端配置(和Server交互),Eureka Server 其实也是一个Client
eureka:
instance:
hostname: localhost # 当前eureka实例的主机名
client:
service-url:
# 配置客户端所交互的Eureka Server的地址(Eureka Server集群中每一个Server其实相对于其它Server来说都是Client)
# 集群模式下,defaultZone应该指向其它Eureka Server,如果有更多其它Server实例,逗号拼接即可
defaultZone: http://${eureka.instance.hostname}:8761/eureka
register-with-eureka: false # 集群模式下可以改成true
fetch-registry: false # 集群模式下可以改成true
praent pom
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.lhj.learngroupId>
<artifactId>lagou-parentartifactId>
<version>1.0-SNAPSHOTversion>
<modules>
<module>lagou-service-commonmodule>
<module>lagou-service-autodelivermodule>
<module>lagou-service-resumemodule>
<module>lagou-cloud-eureka-server-8761module>
modules>
<packaging>pompackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.6.RELEASEversion>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Greenwich.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-loggingartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.4version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>8source>
<target>8target>
<encoding>utf-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
pom
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>lagou-parentartifactId>
<groupId>com.lhj.learngroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>lagou-cloud-eureka-server-8761artifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
dependencies>
project>
启动类代码贴下
参考GPT的内容和自己的思路:
根据错误信息,是因为找不到ServletWebServerFactory Bean导致启动失败。这个Bean在Spring Boot中的spring-boot-starter-web依赖中。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
mvn dependency:purge-local-repository
mvn clean install -U
另外,如果您的应用程序中使用了其他web服务器而非内置的Tomcat,则需要手动提供ServletWebServerFactory Bean。
分析:
这是eureka服务所以不需要spring boot 的web包。eureka服务端的启动只需要做两件事情。
1.引入eureka依赖。
2.application.yml配置文件。
3、启动上加注解。
既然现在出问题了肯定是这三步中出现。
排错:
* 我删除了maven本地仓库的相关jar包 并重新引入,依旧报错。
* application.yml我看了很多模板没有问题
* 现在只剩下启动类的错误,集中火力。我发现启动类配置错误了。
错误如下:
/**
* @author lihaojie
* @date 2023/03/06 09:06
**/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer8761 {
public static void main(String[] args) {
SpringApplication.run(EnableEurekaServer.class, args);
}
}
正确配置:
/**
* @author lihaojie
* @date 2023/03/06 09:06
**/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer8761 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer8761.class, args);
}
}