이 콘텐츠는 선택한 언어로 제공되지 않습니다.

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

Configure your application to use remote caches on Data Grid clusters.

  1. Provide the addresses where Data Grid Server listens for client connections so the starter can create the RemoteCacheManager bean.
  2. Use the Spring @Autowired annotation to include your own custom cache manager class in your application:

    private final RemoteCacheManager cacheManager;
    
    @Autowired
    public YourClassName(RemoteCacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }
    Copy to Clipboard Toggle word wrap

3.1.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 11222.
infinispan.client.hotrod.server_list=127.0.0.1:6667
Copy to Clipboard Toggle word wrap
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.
infinispan.remote.server-list=127.0.0.1:6667
Copy to Clipboard Toggle word wrap

3.2. 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

  1. Open hotrod-client.properties or application.properties for editing.
  2. Do one of the following:

    • Use ProtoStream as the marshaller.

      infinispan.client.hotrod.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshaller
      Copy to Clipboard Toggle word wrap
      infinispan.remote.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshaller
      Copy to Clipboard Toggle word wrap
    • 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.*
      Copy to Clipboard Toggle word wrap
      infinispan.remote.java-serial-allowlist=your_marshalled_beans_package.*
      Copy to Clipboard Toggle word wrap
  3. Save and close your properties file.

3.3. Cache Manager Configuration Beans

Customize the cache manager with the following configuration beans:

  • InfinispanRemoteConfigurer
  • Configuration
  • InfinispanRemoteCacheCustomizer
Note

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();
}
Copy to Clipboard Toggle word wrap

Configuration Bean

@Bean
public org.infinispan.client.hotrod.configuration.Configuration customConfiguration() {
    new ConfigurationBuilder()
        .addServer()
        .host("127.0.0.1")
        .port(12345)
        .build();
}
Copy to Clipboard Toggle word wrap

InfinispanRemoteCacheCustomizer Bean

@Bean
public InfinispanRemoteCacheCustomizer customizer() {
    return b -> b.tcpKeepAlive(false);
}
Copy to Clipboard Toggle word wrap

Tip

Use the @Ordered annotation to apply customizers in a specific order.

3.4. 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 @EnableCaching annotation to your application.

If the Data Grid starter detects the:

  • EmbeddedCacheManager bean, it instantiates a new SpringEmbeddedCacheManager.
  • RemoteCacheManager bean, it instantiates a new SpringRemoteCacheManager.

3.5. Exposing Data Grid Statistics

Data Grid supports the Spring Boot Actuator to expose cache statistics as metrics.

Procedure

  1. Add the following to your pom.xml file:

    <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>
    Copy to Clipboard Toggle word wrap
  2. 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);
      };
    }
    Copy to Clipboard Toggle word wrap

    Declaratively

    <local-cache name="mycache" statistics="true"/>
    Copy to Clipboard Toggle word wrap

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"));
Copy to Clipboard Toggle word wrap
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat