当前位置: 首页 > 知识库问答 >
问题:

使用分布式terracotta ehcache运行spring boot代码时出现异常

庄高谊
2023-03-14

我已经创建了一个使用分布式ehcache的spring boot项目。我下载并运行terracotta服务器,配置在tc-config.xml中。服务器在本地主机9510上成功运行。以下是我的ehcache。xml,它位于spring boot项目的类路径中

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

以下是我的maven依赖。

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.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>
        <!-- <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency> -->

        <!-- https://mvnrepository.com/artifact/org.ehcache/ehcache-clustered -->
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache-clustered</artifactId>

        </dependency>
            <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core -->
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
   <version>2.6.5</version>
</dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-terracotta -->
        <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core -->

        <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core -->
        <!-- <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> 
            <version>2.6.6</version> </dependency> -->

        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>

        </dependency>



        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency> -->

        <dependency>
            <groupId>org.terracotta</groupId>
            <artifactId>terracotta-toolkit-runtime-ee</artifactId>
            <version>4.0.0</version>
        </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->

    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>3.3.2.GA</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/javax.cache/cache-api -->
<dependency>
    <groupId>javax.cache</groupId>
    <artifactId>cache-api</artifactId>
    <version>1.0.0-PFD</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>

下面是我尝试创建缓存管理器的代码,以便在我的应用程序中使用它

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

@SpringBootApplication
@EnableCaching
public class TicketBookingManagementApplication {

    public TicketBookingManagementApplication() {

    }
    @Bean
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheCacheManager().getObject());
    }
     @Bean
        public EhCacheManagerFactoryBean ehCacheCacheManager() {
            EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
            cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
            cmfb.setShared(true);
            return cmfb;
        }
    public static void main(String[] args) {
        SpringApplication.run(TicketBookingManagementApplication.class, args);
    }

}

但我得到的例外如下

cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in com.infotech.book.ticket.app.TicketBookingManagementApplication: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'cacheManager' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheCacheManager' defined in com.infotech.book.ticket.app.TicketBookingManagementApplication: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: java.lang.NoClassDefFoundError: net/sf/ehcache/config/TerracottaConfigConfiguration
2018-01-10 14:57:25.675  INFO 27288 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2018-01-10 14:57:25.683  INFO 27288 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-01-10 14:57:25.701  INFO 27288 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-01-10 14:57:25.713 ERROR 27288 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in com.infotech.book.ticket.app.TicketBookingManagementApplication: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'cacheManager' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheCacheManager' defined in com.infotech.book.ticket.app.TicketBookingManagementApplication: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: java.lang.NoClassDefFoundError: net/sf/ehcache/config/TerracottaConfigConfiguration
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at com.infotech.book.ticket.app.TicketBookingManagementApplication.main(TicketBookingManagementApplication.java:32) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'cacheManager' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheCacheManager' defined in com.infotech.book.ticket.app.TicketBookingManagementApplication: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: java.lang.NoClassDefFoundError: net/sf/ehcache/config/TerracottaConfigConfiguration
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 18 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheCacheManager' defined in com.infotech.book.ticket.app.TicketBookingManagementApplication: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: java.lang.NoClassDefFoundError: net/sf/ehcache/config/TerracottaConfigConfiguration
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:334) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at com.infotech.book.ticket.app.TicketBookingManagementApplication$$EnhancerBySpringCGLIB$$fdf56fc4.ehCacheCacheManager(<generated>) ~[classes/:na]
    at com.infotech.book.ticket.app.TicketBookingManagementApplication.cacheManager(TicketBookingManagementApplication.java:22) [classes/:na]
    at com.infotech.book.ticket.app.TicketBookingManagementApplication$$EnhancerBySpringCGLIB$$fdf56fc4.CGLIB$cacheManager$0(<generated>) ~[classes/:na]
    at com.infotech.book.ticket.app.TicketBookingManagementApplication$$EnhancerBySpringCGLIB$$fdf56fc4$$FastClassBySpringCGLIB$$af6a5740.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at com.infotech.book.ticket.app.TicketBookingManagementApplication$$EnhancerBySpringCGLIB$$fdf56fc4.cacheManager(<generated>) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 19 common frames omitted
