Using Spring Boot Data Geode

Spring Boot Data Geode is a new project aimed at simplifying the use of Geode/Gemfire/PivotalCloudCache. Its primary goal is to extend Spring Boot with auto-configuration support as well as streamline the programmer’s experience while working in the spring ecosystem.

The project has the following primary goals.

  • Auto Configure the ClientCache automatically when the project’s starter is on the classpath.

or

  • Auto Configure Spring’s Cache Abstraction

  • Provide automatic connectivity to a cache when the application is deployed to Pivotal Cloud Foundry and bound to a Pivotal Cloud Cache.

Getting Started

All code for this article can be found on Github

The easiest way to get started is to use Docker to start a local cache on your laptop.

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

Since the ClientCache is already registered by spring boot, all you need to do is define a ClientRegionFactoryBean in your configuration.

You can then inject this region into your code and do Cache Get/Put Operations.

Upon starting the app, Spring Boot will automatically connect to the Cache cluster running on your machine. It will use the locator address of localhost[10334]. If you wish to connect to a different Cache, you can use the following spring properties.

Testing

Testing can be done in two ways.

  • Unit Testing with Mockito Mocks
  • Integration Tests using a real server.

Mocks

By far the easiest way to test Gemfire code is with mocks. The Gemfire region can easily be mocked using Mockito.

Integration Testing

There is a new project called
Spring Geode Test

This project makes it trivial to spin up a test Locator and Server for use during an integration test.

  • Extend ForkingClientServerIntegrationTestsSupport
  • Add a Spring boot configuration class to bootstrap a server and configure your test regions.

To start, add the following dependency to your project.

Implement a @CacheServerAppliction to bootstrap the test server.

you can then run your service code as required.

The full test code can be found here.

https://github.com/jeffellin/gemfire-test-demo/blob/master/src/test/java/com/example/gftestdemo/GemFireTests.java

You may also like...