Chapter 3. Using Remote Caches
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!
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
RemoteCacheManager
bean. Use the Spring
@Autowired
annotation 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 Copy linkLink copied to clipboard!
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 11222.
# List Data Grid servers by IP address or hostname at port 11222.
infinispan.client.hotrod.server_list=127.0.0.1:6667
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 11222.
# List Data Grid servers by IP address or hostname at port 11222.
infinispan.remote.server-list=127.0.0.1:6667
3.2. Configuring Marshalling Copy linkLink copied to clipboard!
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.properties
orapplication.properties
for 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.ProtoStreamMarshaller
Copy to Clipboard Copied! Toggle word wrap Toggle overflow infinispan.remote.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshaller
infinispan.remote.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshaller
Copy 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 Copy linkLink copied to clipboard!
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 Copy linkLink copied to clipboard!
With both embedded and remote caches, Data Grid provides an implementation of Spring Cache that you can enable.
Procedure
-
Add the
@EnableCaching
annotation to your application.
If the Data Grid starter detects the:
-
EmbeddedCacheManager
bean, it instantiates a newSpringEmbeddedCacheManager
. -
RemoteCacheManager
bean, it instantiates a newSpringRemoteCacheManager
.
Reference
3.5. Exposing Data Grid Statistics Copy linkLink copied to clipboard!
Data Grid supports the Spring Boot Actuator to expose cache statistics as metrics.
Procedure
Add the following to your
pom.xml
file: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 name="mycache" statistics="true"/>
<local-cache name="mycache" 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: