You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot2.5.2整合Eureka与Gateway报无法初始化Eureka servlet上下文错误

问题根源

Spring Cloud Gateway 基于WebFlux响应式框架,默认使用Netty作为运行容器,不会初始化Servlet上下文;而Eureka Server的核心实现依赖Servlet容器环境,启动时需要读取Servlet上下文,二者默认的运行环境冲突,就会触发EurekaServerBootstrap第73行的空指针异常。
另外你当前的注册中心还缺少两项必要配置:一是没有配置bootstrap指向配置中心拉取配置,二是配置中心存储的discovery-service.yml中缺少Eureka Server的基础配置。

解决方案

第一步:修复依赖冲突

在注册中心的pom.xml中添加spring-boot-starter-web依赖,引入Tomcat Servlet容器,提供Eureka Server所需的Servlet上下文:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

第二步:添加注册中心本地bootstrap配置

在注册中心的src/main/resources下新建bootstrap.yml,添加配置指向配置中心拉取配置:

spring:
  application:
    name: discovery-service
  cloud:
    config:
      uri: http://localhost:8081
      # 配置中心默认安全用户名为user,密码为你配置的configPassword
      username: user
      password: configPassword

第三步:补充Eureka Server配置

修改配置中心的shared/discovery-service.yml,添加Eureka Server的基础配置:

server:
  port: 8082
# 新增Eureka配置
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# 新增web应用类型配置,避免web和webflux依赖冲突
spring:
  main:
    web-application-type: servlet

logging:
  level:
    org:
      hibernate:
        SQL: DEBUG
        type:
          descriptor:
            sql:
              BasicBinder: TRACE

完成以上三步后重启服务即可正常启动。

内容的提问来源于stack exchange,提问作者Herval NGANYA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 16:12:01