Spring Developers guide to Starting with Pivotal Cloud Cache and Gemfire

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

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.

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

Start the cache server by running the main class.

Simple Client

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

  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.

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.

    Look for p-cloudcache. If it isn’t available, you will need to work with your cloud operator to have them install the tile.

  2. Create the service

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

  3. 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.

  4. 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.

    Connect to the cache

    Create the region

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.

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

Push your app as normal

use cf client to view the results

You may also like...