在 Spring 中使用 Data Grid


Red Hat Data Grid 8.3

在 Spring 应用程序中添加 Data Grid

Red Hat Customer Content Services

摘要

为基于 Spring 的应用程序添加 Data Grid 缓存功能。

Red Hat Data Grid

Data Grid 是一个高性能分布式内存数据存储。

无架构数据结构
将不同对象存储为键值对的灵活性。
基于网格的数据存储
旨在在集群中分发和复制数据。
弹性扩展
动态调整节点数量,以便在不中断服务的情况下满足需求。
数据互操作性
从不同端点在网格中存储、检索和查询数据。

Data Grid 文档

红帽客户门户网站中提供了 Data Grid 的文档。

Data Grid 下载

访问 红帽客户门户上的 Data Grid 软件下载。

注意

您必须有一个红帽帐户才能访问和下载数据中心软件。

使开源包含更多

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。我们从这四个术语开始:master、slave、黑名单和白名单。由于此项工作十分艰巨,这些更改将在即将推出的几个发行版本中逐步实施。有关更多详情,请参阅我们的首席技术官 Chris Wright 提供的消息

第 1 章 使用 Data Grid 作为 Spring Cache 供应商

在应用程序中添加 Data Grid 依赖项,并使用 Spring Cache 注解将数据存储在嵌入式或远程缓存中。

1.1. 使用 Data Grid 设置 Spring 缓存

将 Data Grid 依赖项添加到您的 Spring 应用程序项目中。如果您在 Data Grid 服务器部署中使用远程缓存,您还应配置 Hot Rod 客户端属性。

流程

  1. 将 Data Grid 和 Spring 集成模块添加到您的 pom.xml 中。

    • 远程缓存: infinispan-spring5-remote
    • 嵌入式缓存: infinispan-spring5-embedded

      提示

      Spring Boot 用户可以添加 infinispan-spring-boot-starter-embedded 而不是 infinispan-spring5-embedded 工件。

  2. 配置 Hot Rod 客户端,以在 hotrod-client.properties 文件中连接到您的 Data Grid 服务器部署。

    infinispan.client.hotrod.server_list = 127.0.0.1:11222
    infinispan.client.hotrod.auth_username=admin
    infinispan.client.hotrod.auth_password=changeme
    Copy to Clipboard Toggle word wrap
Spring Cache 依赖项

远程缓存

<dependencies>
    <dependency>
        <groupId>org.infinispan</groupId>
        <artifactId>infinispan-spring5-remote</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${version.spring}</version>
    </dependency>
</dependencies>
Copy to Clipboard Toggle word wrap

嵌入式缓存

<dependencies>
    <dependency>
        <groupId>org.infinispan</groupId>
        <artifactId>infinispan-spring5-embedded</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${version.spring}</version>
    </dependency>
</dependencies>
Copy to Clipboard Toggle word wrap

1.2. 使用 Data Grid 作为 Spring Cache 供应商

@EnableCaching 注释添加到您的其中一个配置类,然后添加 @Cacheable@CacheEvict 注释以使用远程或嵌入式缓存。

先决条件

  • 将 Data Grid 依赖项添加到应用程序项目中。
  • 如果您使用 Data Grid Server 部署,请创建所需的远程缓存并配置 Hot Rod 客户端属性。

流程

  1. 使用以下方法之一在应用程序上下文中启用缓存注解:

    声明

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    
            <cache:annotation-driven />
    
    </beans>
    Copy to Clipboard Toggle word wrap

    programmatic

    @EnableCaching @Configuration
    public class Config {
    }
    Copy to Clipboard Toggle word wrap

  2. 使用 @Cacheable 注解方法来缓存返回值。

    提示

    要直接引用缓存中的条目,您必须包含 key 属性。

  3. 使用 @CacheEvict 注解方法,以从缓存中删除旧条目。

1.3. Spring Cache 注解

@Cacheable@CacheEvict 注释向方法添加缓存功能。

@cacheable
将返回值存储在缓存中。
@CacheEvict
通过删除旧条目来控制缓存大小。
@cacheable

将书签对象作为示例,在从数据库中加载每个实例时(如 Book Dao findfindBook (Integer bookId) ),您可以添加 @Cacheable 注解,如下所示:

@Transactional
@Cacheable(value = "books", key = "#bookId")
public Book findBook(Integer bookId) {...}
Copy to Clipboard Toggle word wrap

在上例中,当 找到书签(Integer bookId) 时,它会返回一个书签实例,它存储在名为 Book 的缓存中

@CacheEvict

