0

我创建了一个使用分布式 ehcache 的 spring boot 项目。我下载并运行带有 tc-config.xml 配置的 terracotta 服务器。服务器在 localhost: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

任何帮助,将不胜感激

4

2 回答 2

1

您的版本不匹配:

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

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

如果要使用 Ehcache 2.x,请注释掉 Ehcache 3 依赖项并使用 Ehcache 2 依赖项。

如果要使用 Ehcache 3.x,请将 Spring Ehcache 集成替换为 Spring JCache 集成,并更新 XML 以使用 Ehcache 3 格式。

于 2018-01-11T08:15:16.033 回答
0

在您的 pom.xml 中使用以下 maven 依赖项

 <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 它对我不起作用。

于 2019-09-11T12:00:51.603 回答