Data Grid Spring Boot Starter
Build Spring Boot Applications with Data Grid Copy linkLink copied to clipboard!
Abstract
The Data Grid starter provides a set of managed transitive dependencies that include everything your Spring Boot project needs to seamlessly interact with Data Grid.
The Data Grid Spring Boot starter gives you a convenient way to get started with Spring Boot but is optional. To use Data Grid with Spring Boot you can simply add the dependencies you want.
1. Setting Up Your Project Copy linkLink copied to clipboard!
Add dependencies for the Data Grid Spring Boot Starter to your project.
1.1. Spring Boot Starter Versions Copy linkLink copied to clipboard!
Data Grid supports Spring Boot 1.5.x and 2.x. For Spring Boot 1.5.x, use the Spring 4 dependencies. For Spring Boot 2.x, use the Spring 5 dependencies.
-
Spring Boot 1.5.x: Replace
${version.infinispan.starter}with1.0.7.Final-redhat-00021. -
Spring Boot 2.x: Replace
${version.infinispan.starter}with2.1.10.Final-redhat-00007.
1.2. Adding Dependencies for Usage Modes Copy linkLink copied to clipboard!
Data Grid provides different dependencies for each usage mode. Add one of the following to your pom.xml file:
Embedded Mode
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter-embedded</artifactId>
<version>${version.infinispan.starter}</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter-embedded</artifactId>
<version>${version.infinispan.starter}</version>
</dependency>
Remote Client/Server Mode
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter-remote</artifactId>
<version>${version.infinispan.starter}</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter-remote</artifactId>
<version>${version.infinispan.starter}</version>
</dependency>
The Data Grid Spring Boot starter uses different Spring Boot versions to other projects such as Red Hat OpenShift Application Runtimes. If you want to use a specific Spring Boot version for compatibility with other projects, you must add the correct dependency to your project.
2. Running in Embedded Mode Copy linkLink copied to clipboard!
Embed the Data Grid library in your project for in-memory data storage.
2.1. Adding the EmbeddedCacheManager Bean Copy linkLink copied to clipboard!
Add
infinispan-spring-boot-starter-embeddedto your project’s classpath to enable Embedded mode.This starter operates in Remote Client/Server mode with
infinispan-spring-boot-starter-remoteon the classpath by default.Use the Spring
@Autowiredannotation to include anEmbeddedCacheManagerbean in your Java configuration classes, as in the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow You are now ready to use Data Grid in Embedded Mode. Here is a simple example:
cacheManager.getCache("testCache").put("testKey", "testValue"); System.out.println("Received value from cache: " + cacheManager.getCache("testCache").get("testKey"));cacheManager.getCache("testCache").put("testKey", "testValue"); System.out.println("Received value from cache: " + cacheManager.getCache("testCache").get("testKey"));Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.2. Cache Manager Configuration Beans Copy linkLink copied to clipboard!
You customize the cache manager with the following configuration beans:
-
InfinispanGlobalConfigurer -
InfinispanCacheConfigurer -
Configuration -
InfinispanConfigurationCustomizer InfinispanGlobalConfigurationCustomizerNoteYou can create one
InfinispanGlobalConfigurerbean only. However you can create multiple configurations with the other beans.
InfinispanCacheConfigurer Bean
Configuration Bean
Link the bean name to the cache that it configures, as follows:
Customizer Beans
2.3. Enabling Spring Cache Support Copy linkLink copied to clipboard!
Add the @EnableCaching annotation to your application to enable Spring Cache support.
When this starter detects the EmbeddedCacheManager bean, it instantiates a new SpringEmbeddedCacheManager, which provides an implementation of Spring Cache.
3. Running in Server Mode Copy linkLink copied to clipboard!
Store and retrieve data from remote Data Grid clusters using Hot Rod, a custom TCP binary wire protocol.
3.1. Setting Up the RemoteCacheManager Copy linkLink copied to clipboard!
Provide the location for the Data Grid server so the starter can create the
RemoteCacheManagerbean.This starter first attempts to locate the server from the
hotrod-client.propertiesfile on the classpath. If not found, the starter then attempts to locate the server from yourapplication.propertiesfile.hotrod-client.properties:infinispan.client.hotrod.server_list=127.0.0.1:11222
infinispan.client.hotrod.server_list=127.0.0.1:11222Copy to Clipboard Copied! Toggle word wrap Toggle overflow application.properties:infinispan.remote.server-list=127.0.0.1:11222
infinispan.remote.server-list=127.0.0.1:11222Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Use the Spring
@Autowiredannotation to include your own custom cache manager class in your application:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.2. Cache Manager Configuration Beans Copy linkLink copied to clipboard!
Customize the cache manager with the following configuration beans:
-
InfinispanRemoteConfigurer -
Configuration InfinispanRemoteCacheCustomizerNoteYou can create one
InfinispanRemoteConfigurerbean only. However you can create multiple configurations with the other beans.
InfinispanRemoteConfigurer Bean
Configuration Bean
InfinispanRemoteCacheCustomizer Bean
@Bean
public InfinispanRemoteCacheCustomizer customizer() {
return b -> b.tcpKeepAlive(false);
}
@Bean
public InfinispanRemoteCacheCustomizer customizer() {
return b -> b.tcpKeepAlive(false);
}
Use the @Ordered annotation to apply customizers in a specific order.
3.3. Enabling Spring Cache Support Copy linkLink copied to clipboard!
Add the @EnableCaching annotation to your application to enable Spring Cache support.
When the Data Grid starter detects the RemoteCacheManager bean, it instantiates a new SpringRemoteCacheManager, which provides an implementation of Spring Cache.
3.4. Exposing Data Grid Statistics Copy linkLink copied to clipboard!
Data Grid supports the Spring Boot Actuator to expose cache statistics as metrics.
To use the Actuator, add the following to your pom.xml file:
You must then activate statistics for the appropriate cache instances, either programmatically or declaratively.
Programmatically
Declaratively
<local-cache name="my-cache" statistics="true"/>
<local-cache name="my-cache" statistics="true"/>
The Spring Boot Actuator registry binds cache instances when your application starts. If you create caches dynamically, you should use the CacheMetricsRegistrar bean to bind caches to the Actuator registry, as follows:
4. Using Spring Session Copy linkLink copied to clipboard!
4.1. Enabling Spring Session Support Copy linkLink copied to clipboard!
Data Grid Spring Session support is built on SpringRemoteCacheManager and SpringEmbeddedCacheManager. This starter produces those beans by default.
To use Spring Session in your project, do the following:
- Add this starter to your project.
- Add Spring Session to the classpath.
Add the following annotations to your configuration:
-
@EnableCaching -
@EnableInfinispanRemoteHttpSession -
@EnableInfinispanEmbeddedHttpSession
-
5. Application Properties Copy linkLink copied to clipboard!
Configure your project with application.properties or application.yaml.