Questo contenuto non è disponibile nella lingua selezionata.
Data Grid Spring Boot Starter
Data Grid Documentation Copia collegamentoCollegamento copiato negli appunti!
Abstract
1. Red Hat Data Grid Copia collegamentoCollegamento copiato negli appunti!
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.
1.1. Data Grid Documentation Copia collegamentoCollegamento copiato negli appunti!
Documentation for Data Grid is available on the Red Hat customer portal.
1.2. Data Grid Downloads Copia collegamentoCollegamento copiato negli appunti!
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.
2. Setting Up Your Project Copia collegamentoCollegamento copiato negli appunti!
Add dependencies for the Data Grid Spring Boot Starter to your project.
2.1. Enforcing Data Grid Versions Copia collegamentoCollegamento copiato negli appunti!
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.
Add infinispan-bom to your pom.xml file before the starter dependencies, as follows:
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.2. Adding Dependencies for Usage Modes Copia collegamentoCollegamento copiato negli appunti!
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>2.2.4.Final-redhat-00001</version> </dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter-embedded</artifactId>
<version>2.2.4.Final-redhat-00001</version>
</dependency>
Remote Client/Server Mode
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-spring-boot-starter-remote</artifactId> <version>2.2.4.Final-redhat-00001</version> </dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter-remote</artifactId>
<version>2.2.4.Final-redhat-00001</version>
</dependency>
3. Running in Embedded Mode Copia collegamentoCollegamento copiato negli appunti!
Embed the Data Grid library in your project for in-memory data storage.
3.1. Adding the EmbeddedCacheManager Bean Copia collegamentoCollegamento copiato negli appunti!
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
3.2. Cache Manager Configuration Beans Copia collegamentoCollegamento copiato negli appunti!
You can 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
3.3. Enabling Spring Cache Support Copia collegamentoCollegamento copiato negli appunti!
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.
4. Running in Server Mode Copia collegamentoCollegamento copiato negli appunti!
Store and retrieve data from remote Data Grid clusters using Hot Rod, a custom TCP binary wire protocol.
4.1. Setting Up the RemoteCacheManager Copia collegamentoCollegamento copiato negli appunti!
Provide the location for the Data Grid server so the starter can create the
RemoteCacheManagerbean.The starter first tries to find the server location in
hotrod-client.propertiesand then fromapplication.properties.Use the Spring
@Autowiredannotation to include your own custom cache manager class in your application:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Hot Rod client properties
Specify client configuration in hotrod-client.properties on your classpath, for example:
List Infinispan or Data Grid servers by IP address or hostname at port 11222.
# List Infinispan or Data Grid servers by IP address or hostname at port 11222.
infinispan.client.hotrod.server_list=127.0.0.1:6667
For more information, see org.infinispan.client.hotrod.configuration.
Application properties
Configure your project with application.properties. See Application Properties for more information.
4.2. Configuring Marshalling Copia collegamentoCollegamento copiato negli appunti!
Configure Data Grid servers to use Java serialization to marshall objects.
By default Data Grid server uses a ProtoStream serialization library as the default marshaller. However, the ProtoStream marshaller is not supported for Spring integration. For this reason you should use the Java Serialization Marshaller.
Specify the following properties in your
application.properties:infinispan.remote.marshaller=org.infinispan.commons.marshall.JavaSerializationMarshaller infinispan.remote.java-serial-whitelist=your_marshalled_beans_package.*
infinispan.remote.marshaller=org.infinispan.commons.marshall.JavaSerializationMarshaller1 infinispan.remote.java-serial-whitelist=your_marshalled_beans_package.*2 Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.3. Cache Manager Configuration Beans Copia collegamentoCollegamento copiato negli appunti!
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.
4.4. Enabling Spring Cache Support Copia collegamentoCollegamento copiato negli appunti!
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.
4.5. Exposing Data Grid Statistics Copia collegamentoCollegamento copiato negli appunti!
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:
5. Using Spring Session Copia collegamentoCollegamento copiato negli appunti!
5.1. Enabling Spring Session Support Copia collegamentoCollegamento copiato negli appunti!
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
-
6. Application Properties Copia collegamentoCollegamento copiato negli appunti!
Configure your project with application.properties or application.yaml.