Spring Developers guide to Starting with Pivotal Cloud Cache and Gemfire

This page was converted from my old blog and hasn't been reviewed. If you see an error please let me know in the comments.

The following article will help the Spring developer complete the following steps.

This article has been updated. Latest content is here

  1. Create a local GemFire Server for testing.
  2. Create a cache client application.
  3. Create a Pivotal Cloud Cache service instance in Pivotal Cloud Foundry (PCF)
  4. Deploy client application to PCF

The complete code is here.

Cache Server for local development

Use Spring Boot

@CacheServerApplication(name = "SpringBootGemFireServer")
public class SpringBootGemFireServer {
public static void main(String[] args) {

This code will start up a single node Gemfire Server with both a locator and a cache node. Once you have the cache server running, you will need to create a region in which to store data.

    @Bean(name = "Persons")
    ReplicatedRegionFactoryBean personsRegion(Cache gemfireCache) {
    ReplicatedRegionFactoryBean person = new ReplicatedRegionFactoryBean&lt;&gt;();


    return person;


The Customer region will store Customer objects with a Long type used as the key.

Start the cache server by running the main class.

[info 2018/04/12 12:16:10.786 EDT <main> tid=0x1] Initializing region Customers
[info 2018/04/12 12:16:10.786 EDT <main> tid=0x1] Initialization of region Customers completed
Cache server connection listener bound to address

Simple Client

Next, we will create a simple client also using Spring Boot which will do most of the heavy lifting for us.

@ClientCacheApplication(name = "AccessingDataGemFireApplication", logLevel = "error")
@EnableEntityDefinedRegions(basePackages = {"com.example.demogemfire.model"},
        clientRegionShortcut = ClientRegionShortcut.CACHING_PROXY)
public class DemoGemfireApplication {
  1. This class is a SpringBootApplication
  2. This application requires a client cache
  3. Automatically define client regions based on Repositories found on the classpath
  4. Make Repositories found, GemFire repositories
  5. Enable PDX Serialization.

We then can define a typical Spring Data Repository.

interface PersonRepository extends CrudRepository<Person, String> {
Person findByName(String name);



Lastly, we need to tell Spring where to find the cache locator by adding a property to application.properties. The correct host and port should be visible in your CacheServer startup log.


When you run the application, you should see output indicating data was placed in the cache and subsequently retrieved from the cache.

Using Pivotal Cloud Cache (PCC)

PCC is a cloud-based cache that can be deployed to Cloud Foundry. Assuming your PCF instance has PCC already installed you can efficiently utilize the cf command line to create and maintain your cache.

Create the cache

  1. Verify that PCC is available.
  <pre><code class="language-none">cf marketplace</code></pre>

Look for `p-cloudcache`. If it isn&rsquo;t available, you will need to work with your cloud operator to have them install the tile.
  1. Create the service
  <pre><code class="language-none">cf create-service p-cloudcache dev-plan pcc</code></pre>

Create a service instance of the cloud cache called `pcc` This may take some time to complete so you can monitor its progress with

  <pre><code class="language-none">cf service pcc</code></pre>
  1. Service Key
Once the instance creation succeeds, we will need a service key. The service key will provide the required credentials for working with the cache. By default, you will have two users one with `developer` access and one with `operator` access. This information will also be exposed via `VCAP_SERVICES` to allow applications in other deployed containers to connect.

  <pre><code class="language-none">cf create-service-key pcc pcc-key</code></pre>

  <pre><code class="language-none">cf service-key pcc pcc-key                                                         

Getting key pcc-key for service instance pcc as jellin@pivotal.io...

{ "distributed_system_id": "12", "locators": [ "[55221]" ], "urls": { "gfsh": "https://cloudcache-yourserver.io/gemfire/v1", "pulse": "https://cloudcache-yourserver.io/pulse" }, "users": [ { "password": "", "roles": [ "developer" ], "username": "developer_" }, { "password": "***********", "roles": [ "cluster_operator" ], "username": "cluster_operator_" } ], "wan": { "sender_credentials": { "active": { "password": "", "username": "gateway_sender_*" } } } }

  1. Create the Using GFSH
Create a Region in PCC to hold the data. Use the locator URL and GFSH operator credentials from above.

Load the GFSH utility included with the GemFire distribution.

  <pre><code class="language-none">./gfsh</code></pre>

Connect to the cache

  <pre><code class="language-none">gfsh&gt;connect --use-http --url https://cloudcache-yourserver.io/gemfire/v1 --user=cluster_operator_DnrQ139FKwjTaLpBJsuQ --password=OxKlo8GXHGgWcRNGPx6nw

Successfully connected to: GemFire Manager HTTP service @ org.apache.geode.management.internal.web.http.support.HttpRequester@a34930a

Cluster-12 gfsh>

Create the region

  <pre><code class="language-none">Cluster-12 gfsh&gt;create region --name=Person --type=REPLICATE
                 Member                      | Status

------------------------------------------------ | ------------------------------------------------------------------------ cacheserver-3418fce1-13dd-4104-97ba-083b11b7a936 | Region "/Person" created on "cacheserver-3418fce1-13dd-4104-97ba-083b1..

Service Discovery

When binding a service to an application container in PCF, we can expose connection information such as URLs and credentials that may change over time. Spring Cloud for Gemfire can automate the retrieval of these credentials.

**NOTE**: While it would be ideal to use Spring Cloud Gemfire to automate the connection we can’t currently extend additional configuration parameters such as PDX Serialization. This is because the connector creates the `ClientCache` before the `@ClientCacheApplication` annotation. In order to work around this add the `@EnableSecurity` annotation and the following config properties.

This is being addressed in a future release of Spring Boot Starter for Gemfire/Geode. Until then here is a possible work around.

If you manually create the ClientCache you have more control but you will lose the benefit of using the annotations.

   ClientCache clientCache() throws IOException, URISyntaxException {
   Properties props = new Properties();
   props.setProperty("security-client-auth-init", "com.example.demogemfire.config.ClientAuthInitialize.create");
   ClientCacheFactory ccf = new ClientCacheFactory(props);
   ccf.setPdxSerializer(new MappingPdxSerializer());
   List&lt;URI&gt; locatorList = EnvParser.getInstance().getLocators();

   for (URI locator : locatorList) {
       ccf.addPoolLocator(locator.getHost(), locator.getPort());

   return ccf.create();


In the above example the EnvParser is responsible for gathering the required data out of VCAP_SERVICES.

Working Code on this branch

Create a PCF manifest to bind the cache to your application

- name: client
  path: target/gs-accessing-data-gemfire-0.1.0.jar
  no-hostname: true
  no-route: true
  health-check-type: none
  - pcc

Push your app as normal

cf push

use cf client to view the results

cf logs client
comments powered by Disqus