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

Spring Boot集成分布式Terracotta EhCache启动报错求助

我来帮你解决这个启动报错的问题,核心错误java.lang.NoClassDefFoundError: net/sf/ehcache/config/TerracottaConfigConfiguration说明你的项目缺少了支持Terracotta分布式缓存的EhCache配置类,根源出在Maven依赖的冲突和缺失上,下面是具体的解决方案:

问题根源分析

你的pom.xml里存在多个重复且版本不兼容的EhCache相关依赖:

  • 同时引入了ehcache、ehcache-core、ehcache-clustered,这些包之间存在版本冲突
  • 缺少专门支持Terracotta集成的ehcache-terracotta依赖
  • 部分依赖版本和你使用的Terracotta 4.0.0服务器不匹配

具体修复步骤

1. 清理并重构Maven依赖

删除重复的EhCache相关依赖,保留正确的组合。下面是修正后的依赖片段:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    <relativePath />
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <!-- 指定EhCache版本,和Terracotta 4.0.0兼容 -->
    <ehcache.version>2.10.6</ehcache.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- 核心EhCache依赖,指定和Terracotta兼容的版本 -->
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>${ehcache.version}</version>
    </dependency>
    <!-- Terracotta集成依赖,提供分布式缓存支持 -->
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-terracotta</artifactId>
        <version>${ehcache.version}</version>
    </dependency>
    <!-- Terracotta Toolkit依赖,保持和服务器版本一致 -->
    <dependency>
        <groupId>org.terracotta</groupId>
        <artifactId>terracotta-toolkit-runtime-ee</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- Hibernate EhCache集成,使用和Spring Boot兼容的版本 -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>5.0.12.Final</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>terracotta-releases</id>
        <url>http://www.terracotta.org/download/reflector/releases/</url>
    </repository>
</repositories>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

依赖调整说明:

  • 移除了重复的ehcache-core、ehcache-clustered依赖,ehcache已包含核心功能,ehcache-terracotta专门提供分布式缓存支持
  • 指定了和Terracotta 4.0.0兼容的EhCache版本(2.10.x系列是和Terracotta 4.x匹配的稳定版本)
  • 更新了hibernate-ehcache版本,确保和Spring Boot 1.5.6.RELEASE的Hibernate版本兼容
  • 保留Terracotta仓库配置,确保能下载到专属依赖

2. 确认ehcache.xml配置正确性

你的现有配置是有效的,可补充根标签的name属性(可选,不影响核心功能):

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         name="ticketBookingCacheManager">
    <terracottaConfig url="localhost:9510"/>
    <cache name="ticketsCache" maxEntriesLocalHeap="1000">
        <terracotta consistency="eventual"/>
    </cache>
</ehcache>

3. 缓存管理器代码无需修改

你在TicketBookingManagementApplication中的缓存管理器配置是正确的,依赖修复后就能正常加载EhCache配置并连接到Terracotta服务器。

验证步骤

  1. 执行mvn clean install清理并重新构建项目,确保所有依赖正确下载
  2. 确认Terracotta服务器(localhost:9510)处于运行状态
  3. 启动Spring Boot项目,此时应该能成功创建缓存管理器并连接到Terracotta服务器

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:58:40