Caused by: net.sf.ehcache.CacheException: java.lang.NoClassDefFoundError: net/sf/ehcache/config/TerracottaConfigConfiguration
    at net.sf.ehcache.CacheManager.init(CacheManager.java:401) ~[ehcache-core-2.6.5.jar:na]
    at net.sf.ehcache.CacheManager.<init>(CacheManager.java:259) ~[ehcache-core-2.6.5.jar:na]
    at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:1037) ~[ehcache-core-2.6.5.jar:na]
    at net.sf.ehcache.CacheManager.newInstance(CacheManager.java:1013) ~[ehcache-core-2.6.5.jar:na]
    at net.sf.ehcache.CacheManager.create(CacheManager.java:998) ~[ehcache-core-2.6.5.jar:na]
    at org.springframework.cache.ehcache.EhCacheManagerFactoryBean.afterPropertiesSet(EhCacheManagerFactoryBean.java:144) ~[spring-context-support-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 38 common frames omitted
Caused by: java.lang.NoClassDefFoundError: net/sf/ehcache/config/TerracottaConfigConfiguration
    at java.lang.Class.getDeclaredConstructors0(Native Method) ~[na:1.8.0_131]
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) ~[na:1.8.0_131]
    at java.lang.Class.getConstructor0(Class.java:3075) ~[na:1.8.0_131]
    at java.lang.Class.getConstructor(Class.java:1825) ~[na:1.8.0_131]
    at net.sf.ehcache.util.ClassLoaderUtil.createNewInstance(ClassLoaderUtil.java:91) ~[ehcache-core-2.6.5.jar:na]
    at net.sf.ehcache.terracotta.TerracottaClusteredInstanceHelper.newClusteredInstanceFactory(TerracottaClusteredInstanceHelper.java:176) ~[ehcache-core-2.6.5.jar:na]
    at net.sf.ehcache.terracotta.TerracottaClient.createNewClusteredInstanceFactory(TerracottaClient.java:186) ~[ehcache-core-2.6.5.jar:na]
    at net.sf.ehcache.terracotta.TerracottaClient.createClusteredInstanceFactory(TerracottaClient.java:138) ~[ehcache-core-2.6.5.jar:na]
    at net.sf.ehcache.CacheManager.doInit(CacheManager.java:432) ~[ehcache-core-2.6.5.jar:na]
    at net.sf.ehcache.CacheManager.init(CacheManager.java:377) ~[ehcache-core-2.6.5.jar:na]
    ... 45 common frames omitted
Caused by: java.lang.ClassNotFoundException: net.sf.ehcache.config.TerracottaConfigConfiguration
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_131]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_131]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_131]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_131]
    ... 55 common frames omitted

任何帮助都将不胜感激

共有2个答案

孟洋
2023-03-14

在你的pom中使用下面的maven依赖性.xml

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.3.4.RELEASE</version>
    </dependency>

另外,请确保您使用版本4.3.4。不要使用5.0.6它对我不起作用。

毕泽宇
2023-03-14

您的版本不匹配:

  • 您的XML和Spring集成与Ehache2. x相关
  • 您的Ehcache依赖项是Ehcache 3. x坐标

您需要选择一个,因为Ehcache 2和3互不兼容。

如果你想使用埃卡什 2.x,请注释掉埃卡什 3 的依赖项,并使用埃卡什 2 的依赖项。

如果您想使用Ehcache3.x,请将SpringEhcache集成替换为SpringJCache集成,并更新XML以使用EhcCache3格式。

 类似资料:
  • 问题内容: 这次有人应该请小我努力使用分布式cahe运行我的代码。我已经在hdfs上保存了文件,但是当我运行以下代码时: 解决了很多问题,任何人都可以告诉我为什么我收到此错误: 问题答案: 问题在于您使用的文件名“〜/ ayush / output / part-00000”依赖于Unix shell(sh,bash,ksh)代字扩展名,将“〜”替换为主目录的路径名。 Java(以及C,C ++和

  • 我们开始Kafka,动物园管理员和Kafka连接在第一个盒子。我们也在第二个盒子里开始了Kafka连接。现在,根据confluent文档,我们必须使用REST API启动HDFS连接器(或任何其他连接器)。所以,在这两个框中启动kafka connect之后,我们尝试通过REST API启动connector。我们尝试了以下命令:- 当我们在这里按enter键时,我们得到以下响应: 位于etc/k

  • 我有一个代码如下: 当我尝试运行代码时,我得到这样的消息: 我提到了这个链接,但不明白如何修改上面的代码,使其可行:例外:需要mockito,但没有调用,实际上与这个mock没有任何交互 有人能帮忙吗。 通过添加c.getResult(新的A(新的B()),新的F())解决了上述错误;在上面 我错过了什么?我一直在使用的课程:

  • 当运行codeception时,我得到了很多错误:DeepCopy\DeepCopy::recursiveCopy。 这是个问题,让我困惑! 当我只有4个单元测试用例时,我使用codeception播放得很好: 当我仍然充满单元测试用例时,故事就发生了。 在我的测试类中有5个单元测试,然后我得到了这个: 下面是代码: php_error.log: PHP 195.#################

  • 我无法运行服务器......我得到的错误为 如何解决这个错误! 当我尝试使用不同的端口时....所有的人都给了我同样的错误!

  • 本文向大家介绍SpringBoot整合Redis正确的实现分布式锁的示例代码,包括了SpringBoot整合Redis正确的实现分布式锁的示例代码的使用技巧和注意事项,需要的朋友参考一下 前言 最近在做分块上传的业务,使用到了Redis来维护上传过程中的分块编号。 每上传完成一个分块就获取一下文件的分块集合,加入新上传的编号,手动接口测试下是没有问题的,前端通过并发上传调用就出现问题了,并发的ge