Data Grid Spring Boot Starter
将 Data Grid 与 Spring Boot 项目搭配使用
摘要
Red Hat Data Grid 复制链接链接已复制到粘贴板!
Data Grid 是一个高性能分布式内存数据存储。
- 无架构数据结构
- 将不同对象存储为键值对的灵活性。
- 基于网格的数据存储
- 旨在在集群中分发和复制数据。
- 弹性扩展
- 动态调整节点数量,以便在不中断服务的情况下满足需求。
- 数据互操作性
- 从不同端点在网格中存储、检索和查询数据。
Data Grid 文档 复制链接链接已复制到粘贴板!
红帽客户门户网站中提供了 Data Grid 的文档。
Data Grid 下载 复制链接链接已复制到粘贴板!
访问红帽客户门户上的 Data Grid 软件下载。
您必须有一个红帽帐户才能访问和下载数据中心软件。
使开源包含更多 复制链接链接已复制到粘贴板!
红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。我们从这四个术语开始:master、slave、黑名单和白名单。由于此项工作十分艰巨,这些更改将在即将推出的几个发行版本中逐步实施。详情请查看 CTO Chris Wright 的信息。
第 1 章 使用嵌入式缓存 复制链接链接已复制到粘贴板!
在项目中直接嵌入 Data Grid 缓存,以进行内存数据存储。
1.1. 添加 EmbeddedCacheManager Bean 复制链接链接已复制到粘贴板!
配置您的应用程序以使用嵌入式缓存。
流程
-
将
infinispan-spring-boot3-starter-embedded添加到项目的 classpath 中,以启用嵌入式模式。 使用 Spring
@Autowired注解在 Java 配置类中包含EmbededCacheManagerbean,如下例所示:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
现在,您可以在应用程序中直接使用 Data Grid 缓存,如下例所示:
cacheManager.getCache("testCache").put("testKey", "testValue");
System.out.println("Received value from cache: " + cacheManager.getCache("testCache").get("testKey"));
cacheManager.getCache("testCache").put("testKey", "testValue");
System.out.println("Received value from cache: " + cacheManager.getCache("testCache").get("testKey"));
1.2. 使用带有 Reactor 的被动模式 复制链接链接已复制到粘贴板!
从 Spring 6.1 开始,支持被动模式在被动应用程序中使用缓存。如果使用 spring-boot-starter-webflux,则您的应用程序可能会阻止。
要启用 Data Grid 被动驱动程序,请在 application.properties 中指定以下属性:
infinispan.embedded.reactive=true
infinispan.embedded.reactive=true
1.3. 缓存管理器配置 Beans 复制链接链接已复制到粘贴板!
您可以使用以下配置 Bean 自定义 Cache Manager:
-
InfinispanGlobalConfigurer -
InfinispanCacheConfigurer -
配置 -
InfinispanConfigurationCustomizer -
InfinispanGlobalConfigurationCustomizer
您只能创建一个 InfinispanGlobalConfigurer bean。但是,您可以使用其他 Bean 创建多个配置。
InfinispanCacheConfigurer Bean
配置 Bean
将 bean 名称链接到它配置的缓存,如下所示:
Customizer Beans
1.4. 启用 Spring 缓存支持 复制链接链接已复制到粘贴板!
通过嵌入式和远程缓存,Data Grid 提供了可以启用的 Spring Cache 的实现。
流程
-
将
@EnableCaching注释添加到您的应用程序。
如果 Data Grid starter 检测到:
-
EmbeddedCacheManagerbean,它会实例化一个新的SpringEmbeddedCacheManager。 -
RemoteCacheManagerbean,它实例化一个新的SpringRemoteCacheManager。
第 2 章 使用远程缓存 复制链接链接已复制到粘贴板!
使用 Hot Rod (一个自定义 TCP 二进制线协议)从远程 Data Grid 集群存储和检索数据。
2.1. 设置 RemoteCacheManager 复制链接链接已复制到粘贴板!
将应用程序配置为在 Data Grid 集群上使用远程缓存。
-
提供 Data Grid 服务器侦听客户端连接的地址,以便启动程序可以创建
RemoteCacheManagerbean。 使用 Spring
@Autowired注释在应用程序中包含您自己的自定义缓存管理器类:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.2. 使用带有 Reactor 的被动模式 复制链接链接已复制到粘贴板!
从 Spring 6.1 开始,支持被动模式在被动应用程序中使用缓存。如果使用 spring-boot-starter-webflux,则您的应用程序可能会阻止。
要启用 Data Grid 被动驱动程序,请在 application.properties 中指定以下属性:
infinispan.remote.reactive=true
infinispan.remote.reactive=true
2.2.1. 属性文件 复制链接链接已复制到粘贴板!
您可以在 hotrod-client.properties 或 application.properties 中指定属性。
属性可以在这两个属性文件中,但启动程序首先应用 hotrod-client.properties 中的配置,这意味着文件优先于 application.properties。
hotrod-client.properties
此文件中的属性的格式采用 infinispan.client.hotrodö 的格式,例如:
# List Data Grid servers by IP address or hostname at port localhost:11222. infinispan.client.hotrod.server_list=127.0.0.1:11222
# 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
此文件中的属性的格式采用 infinispan.remote.* 格式,例如:
# List Data Grid servers by IP address or hostname at port localhost:11222. infinispan.remote.server-list=127.0.0.1:11222
# List Data Grid servers by IP address or hostname at port localhost:11222.
infinispan.remote.server-list=127.0.0.1:11222
2.3. 配置 Marshalling 复制链接链接已复制到粘贴板!
配置 Data Grid 对 Java 对象进行 marshall 处理生成二进制格式,以便它们可以进行有线传输或存储到磁盘。
默认情况下,Data Grid 使用 Java Serialization marshaller,这需要将您的类添加到允许列表中。作为替代方案,您可以使用 ProtoStream,这需要注解类并为自定义 Java 对象生成 SerializationContextInitializer。
流程
-
打开
hotrod-client.properties或application.properties进行编辑。 执行以下操作之一:
使用 ProtoStream 作为 marshaller。
infinispan.client.hotrod.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshaller
infinispan.client.hotrod.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshallerCopy to Clipboard Copied! Toggle word wrap Toggle overflow infinispan.remote.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshaller
infinispan.remote.marshaller=org.infinispan.commons.marshall.ProtoStreamMarshallerCopy to Clipboard Copied! Toggle word wrap Toggle overflow 如果使用 Java 序列化,请将类添加到序列化允许列表中。您可以指定以逗号分隔的完全限定类名称或正则表达式列表来匹配类。
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
- 保存并关闭您的属性文件。
2.4. 缓存管理器配置 Beans 复制链接链接已复制到粘贴板!
使用以下配置 Bean 自定义缓存管理器:
-
InfinispanRemoteConfigurer -
配置 -
InfinispanRemoteCacheCustomizer
您只能创建一个 InfinispanRemoteConfigurer bean。但是,您可以使用其他 Bean 创建多个配置。
InfinispanRemoteConfigurer Bean
配置 Bean
InfinispanRemoteCacheCustomizer Bean
@Bean
public InfinispanRemoteCacheCustomizer customizer() {
return b -> b.tcpKeepAlive(false);
}
@Bean
public InfinispanRemoteCacheCustomizer customizer() {
return b -> b.tcpKeepAlive(false);
}
使用 @Ordered 注释以特定顺序应用自定义器。
2.5. 启用 Spring 缓存支持 复制链接链接已复制到粘贴板!
通过嵌入式和远程缓存,Data Grid 提供了可以启用的 Spring Cache 的实现。
流程
-
将
@EnableCaching注释添加到您的应用程序。
如果 Data Grid starter 检测到:
-
EmbeddedCacheManagerbean,它会实例化一个新的SpringEmbeddedCacheManager。 -
RemoteCacheManagerbean,它实例化一个新的SpringRemoteCacheManager。
2.6. 公开数据网格统计 复制链接链接已复制到粘贴板!
Data Grid 支持 Spring Boot Actuator 将缓存统计信息作为指标公开。
流程
在您的
pom.xml文件中添加以下内容:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 以编程方式或声明性方式激活相应缓存实例的统计信息。
以编程方式
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 声明性
<local-cache statistics="true"/>
<local-cache statistics="true"/>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Spring Boot Actuator registry 在应用程序启动时绑定缓存实例。
如果您动态创建缓存,您应该使用 CacheMetricsRegistrar bean 将缓存绑定到 Actuator registry,如下所示:
第 3 章 使用 Spring 会话 复制链接链接已复制到粘贴板!
3.1. 启用 Spring 会话支持 复制链接链接已复制到粘贴板!
Data Grid Spring Session 支持基于 SpringRemoteCacheManager 和 SpringEmbeddedCacheManager 构建。Data Grid starter 默认生成这些 Bean。
流程
- 将此启动程序添加到您的项目中。
- 将 Spring Session 添加到类路径。
在您的配置中添加以下注解:
-
@EnableCaching -
@EnableInfinispanRemoteHttpSession -
@EnableInfinispanEmbeddedHttpSession
-
Data Grid 不提供默认缓存。要使用 Spring Session,您必须首先创建一个 Data Grid 缓存。
第 4 章 应用程序属性 复制链接链接已复制到粘贴板!
使用 application.properties 或 application.yaml 配置您的项目。