[Update] 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 is an update to an older post which was based on older versions of Spring/Spring Data Geode.  In this article, you will learn how to complete the following steps.

  • 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 on Github.

Cache Server for local development

Use Spring Boot

 1@SpringBootApplication
 2@CacheServerApplication(name = "SpringBootGemFireServer")
 3@EnableLocator
 4@EnableManager
 5public class SpringBootGemFireServer {
 6
 7    public static void main(String[] args) {
 8        SpringApplication.run(SpringBootGemFireServer.class);
 9    }
10This 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.
11
12    @Bean(name = "Persons")
13    ReplicatedRegionFactoryBean personsRegion(Cache gemfireCache) {
14
15        ReplicatedRegionFactoryBean person = new ReplicatedRegionFactoryBean<>();
16
17        person.setCache(gemfireCache);
18        person.setClose(false);
19        person.setPersistent(false);
20
21        return person;
22
23    }

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

Start the cache server by running the main class.

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

Alternatively, you can use a docker image to achieve the same result.

1    docker run -ti -p 40404:40404 -p 10334:10334 apachegeode/geode:1.6.0 bash

Running this command will drop you into a GFSH shell that you can use to start the locator, server and create some regions.

```bash start locator --name locator start server --name server1 create region --name /restrictionRegion --type=REPLICATE ```

Simple Client

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

1@SpringBootApplication
2@ClientCacheApplication(name = "AccessingDataGemFireApplication", logLevel = "error")
3@EnableEntityDefinedRegions(basePackages = {"com.example.demogemfire.model"},
4        clientRegionShortcut = ClientRegionShortcut.CACHING_PROXY)
5@EnableGemfireRepositories
6@EnablePdx()
7public class DemoGemfireApplication {
8...
9}
  • 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 establish a typical Spring Data Repository.
1interface PersonRepository extends CrudRepository<Person, String> {
2
3    Person findByName(String name);
4  
5    ...
6}  

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.

1spring.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.

1cf marketplace

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

1cf create-service p-cloudcache dev-plan pcc

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

1cf service pcc

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.

 1cf create-service-key pcc pcc-key
 2cf service-key pcc pcc-key                                       
 3
 4Getting key pcc-key for service instance pcc as jellin@pivotal.io...
 5
 6{
 7  "distributed_system_id": "12",
 8  "locators": [
 9  "192.168.12.186[55221]"
10  ],
11  "urls": {
12  "gfsh": "https://cloudcache-yourserver.io/gemfire/v1",
13  "pulse": "https://cloudcache-yourserver.io/pulse"
14  },
15  "users": [
16  {
17    "password": "**********",
18    "roles": [
19    "developer"
20    ],
21    "username": "developer_*******"
22  },
23  {
24    "password": "***********",
25    "roles": [
26    "cluster_operator"
27    ],
28    "username": "cluster_operator_*******"
29  }
30  ],
31  "wan": {
32  "sender_credentials": {
33    "active": {
34    "password": "**********",
35    "username": "gateway_sender_*******"
36    }
37  }
38  }
39}

Create Regions 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.

1./gfsh

Connect to the cache to create the region.

1gfsh>connect --use-http --url https://cloudcache-yourserver.io/gemfire/v1 --user=cluster_operator_DnrQ139FKwjTaLpBJsuQ --password=OxKlo8GXHGgWcRNGPx6nw
2Successfully connected to: GemFire Manager HTTP service @ org.apache.geode.management.internal.web.http.support.HttpRequester@a34930a
3
4
5Cluster-12 gfsh>create region --name=Person --type=REPLICATE
6                      Member                      | Status
7------------------------------------------------ | ------------------------------------------------------------------------
8cacheserver-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.

In a previous post I talked about manually creating the ClientCache bean to customize it. This procedure is no longer necessary with Spring Boot Data Gemfire. When pushing an app to cloud foundry as long as it is bound to PCC the credentials will automatically be loaded from VCAP_SERVICES.

If you are running your application outside of PCC you can manually set this information via spring properties.

1spring.data.gemfire.pool.locators=192.168.12.185[55221]
2spring.data.gemfire.security.username=cluster_operator_****
3spring.data.gemfire.security.password=****

Create a PCF manifest to bind the cache to your application

1applications:
2- name: client
3  path: target/gs-accessing-data-gemfire-0.1.0.jar
4  no-hostname: true
5  no-route: true
6  health-check-type: none
7  services:
8  - pcc

Push your app as normal

Customizing Region and Cache Configuration.

With Spring Boot Data G the Region and Client Cache are automatically configured. Sometimes the settings need adjustments. In the past version of this post, I advocated just creating these beans manually. In Spring Boot Data G there is an interface that can be used to customize these beans before their creation.

 1@Bean
 2public RegionConfigurer regionConfigurer(){
 3    return new RegionConfigurer() {
 4        @Override
 5        public void configure(String beanName, ClientRegionFactoryBean<?, ?> bean) {
 6            if(beanName.equals("Person")){
 7            //bean.setCacheListeners(...);
 8            }
 9          }
10    };
11}
12
13@Bean
14public ClientCacheConfigurer cacheConfigurer(){
15    return new ClientCacheConfigurer() {
16        @Override
17        public void configure(String beanName, ClientCacheFactoryBean clientCacheFactoryBean) {
18            //customize the cache
19        clientCacheFactoryBean.setSubscriptionEnabled(false);
20            }
21          }
22    };
23}
comments powered by Disqus