Ellin.com
Not Very Interesting
[Update] Spring Developers guide to Starting with Pivotal Cloud Cache and Gemfire
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.

>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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@SpringBootApplication
@CacheServerApplication(name = "SpringBootGemFireServer")
@EnableLocator
@EnableManager
public class SpringBootGemFireServer {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootGemFireServer.class);
    }
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;

    }

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
2
3
[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

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
2
3
4
5
6
7
8
9
@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 establish a typical Spring Data Repository.
1
2
3
4
5
6
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.

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

1
cf 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

1
cf 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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
cf create-service-key pcc pcc-key
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 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.

1
2
3
4
5
6
7
8
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>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.

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.

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

Create a PCF manifest to bind the cache to your application

1
2
3
4
5
6
7
8
applications:
- name: client
  path: target/gs-accessing-data-gemfire-0.1.0.jar
  no-hostname: true
  no-route: true
  health-check-type: none
  services:
  - 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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Bean
public RegionConfigurer regionConfigurer(){
    return new RegionConfigurer() {
        @Override
        public void configure(String beanName, ClientRegionFactoryBean<?, ?> bean) {
            if(beanName.equals("Person")){
            //bean.setCacheListeners(...);
            }
          }
    };
}

@Bean
public ClientCacheConfigurer cacheConfigurer(){
    return new ClientCacheConfigurer() {
        @Override
        public void configure(String beanName, ClientCacheFactoryBean clientCacheFactoryBean) {
            //customize the cache
        clientCacheFactoryBean.setSubscriptionEnabled(false);
            }
          }
    };
}

Last modified on 2019-04-10

comments powered by Disqus