使用 @CacheEvict 注释,您可以指定您是否想驱除整个载缓存,或者只驱除与特定 一 个bookId 匹配的条目。

整个缓存驱除

使用 @CacheEvict 注解 deleteAllBookEntries () 方法并添加 allEntries 参数,如下所示:

@Transactional
@CacheEvict (value="books", key = "#bookId", allEntries = true)
public void deleteAllBookEntries() {...}
Copy to Clipboard Toggle word wrap

基于条目的驱除

使用 @CacheEvict 注解 deleteBook (Integer bookId) 方法,并指定与该条目关联的键,如下所示:

@Transactional
@CacheEvict (value="books", key = "#bookId")
public void deleteBook(Integer bookId) {...}
Copy to Clipboard Toggle word wrap

1.4. 为缓存操作配置超时

在执行读写操作时,Data Grid Spring Cache 供应商默认为阻止行为。缓存操作是同步的,且不会超时。

如果需要,您可以配置等待操作完成的最长时间,然后再超时。

流程

  • SpringEmbeddedCacheManagerFactoryBeanSpringRemoteCacheManagerFactoryBean 上为应用程序配置以下超时属性。

    对于远程缓存,您还可以将这些属性添加到 hotrod-client.properties 文件中。

Expand
属性描述

infinispan.spring.operation.read.timeout

以毫秒为单位指定等待读取操作完成的时间(以毫秒为单位)。默认值为 0, 表示无限等待时间。

infinispan.spring.operation.write.timeout

指定等待写入操作完成的时间(以毫秒为单位)。默认值为 0, 表示无限等待时间。

以下示例显示了 SpringRemoteCacheManagerFactoryBean 上下文 XML 中的 timeout 属性:

<bean id="springRemoteCacheManagerConfiguredUsingConfigurationProperties"
      class="org.infinispan.spring.remote.provider.SpringRemoteCacheManagerFactoryBean">
    <property name="configurationProperties">
        <props>
           <prop key="infinispan.spring.operation.read.timeout">500</prop>
           <prop key="infinispan.spring.operation.write.timeout">700</prop>
        </props>
    </property>
</bean>
Copy to Clipboard Toggle word wrap

第 2 章 使用 Spring Session 进行外部化会话

将 Spring 应用程序的会话数据存储在 Data Grid 缓存中,并独立于容器。

2.1. 使用 Spring Session 进行外部化会话

使用 Spring Session API 将会话数据外部化到 Data Grid。

流程

  1. 在您的 pom.xml 中添加依赖项。

    • 嵌入式缓存: infinispan-spring5-embedded
    • 远程缓存: infinispan-spring5-remote

      以下示例用于远程缓存:

      <dependencies>
          <dependency>
              <groupId>org.infinispan</groupId>
              <artifactId>infinispan-core</artifactId>
          </dependency>
          <dependency>
              <groupId>org.infinispan</groupId>
              <artifactId>infinispan-spring5-remote</artifactId>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>${version.spring}</version>
          </dependency>
          <dependency>
             <groupId>org.springframework.session</groupId>
             <artifactId>spring-session-core</artifactId>
             <version>${version.spring}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-web</artifactId>
             <version>${version.spring}</version>
         </dependency>
      </dependencies>
      Copy to Clipboard Toggle word wrap
  2. 指定适当的 FactoryBean 来公开 CacheManager 实例。

    • 嵌入式缓存: SpringEmbededCacheManagerFactoryBean
    • 远程缓存: SpringRemoteCacheManagerFactoryBean
  3. 使用适当的注解启用 Spring Session。

    • 嵌入式缓存 :@EnableInfinispanEmbeddedHttpSession
    • 远程缓存 :@EnableInfinispanRemoteHttpSession

      这些注解具有可选参数:

      • maxInactiveIntervalInSeconds 以秒为单位设置会话过期时间。默认值为 1800
      • cacheName 指定存储会话的缓存名称。默认值为 会话

以下示例显示了基于注解的完整配置:

@EnableInfinispanEmbeddedHttpSession
@Configuration
public class Config {

   @Bean
   public SpringEmbeddedCacheManagerFactoryBean springCacheManager() {
      return new SpringEmbeddedCacheManagerFactoryBean();
   }

   //An optional configuration bean responsible for replacing the default
   //cookie that obtains configuration.
   //For more information refer to the Spring Session documentation.
   @Bean
   public HttpSessionIdResolver httpSessionIdResolver() {
       return HeaderHttpSessionIdResolver.xAuthToken();
   }
}
Copy to Clipboard Toggle word wrap

法律通告

Copyright © 2023 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部