3

我正在尝试将 jcache 与 hazelcast 服务器提供商一起使用。但得到这个例外。

    java.lang.IllegalArgumentException: Cannot find cache named 'xyzCache' for Builder throws caches=[xyzCache] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
    at org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheR esolver.java:81)
    at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:242)
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.java:675)
    at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:255)
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContexts.<init>(CacheAspectSupport.java:581)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:327)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)

这是我用来配置 hazelcast 的 java 配置。

HazelcastConfiguration.java

    @EnableCaching
    class HazelcastConfiguration {

    @Bean
    public Config getConfig() throws FileNotFoundException {
    Config config;

    if ((xmlConfigLocation == null) || (xmlConfigLocation.isEmpty())) {
      // use default Hazelcast configuration
      config = new Config();
    } else {
      // overlay custom xml config on default Hazelcast configuration.
      config = new FileSystemXmlConfig(xmlConfigLocation);
    }
    //Trying to create cache config 
    MapConfig cache = new MapConfig();

    cache.setName("xyzCache");
    cache.getMaxSizeConfig().setSize(1);
    cache.setMaxIdleSeconds(0);
    cache.setTimeToLiveSeconds(86400);
    cache.setEvictionPolicy(EvictionPolicy.LRU);
    config.addMapConfig(cache);
    }
    }

使用的依赖:

    <dependency>
          <groupId>com.hazelcast</groupId>  
          <artifactId>hazelcast-spring</artifactId>  
          <version>3.6.8</version>  
       </dependency>  

    <dependency>
          <groupId>com.hazelcast</groupId>  
          <artifactId>hazelcast-cloud</artifactId>  
          <version>3.6.8</version>
       </dependency>

Spring Boot 版本:1.4.6

使用这些配置,我可以在应用程序中创建和使用 hazelcast 缓存。将以下依赖项添加到提供程序 jcache 缓存提供程序后。Spring boot 尝试从其自动配置和缓存管理器中使用 JCacheCacheConfiguration。

    <dependency>
          <groupId>javax.cache</groupId>
          <artifactId>cache-api</artifactId>
          <version>1.0.0</version>
       </dependency>

Spring Boot 启动应用程序时没有任何异常或错误。但是,一旦我尝试运行第一个 api 调用,它就会开始将我抛到异常之上。有什么建议吗?

4

3 回答 3

1

您似乎通过配置缓存MapConfig。尝试使用从config.getCacheConfig("xyzCache");配置部分中的方法返回的配置对象,然后让我们看看它是否解决了问题。

于 2017-10-24T20:26:39.283 回答
0

设置缓存实例的方法是Config上的config.setInstanceName("xyzConfig")

所以完整的代码应该是这样的:

@EnableCaching
class HazelcastConfiguration {

@Bean
public Config getConfig() throws FileNotFoundException {
    Config config;

    if ((xmlConfigLocation == null) || (xmlConfigLocation.isEmpty())) {
      // use default Hazelcast configuration
      config = new Config();
    } else {
      // overlay custom xml config on default Hazelcast configuration.
      config = new FileSystemXmlConfig(xmlConfigLocation);
    }

    config.setInstanceName("xyzConfig");

    //Trying to create cache config 
    MapConfig cache = new MapConfig();
    cache.getMaxSizeConfig().setSize(1);
    cache.setTimeToLiveSeconds(86400);
    cache.setEvictionPolicy(EvictionPolicy.LFU);

    // This were you put cache key and value
    config.getMapConfigs().put("xyzCache",cache);
}
于 2017-10-24T08:24:42.787 回答
0

javax.cache::cache-api工件不在您的类路径中时,Spring Boot 使用 Hazelcast 支持缓存IMap,因此您将MapConfig被拾取并配置一个IMap保存您Cacheable方法的缓存结果的缓存。

一旦 JCache API 位于类路径中,它就会尝试使用 Hazelcast 作为 JCache 实现(参见 [1])。在这种情况下,SpringJCacheCacheManager尝试获取CacheJCache 提供者已知的信息,因此您需要为您在@Cacheable注释中声明的缓存名称配置 Hazelcast(有关 Hazelcast JCache 配置,请参阅 [2])。例如,对于您最初发布的编程配置,何时javax.cache::cache-api在类路径中您需要配置 Hazelcast,如下所示:

@Bean
public Config getConfig() {
    Config config = new Config();
    // do your file stuff here
    CacheSimpleConfig cacheConfig = new CacheSimpleConfig();
    cacheConfig.setName("xyzCache");
    // set other options here
    config.addCacheConfig(cacheConfig);

    // alternatively to creating CacheSimpleConfig and adding it:
    // config.getCacheConfig("xyzCache").setBackupCount(1).set...;
    return config;
}

[1] https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-caching-provider-jcache

[2] http://docs.hazelcast.org/docs/3.9/manual/html-single/index.html#configuring-for-jcache

于 2017-10-31T12:35:11.167 回答