このコンテンツは選択した言語では利用できません。
Chapter 2. Using Remote Caches
Store and retrieve data from remote Data Grid clusters using Hot Rod, a custom TCP binary wire protocol.
2.1. Setting Up the RemoteCacheManager リンクのコピーリンクがクリップボードにコピーされました!
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:private final RemoteCacheManager cacheManager; @Autowired public YourClassName(RemoteCacheManager cacheManager) { this.cacheManager = cacheManager; }
2.2. Using the reactive mode with Reactor リンクのコピーリンクがクリップボードにコピーされました!
Starting with Spring 6.1, reactive mode is supported to make use of caching within reactive applications. If you use spring-boot-starter-webflux, your application may block.
To enable the Data Grid reactive driver, specify the following property in application.properties:
infinispan.remote.reactive=true
2.2.1. Properties Files リンクのコピーリンクがクリップボードにコピーされました!
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.
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.
infinispan.remote.server-list=127.0.0.1:11222
2.3. Configuring Marshalling リンクのコピーリンクがクリップボードにコピーされました!
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.ProtoStreamMarshallerinfinispan.remote.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshallerAdd 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.remote.java-serial-allowlist=your_marshalled_beans_package.*
- Save and close your properties file.
2.4. Configuring and creating caches on startup from the client リンクのコピーリンクがクリップボードにコピーされました!
Configure Data Grid to create caches on first access from the client.
Procedure
-
Open
hotrod-client.propertiesorapplication.propertiesorapplication.yamlfor editing. Do one of the following:
infinispan.client.hotrod.cache.mycache.template_name=mytemplate1 infinispan.client.hotrod.cache.mycache.force_return_values=true infinispan.client.hotrod.cache.mycache.marshaller=org.infinispan.commons.marshall.JavaSerializationMarshaller infinispan.client.hotrod.cache.[com.myproject.mycache].template_name=mytemplate1 infinispan.client.hotrod.cache.[com.myproject.mycache].marshaller=org.infinispan.commons.marshall.UTF8StringMarshaller infinispan.client.hotrod.cache.[com.myproject.mycache].near_cache.mode=INVALIDATED infinispan.client.hotrod.cache.[com.myproject.*].template_name=mytemplate2 infinispan.client.hotrod.cache.[com.myproject.*].transaction.transaction_mode=NON_XAinfinispan.remote.cache.example.configuration=<distributed-cache/> infinispan.remote.cache.example.force-return-values=true infinispan.remote.cache.mycache.template-name=myTemplate infinispan.remote.cache.mycache.marshaller=org.infinispan.commons.marshall.JavaSerializationMarshaller infinispan.remote.cache.[com.myproject.mycache].template-name=myCustomTemplate infinispan.remote.cache.[com.myproject.mycache].marshaller=org.infinispan.commons.marshall.UTF8StringMarshaller infinispan.remote.cache.[com.myproject.mycache].near-cache.mode=INVALIDATED infinispan.remote.cache.[com.myproject.*].template-name=myCustomTemplate infinispan.remote.cache.[com.myproject.*].transaction.transaction-mode=NON_XA# Infinispan Configuration infinispan: remote: # Basic Connection Settings server-list: "180.567.112.333:6668" # Cache Configurations cache: example: configuration: "<distributed-cache/>" force-return-values: true mycache: template-name: "myTemplate" marshaller: "org.infinispan.commons.marshall.JavaSerializationMarshaller" "[com.mycompany.mycache]": template-name: "mytemplate2" marshaller: "org.infinispan.commons.marshall.UTF8StringMarshaller" near-cache: mode: "INVALIDATED" "[com.mycompany.*]": template-name: "mytemplate2" transaction: transaction-mode: "NON_XA"- Save and close your properties or yaml file.
2.5. Cache Manager Configuration Beans リンクのコピーリンクがクリップボードにコピーされました!
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
@Bean
public InfinispanRemoteConfigurer infinispanRemoteConfigurer() {
return () -> new ConfigurationBuilder()
.addServer()
.host("127.0.0.1")
.port(12345)
.build();
}
Configuration Bean
@Bean
public org.infinispan.client.hotrod.configuration.Configuration customConfiguration() {
new ConfigurationBuilder()
.addServer()
.host("127.0.0.1")
.port(12345)
.build();
}
InfinispanRemoteCacheCustomizer Bean
@Bean
public InfinispanRemoteCacheCustomizer customizer() {
return b -> b.tcpKeepAlive(false);
}
Use the @Ordered annotation to apply customizers in a specific order.
2.6. Enabling Spring Cache Support リンクのコピーリンクがクリップボードにコピーされました!
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
2.7. Exposing Data Grid Statistics リンクのコピーリンクがクリップボードにコピーされました!
Data Grid supports the Spring Boot Actuator to expose cache statistics as metrics.
Procedure
Add the following to your
pom.xmlfile:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>${version.spring.boot}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${version.spring.boot}</version> </dependency>Activate statistics for the appropriate cache instances, either programmatically or declaratively.
Programmatically
@Bean public InfinispanCacheConfigurer cacheConfigurer() { return cacheManager -> { final org.infinispan.configuration.cache.Configuration config = new ConfigurationBuilder() .jmxStatistics().enable() .build(); cacheManager.defineConfiguration("my-cache", config); }; }Declaratively
<local-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:
@Autowire
CacheMetricsRegistrar cacheMetricsRegistrar;
@Autowire
CacheManager cacheManager;
...
cacheMetricsRegistrar.bindCacheToRegistry(cacheManager.getCache("my-cache"));