使用带有 Spring 的 Data Grid


Red Hat Data Grid 8.5

将 Data Grid 添加到 Spring 应用程序

Red Hat Customer Content Services

摘要

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

Red Hat Data Grid

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

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

Data Grid 文档

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

Data Grid 下载

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

注意

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

使开源包含更多

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

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

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

1.1. 使用 Data Grid 设置 Spring 缓存

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

重要

Data Grid 仅支持 Spring 版本 6。

流程

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

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

      提示

      Spring Boot 用户可以添加以下工件而不是 infinispan-spring6-embedded

      • 对于 Spring Boot 3 添加 infinispan-spring-boot3-starter-embedded
  2. 配置 Hot Rod 客户端,以在 hotrod-client.properties 文件中连接到您的 Data Grid Server 部署。

    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-spring6-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-spring6-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 对象作为示例,如果您在使用 BookDao#findBook (Integer bookId) 等方法从数据库加载后缓存每个实例,您可以添加 @Cacheable 注解,如下所示:

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

在前面的示例中,当 findBook (Integer bookId) 返回 Book 实例时,它将存储在名为 books 的缓存中。

@CacheEvict

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

整个缓存驱除

使用 @CacheEvictdeleteAllBookEntries () 方法添加 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 提供程序默认为在执行读写操作时阻止行为。缓存操作是同步的,不会超时。

如果需要,您可以配置最长时间,以等待操作在超时前完成。

流程

  • 在 Spring embedded CacheManagerFactoryBean 或 SpringRemoteCacheManagerFactoryBean 上的应用程序上下文 XML 中配置以下超时属性。

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

Expand
属性描述

infinispan.spring.operation.read.timeout

指定用于等待读取操作完成的时间(以毫秒为单位)。默认值为 0, 这意味着无限等待时间。

infinispan.spring.operation.write.timeout

指定用于等待写入操作完成的时间(以毫秒为单位)。默认值为 0, 这意味着无限等待时间。

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

<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-spring6-embedded
    • 远程缓存: infinispan-spring6-remote

      以下示例是远程缓存:

      <dependencies>
          <dependency>
              <groupId>org.infinispan</groupId>
              <artifactId>infinispan-core</artifactId>
          </dependency>
          <dependency>
              <groupId>org.infinispan</groupId>
              <artifactId>infinispan-spring6-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 实例。

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

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

      这些注解有可选参数:

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

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

@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 © 2024 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