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

SpringBoot 3.0.0无法识别Eureka服务与客户端问题咨询

SpringBoot 3.0.0与2.7.4的Eureka兼容问题及版本变更说明

在SpringBoot 3.0.0环境下,Eureka Server与Client无法互相识别,但切换到2.7.4版本后可正常运行,以下是版本间的关键变更及修复方案,附原代码实现:


Api-Gateway(Eureka Server)

启动类代码

@SpringBootApplication
@EnableEurekaServer
public class ApiGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);
    }
}

application.properties配置

spring.application.name = api-gateway
server.port = 8761
eureka.client.register-with-eureka = false
eureka.client.fetch-registry = false
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

路由模块(Gateway + Eureka Client)

启动类代码

@EnableDiscoveryClient
@SpringBootApplication
public class RoutingApplication {
    public static void main(String[] args) {
        SpringApplication.run(RoutingApplication.class, args);
    }
}

application.properties配置

server.port=8080
spring.application.name=routing
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.instance.hostname=localhost
spring.cloud.gateway.discovery.locator.enabled=true

#spring.main.web-application-type=reactive
spring.cloud.gateway.enabled=true
spring.cloud.gateway.routes[0].id=user
spring.cloud.gateway.routes[0].uri=lb://USER
spring.cloud.gateway.routes[0].predicates=Path=/user/**

spring.cloud.gateway.routes[1].id=testes
spring.cloud.gateway.routes[1].uri=lb://TESTES
spring.cloud.gateway.routes[1].predicates=Path=/testes/**

spring.cloud.gateway.routes[2].id=user-create
spring.cloud.gateway.routes[2].uri=lb://USER-CREATE
spring.cloud.gateway.routes[2].predicates=Path=/user-create/**

spring.cloud.gateway.routes[3].id=category-product
spring.cloud.gateway.routes[3].uri=lb://CATEGORY-PRODUCT
spring.cloud.gateway.routes[3].predicates=Path=/category-product/**

Category-Product模块(Eureka Client)

启动类代码

@EnableDiscoveryClient
@SpringBootApplication
public class CategoryProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(CategoryProductApplication.class, args);
    }
}

控制器代码

@RestController
@RequestMapping("/category-product")
public class CategoryProductContriller {
  @GetMapping("/item")
  public String itens()
  {
    return "Testar";
  }
}

application.properties配置

server.port = 8001
spring.application.name = category-product
eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka
eureka.instance.hostname=localhost

版本间关键变更及修复方案

1. 依赖版本适配

SpringBoot 3.0.x对应的Spring Cloud版本为2022.0.x(Kilburn),该版本基于Jakarta EE 9,而适配SpringBoot 2.x的旧版Eureka依赖Java EE(javax包),会导致类加载异常。需确保引入适配Jakarta的Eureka依赖:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2022.0.4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- Eureka Server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<!-- Eureka Client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!-- Spring Cloud Gateway -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

2. 配置属性格式修正

SpringBoot 3.0对配置属性绑定更严格,Eureka Client的配置前缀从驼峰式eureka.client.serviceUrl.defaultZone改为短横线分隔的eureka.client.service-url.defaultZone,同时注意URL末尾的/需统一添加,避免解析错误:

# 修正后的配置
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

3. 恢复Gateway的Reactive模式

Spring Cloud Gateway基于WebFlux实现,必须运行在Reactive环境下,取消注释路由模块配置中的spring.main.web-application-type=reactive:

spring.main.web-application-type=reactive

验证步骤

  1. 启动Api-Gateway(Eureka Server),访问http://localhost:8761确认控制台正常显示;
  2. 启动Category-Product模块,查看Eureka控制台是否成功注册该服务;
  3. 启动路由模块,访问http://localhost:8080/category-product/item验证路由是否生效。

内容的提问来源于stack exchange,提问作者Rafael Souza

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 15:15:42