Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Data Grid Spring Boot Starter
Use Data Grid with your Spring Boot project
Abstract
Red Hat Data Grid Link kopierenLink in die Zwischenablage kopiert!
Data Grid is a high-performance, distributed in-memory data store.
- Schemaless data structure
- Flexibility to store different objects as key-value pairs.
- Grid-based data storage
- Designed to distribute and replicate data across clusters.
- Elastic scaling
- Dynamically adjust the number of nodes to meet demand without service disruption.
- Data interoperability
- Store, retrieve, and query data in the grid from different endpoints.
Data Grid documentation Link kopierenLink in die Zwischenablage kopiert!
Documentation for Data Grid is available on the Red Hat customer portal.
Data Grid downloads Link kopierenLink in die Zwischenablage kopiert!
Access the Data Grid Software Downloads on the Red Hat customer portal.
You must have a Red Hat account to access and download Data Grid software.
Making open source more inclusive Link kopierenLink in die Zwischenablage kopiert!
Red Hat is committed to replacing problematic language in our code, documentation, and web properties. We are beginning with these four terms: master, slave, blacklist, and whitelist. Because of the enormity of this endeavor, these changes will be implemented gradually over several upcoming releases. For more details, see our CTO Chris Wright’s message.
Chapter 1. Setting Up Your Project Link kopierenLink in die Zwischenablage kopiert!
Add dependencies for the Data Grid Spring Boot Starter to your project.
1.1. Enforcing Data Grid Versions Link kopierenLink in die Zwischenablage kopiert!
This starter uses a high-level API to ensure compatibility between major versions of Data Grid. However you can enforce a specific version of Data Grid with the infinispan-bom module.
Procedure
Add
infinispan-bomto yourpom.xmlfile before the starter dependencies.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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.
1.2. Adding Dependencies for Usage Modes Link kopierenLink in die Zwischenablage kopiert!
Data Grid provides different dependencies for embedded caches and remote caches.
Procedure
-
Add one of the following to your
pom.xmlfile:
Embedded caches
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-spring-boot-starter-embedded</artifactId> </dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter-embedded</artifactId>
</dependency>
Remote caches
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-spring-boot-starter-remote</artifactId> </dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter-remote</artifactId>
</dependency>
Chapter 2. Using Embedded Caches Link kopierenLink in die Zwischenablage kopiert!
Embed Data Grid caches directly in your project for in-memory data storage.
2.1. Adding the EmbeddedCacheManager Bean Link kopierenLink in die Zwischenablage kopiert!
Configure your application to use embedded caches.
Procedure
-
Add
infinispan-spring-boot-starter-embeddedto your project’s classpath to enable Embedded mode. 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 caches directly within your application, as in the following 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"));
2.2. Cache Manager Configuration Beans Link kopierenLink in die Zwischenablage kopiert!
You can customize the cache manager with the following configuration beans:
-
InfinispanGlobalConfigurer -
InfinispanCacheConfigurer -
Configuration -
InfinispanConfigurationCustomizer -
InfinispanGlobalConfigurationCustomizer
You can create one InfinispanGlobalConfigurer bean 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 Link kopierenLink in die Zwischenablage kopiert!
With both embedded and remote caches, Data Grid provides an implementation of Spring Cache that you can enable.
Procedure
-
Add the
@EnableCachingannotation to your application.
If the Data Grid starter detects the:
-
EmbeddedCacheManagerbean, it instantiates a newSpringEmbeddedCacheManager. -
RemoteCacheManagerbean, it instantiates a newSpringRemoteCacheManager.
Reference
Chapter 3. Using Remote Caches Link kopierenLink in die Zwischenablage kopiert!
Store and retrieve data from remote Data Grid clusters using Hot Rod, a custom TCP binary wire protocol.
3.1. Setting Up the RemoteCacheManager Link kopierenLink in die Zwischenablage kopiert!
Configure your application to use remote caches on Data Grid clusters.
-
Provide the addresses where Data Grid Server listens for client connections so the starter can create the
RemoteCacheManagerbean. 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.1.1. Properties Files Link kopierenLink in die Zwischenablage kopiert!
You can specify properties in either hotrod-client.properties or application.properties.
Properties can be in both properties files but the starter applies the configuration in hotrod-client.properties first, which means that file takes priority over application.properties.
hotrod-client.properties
Properties in this file take the format of infinispan.client.hotrod.*, for example:
List Data Grid servers by IP address or hostname at port localhost:11222.
# List Data Grid servers by IP address or hostname at port localhost:11222.
infinispan.client.hotrod.server_list=127.0.0.1:11222
application.properties
Properties in this file take the format of infinispan.remote.*, for example:
List Data Grid servers by IP address or hostname at port localhost:11222.
# List Data Grid servers by IP address or hostname at port localhost:11222.
infinispan.remote.server-list=127.0.0.1:11222
3.2. Configuring Marshalling Link kopierenLink in die Zwischenablage kopiert!
Configure Data Grid to marshall Java objects into binary format so they can be transferred over the wire or stored to disk.
By default Data Grid uses a Java Serialization marshaller, which requires you to add your classes to an allow list. As an alternative you can use ProtoStream, which requires you to annotate your classes and generate a SerializationContextInitializer for custom Java objects.
Procedure
-
Open
hotrod-client.propertiesorapplication.propertiesfor editing. Do one of the following:
Use ProtoStream as the marshaller.
infinispan.client.hotrod.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshaller
infinispan.client.hotrod.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshallerCopy to Clipboard Copied! Toggle word wrap Toggle overflow infinispan.remote.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshaller
infinispan.remote.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshallerCopy to Clipboard Copied! Toggle word wrap Toggle overflow Add your classes to the serialization allow list if you use Java Serialization. You can specify a comma-separated list of fully qualified class names or a regular expression to match classes.
infinispan.client.hotrod.java_serial_allowlist=your_marshalled_beans_package.*
infinispan.client.hotrod.java_serial_allowlist=your_marshalled_beans_package.*Copy to Clipboard Copied! Toggle word wrap Toggle overflow infinispan.remote.java-serial-allowlist=your_marshalled_beans_package.*
infinispan.remote.java-serial-allowlist=your_marshalled_beans_package.*Copy to Clipboard Copied! Toggle word wrap Toggle overflow
- Save and close your properties file.
3.3. Cache Manager Configuration Beans Link kopierenLink in die Zwischenablage kopiert!
Customize the cache manager with the following configuration beans:
-
InfinispanRemoteConfigurer -
Configuration -
InfinispanRemoteCacheCustomizer
You can create one InfinispanRemoteConfigurer bean 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.4. Enabling Spring Cache Support Link kopierenLink in die Zwischenablage kopiert!
With both embedded and remote caches, Data Grid provides an implementation of Spring Cache that you can enable.
Procedure
-
Add the
@EnableCachingannotation to your application.
If the Data Grid starter detects the:
-
EmbeddedCacheManagerbean, it instantiates a newSpringEmbeddedCacheManager. -
RemoteCacheManagerbean, it instantiates a newSpringRemoteCacheManager.
Reference
3.5. Exposing Data Grid Statistics Link kopierenLink in die Zwischenablage kopiert!
Data Grid supports the Spring Boot Actuator to expose cache statistics as metrics.
Procedure
Add the following to your
pom.xmlfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Activate statistics for the appropriate cache instances, either programmatically or declaratively.
Programmatically
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Declaratively
<local-cache statistics="true"/>
<local-cache statistics="true"/>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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:
Chapter 4. Using Spring Session Link kopierenLink in die Zwischenablage kopiert!
4.1. Enabling Spring Session Support Link kopierenLink in die Zwischenablage kopiert!
Data Grid Spring Session support is built on SpringRemoteCacheManager and SpringEmbeddedCacheManager. The Data Grid starter produces those beans by default.
Procedure
- Add this starter to your project.
- Add Spring Session to the classpath.
Add the following annotations to your configuration:
-
@EnableCaching -
@EnableInfinispanRemoteHttpSession -
@EnableInfinispanEmbeddedHttpSession
-
Data Grid does not provide a default cache. To use Spring Session, you must first create a Data Grid cache.
Chapter 5. Application Properties Link kopierenLink in die Zwischenablage kopiert!
Configure your project with application.properties or application.yaml.