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
- Create a local GemFire Server for testing.
- Create a cache client application.
- Create a Pivotal Cloud Cache service instance in Pivotal Cloud Foundry (PCF)
- Deploy client application to PCF
The complete code is here.
Cache Server for local development
Use Spring Boot
@SpringBootApplication
@CacheServerApplication(name = "SpringBootGemFireServer")
@EnableLocator
@EnableManager
public class SpringBootGemFireServer {
public static void main(String[] args) {
SpringApplication.run(SpringBootGemFireServer.class);
}</code></pre>
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<>();
person.setCache(gemfireCache);
person.setClose(false);
person.setPersistent(false);
return person;
}</code></pre>
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 0.0.0.0/0.0.0.0:40404
Simple Client
Next, we will create a simple client also using Spring Boot which will do most of the heavy lifting for us.
@SpringBootApplication
@ClientCacheApplication(name = "AccessingDataGemFireApplication", logLevel = "error")
@EnableEntityDefinedRegions(basePackages = {"com.example.demogemfire.model"},
clientRegionShortcut = ClientRegionShortcut.CACHING_PROXY)
@EnableGemfireRepositories
@EnablePdx()
public class DemoGemfireApplication {
...
}
- This class is a SpringBootApplication
- This application requires a client cache
- Automatically define client regions based on Repositories found on the classpath
- Make Repositories found, GemFire repositories
- 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.
spring.data.gemfire.pool.locators=localhost[10334]
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
- Verify that PCC is available.
<div>
<pre><code class="language-none">cf marketplace</code></pre>
</div>
Look for `p-cloudcache`. If it isn’t available, you will need to work with your cloud operator to have them install the tile.
- Create the service
<div>
<pre><code class="language-none">cf create-service p-cloudcache dev-plan pcc</code></pre>
</div>
Create a service instance of the cloud cache called `pcc` This may take some time to complete so you can monitor its progress with
<div>
<pre><code class="language-none">cf service pcc</code></pre>
</div>
- 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.
<div>
<pre><code class="language-none">cf create-service-key pcc pcc-key</code></pre>
</div>
<div>
<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": [ "192.168.12.186[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_*" } } } }
- 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.
<div>
<pre><code class="language-none">./gfsh</code></pre>
</div>
Connect to the cache
<div>
<pre><code class="language-none">gfsh>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>