首页 / 操作系统 / Linux / Infinispan 8 中新的 Redis 缓存存储实现
Infinispan 8 包含了一个新的在 Redis k/v 服务器中存储缓存数据的 cache store。这个 cache store 可以把缓存数据存储在一个集中的 Redis 中,所有的 Infinispan 客户端都可以访问。Cache store 支持三种 Redis 的部署方式:单服务器、主从切换(Sentinel)和集群(需要 Redis 3 支持)。目前支持的 Redis 版本包括 2.8+ 和 3.0+。数据过期和清理由 Redis 负责,可以减轻 Infinispan 服务器人工删除 cache 项的工作量。拓扑结构独立服务器对于单服务器部署,cache store 会指向所连的 Redis 的 master,将其作为数据的存储。使用这种结构,Redis 是没有容灾功能的,除非在它上面另外再自己构造一个。下面是独立服务器的本地cache store 的配置:<?xml version="1.0" encoding="UTF-8"?>
<infinispan
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:8.0 http://www.infinispan.org/schemas/infinispan-config-8.0.xsd
urn:infinispan:config:store:redis:8.0
http://www.infinispan.org/schemas/infinispan-cachestore-redis-config-8.0.xsd"
xmlns="urn:infinispan:config:8.0"
xmlns:redis="urn:infinispan:config:store:redis:8.0" >
<cache-container>
<local-cache>
<persistence passivation="false">
<redis-store xmlns="urn:infinispan:config:store:redis:8.0"
topology="server" socket-timeout="10000" connection-timeout="10000">
<redis-server host="server1" />
<connection-pool min-idle="6" max-idle="10" max-total="20"
min-evictable-idle-time="30000" time-between-eviction-runs="30000" />
</redis-store>
</persistence>
</local-cache>
</cache-container>
</infinispan>注意 topology 属性在这里是 server。这可以保证 cache store 使用的是独立的 Redis 服务器拓扑结构。只需要定义一个 Redis 服务器(如果定义了多个,只有第一个会被使用),端口会使用 Redis 的默认端口 6379,也可以使用 port 属性覆盖端口。所有的连接由一个连接池进行管理,连接池同时还负责连接的创建、释放、选择处于空闲的连接。Sentinel模式Sentinel 模式依赖于 Redis 的 Sentinel 服务器,以此来连接到 Redis 的 master。具体来说,Infinispan 连接到 Redis 的 Sentinel 服务器,请求 master 的名字,然后能获得正确的 master 服务器地址。这种拓扑结构通过 Redis Sentinel 提供了可用性,实现了对 Redis 服务器的失效检测和自动恢复。<?xml version="1.0" encoding="UTF-8"?>
<infinispan
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:8.0 http://www.infinispan.org/schemas/infinispan-config-8.0.xsd
urn:infinispan:config:store:redis:8.0
http://www.infinispan.org/schemas/infinispan-cachestore-redis-config-8.0.xsd"
xmlns="urn:infinispan:config:8.0"
xmlns:redis="urn:infinispan:config:store:redis:8.0" >
<cache-container>
<local-cache>
<persistence passivation="false">
<redis-store xmlns="urn:infinispan:config:store:redis:8.0"
topology="sentinel" master-name="mymaster" socket-timeout="10000" connection-timeout="10000">
<sentinel-server host="server1" />
<sentinel-server host="server2" />
<sentinel-server host="server3" />
<connection-pool min-idle="6" max-idle="10" max-total="20"
min-evictable-idle-time="30000" time-between-eviction-runs="30000" />
</redis-store>
</persistence>
</local-cache>
</cache-container>
</infinispan>对于 Sentinel 模式,topology 属性需要改成 sentinel。还需要指定 master 的名字,用于选择正确的 Redis 的 master,因为一个 Sentinel 服务器可以监控多个 Redis 的 master。需要注意的是,Sentinel 服务器通过一个叫 sentinel-server 的 XML 标签来定义,这与单服务器和集群都不一样。如果没有指定,Sentinel 的默认端口是。至少需要指定一个 Sentinel 服务器,如果你有多台Sentinel 服务器,也可以都加上,这样可以 Sentinel 服务器自身也可以实现容灾。集群在集群拓扑结构下,Infinispan 可以连接到一个 Redis 集群。一个或多个集群节点可以加到infinispan (越多越好),被用于保存所有的数据。Redis 集群支持失效检测,所以如果集群里有master 挂掉了,就会有 slave 提升为 master。Redis 集群需要 Redis 3。<?xml version="1.0" encoding="UTF-8"?>
<infinispan
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:8.0 http://www.infinispan.org/schemas/infinispan-config-8.0.xsd
urn:infinispan:config:store:redis:8.0
http://www.infinispan.org/schemas/infinispan-cachestore-redis-config-8.0.xsd"
xmlns="urn:infinispan:config:8.0"
xmlns:redis="urn:infinispan:config:store:redis:8.0" >
<cache-container>
<local-cache>
<persistence passivation="false">
<redis-store xmlns="urn:infinispan:config:store:redis:8.0"
topology="cluster" socket-timeout="10000" connection-timeout="10000">
<redis-server host="server1" port="6379" />
<redis-server host="server2" port="6379" />
<redis-server host="server3" port="6379" />
<connection-pool min-idle="6" max-idle="10" max-total="20"
min-evictable-idle-time="30000" time-between-eviction-runs="30000" />
</redis-store>
</persistence>
</local-cache>
</cache-container>
</infinispan>对于集群,topology 属性必须改成 cluster。必须指定一个或多个 Redis 集群节点,可以使用 redis-server 标签来说明。注意如果是操作集群,不支持 database ID。一个 Redis 对应多个 Cache StoreRedis 的独立服务器模式或者 Sentinel 模式都支持 database ID。一个 database ID 可以让单个Redis 服务器支持多个独立的 database,通过一个整数 ID来区分。这可以让 Infinispan 在单个Redis 部署上支持多个 cache store,不同的 store 直接的数据可以加以隔离。Redis 集群不支持database ID。在 redis-store 标签上可以通过 database 属性定义 database ID。 <?xml version="1.0" encoding="UTF-8"?>
<infinispan
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:8.0 http://www.infinispan.org/schemas/infinispan-config-8.0.xsd
urn:infinispan:config:store:redis:8.0
http://www.infinispan.org/schemas/infinispan-cachestore-redis-config-8.0.xsd"
xmlns="urn:infinispan:config:8.0"
xmlns:redis="urn:infinispan:config:store:redis:8.0" >
<cache-container>
<local-cache>
<persistence passivation="false">
<redis-store xmlns="urn:infinispan:config:store:redis:8.0"
topology="sentinel" master-name="mymaster" socket-timeout="10000"
connection-timeout="10000" database="5">
<sentinel-server host="server1" />
<sentinel-server host="server2" />
<sentinel-server host="server3" />
<connection-pool min-idle="6" max-idle="10" max-total="20"
min-evictable-idle-time="30000" time-between-eviction-runs="30000" />
</redis-store>
</persistence>
</local-cache>
</cache-container>
</infinispan>Redis的密码认证Redis 可选用密码进行认证,用于增加对服务器的安全性。这需要在 cache store 连接的时候指定密码。redis-store 标签的 password 属性可以指定这个密码。<?xml version="1.0" encoding="UTF-8"?>
<infinispan
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:8.0 http://www.infinispan.org/schemas/infinispan-config-8.0.xsd
urn:infinispan:config:store:redis:8.0
http://www.infinispan.org/schemas/infinispan-cachestore-redis-config-8.0.xsd"
xmlns="urn:infinispan:config:8.0"
xmlns:redis="urn:infinispan:config:store:redis:8.0" >
<cache-container>
<local-cache>
<persistence passivation="false">
<redis-store xmlns="urn:infinispan:config:store:redis:8.0"
topology="sentinel" master-name="mymaster" socket-timeout="10000"
connection-timeout="10000" password="mysecret">
<sentinel-server host="server1" />
<sentinel-server host="server2" />
<sentinel-server host="server3" />
<connection-pool min-idle="6" max-idle="10" max-total="20"
min-evictable-idle-time="30000" time-between-eviction-runs="30000" />
</redis-store>
</persistence>
</local-cache>
</cache-container>
</infinispan>是否有SSL支持?Redis 没有提供协议加密,而是将这个留给其他专业的软件。目前,Infinispan 集成的连接Redis服务器的 Redis 客户端(Jedis)还没有原生的对 SSL 连接的支持。下面关于Redis的文章您也可能喜欢,不妨参考下:Ubuntu 14.04下Redis安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htmRedis主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htmRedis集群明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htmUbuntu 12.10下安装Redis(图文详解)+ Jedis连接Redis http://www.linuxidc.com/Linux/2013-06/85816.htmRedis系列-安装部署维护篇 http://www.linuxidc.com/Linux/2012-12/75627.htmCentOS 6.3安装Redis http://www.linuxidc.com/Linux/2012-12/75314.htmRedis安装部署学习笔记 http://www.linuxidc.com/Linux/2014-07/104306.htmRedis配置文件redis.conf 详解 http://www.linuxidc.com/Linux/2013-11/92524.htmInfinispan 的详细介绍:请点这里
Infinispan 的下载地址:请点这里英文原文:New Redis Cache Store Introduced in Infinispan 8本文永久更新链接地址