Spring Boot连接到Pivotal Cloud缓存

时间:2018-11-08 17:55:56

标签: spring apache spring-boot pivotal-cloud-foundry geode

我的项目正在使用Spring Boot 2.0.4-RELEASE版本。 我们正在部署到Pivotal Cloud Foundry上,并且正在使用使用Apache Geode的Pivotal Cloud Cache服务服务。 如果我使用gemfire shell创建Cloud Cache区域,则我的代码已启动并正在运行,但是我想从我的代码中创建这些区域,以便不遗漏任何内容。 我有以下配置类,在其中尝试创建区域

package com.mycompany.services.config;

import org.apache.geode.cache.ExpirationAction;
import org.apache.geode.cache.ExpirationAttributes;
import org.apache.geode.cache.GemFireCache;
import org.apache.geode.cache.RegionShortcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
import org.springframework.data.gemfire.config.annotation.EnableCachingDefinedRegions;
import org.springframework.data.gemfire.config.annotation.EnablePdx;

@Configuration
@EnableCachingDefinedRegions
@EnablePdx()
public class Config {

    @Bean
    public PartitionedRegionFactoryBean<?, ?> createCacheRegion(GemFireCache gemfireCache) {
        int expirationTime = 600;
        String regionName = "cacheRegion";

        PartitionedRegionFactoryBean<?, ?> newRegion = new PartitionedRegionFactoryBean<>();
        newRegion.setCache(gemfireCache);
        newRegion.setClose(false);
        newRegion.setPersistent(true);
        newRegion.setName(regionName);

        ExpirationAttributes entryTimeToLive = new ExpirationAttributes(expirationTime,  ExpirationAction.INVALIDATE);
        newRegion.setEntryTimeToLive(entryTimeToLive);
        newRegion.setShortcut(RegionShortcut.REPLICATE);
        return newRegion;
    }

}

但是-当此代码在Pivotal Cloud Foundry上运行时-我收到以下错误

2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] 2018-11-08 17:46:09.795 ERROR 19 --- [ main] o.s.boot.SpringApplication : Application run failed
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dqsCacheRegion' defined in class path resource [com/mycompany/services/config/Config.class]: Invocation of init method failed; nested exception is java.lang.UnsupportedOperationException: operation is not supported on a client cache
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:740) ~[spring-beans-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE]
2018-11-08T17:46:09.797+00:00 [APP/PROC/WEB/0] [OUT] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE]

任何人都可以提供任何建议以帮助我在我的代码中创建Pivotal Cloud Cache(Apache Geode)区域,而不是必须使用gemfire shell创建它们

谢谢 达米安

1 个答案:

答案 0 :(得分:1)

客户端应用程序无法在服务器上创建区域,该任务应由具有管理员特权的操作员通过gfsh,单个cache.xml配置文件或{{3} }。允许客户随意创建区域可能会适得其反,并在您的集群上生成大量未使用的区域以及不必要的噪音和开销,这在生产环境中根本不建议这样做。

也就是说,cluster configuration service允许您自动从客户端应用程序Spring Data for Apache Geode/GemFire,这在开发环境中很有用。我不知道Pivotal Cloud Cache内部策略和限制是否允许这样做,您可能想直接与Pivotal进行检查。

希望这会有所帮助。 干杯。