jboss - 如何将缓存的 Keycloak 数据保存到持久数据存储中?
问题描述
我们在 2 个 Docker 容器中运行 Keycloak(v4.4,独立模式)。我们希望这些容器是无状态的,因此我们必须将所有缓存的数据保存到后备存储(数据库或其他缓存解决方案,如 Redis)。我们不能让缓存数据只存在于内存中,因为我们的任何一个容器都可能随时被销毁。
理想情况下,我们希望将缓存数据持久化到我们自己的 Redis 实例中。由于 Keycloak 使用 Infinispan,似乎这是配置 Infinispan 以使用 Redis 的方式:http: //infinispan.org/docs/cachestores/redis/。
standalone-4.4.0.xml
天真地,我试图通过将我的文件更新为如下所示让 Keycloak 在 Redis 中存储会话信息(注意第redis-store
5 行的元素):
<subsystem xmlns="urn:jboss:domain:infinispan:6.0">
<cache-container name="keycloak">
<local-cache name="sessions">
<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>
<local-cache name="realms">
<object-memory size="10000"/>
</local-cache>
<local-cache name="users">
<object-memory size="10000"/>
</local-cache>
<local-cache name="authenticationSessions"/>
<local-cache name="offlineSessions"/>
<local-cache name="clientSessions"/>
<local-cache name="offlineClientSessions"/>
<local-cache name="loginFailures"/>
<local-cache name="work"/>
<local-cache name="authorization">
<object-memory size="10000"/>
</local-cache>
<local-cache name="keys">
<object-memory size="1000"/>
<expiration max-idle="3600000"/>
</local-cache>
<local-cache name="actionTokens">
<object-memory size="-1"/>
<expiration max-idle="-1" interval="300000"/>
</local-cache>
</cache-container>
<cache-container name="server" default-cache="default" module="org.wildfly.clustering.server">
<local-cache name="default">
<transaction mode="BATCH"/>
</local-cache>
</cache-container>
<cache-container name="web" default-cache="passivation" module="org.wildfly.clustering.web.infinispan">
<local-cache name="passivation">
<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>
<cache-container name="ejb" aliases="sfsb" default-cache="passivation" module="org.wildfly.clustering.ejb.infinispan">
<local-cache name="passivation">
<locking isolation="REPEATABLE_READ"/>
<transaction mode="BATCH"/>
<file-store passivation="true" purge="false"/>
</local-cache>
</cache-container>
<cache-container name="hibernate" module="org.infinispan.hibernate-cache">
<local-cache name="entity">
<transaction mode="NON_XA"/>
<object-memory size="10000"/>
<expiration max-idle="100000"/>
</local-cache>
<local-cache name="local-query">
<object-memory size="10000"/>
<expiration max-idle="100000"/>
</local-cache>
<local-cache name="timestamps"/>
</cache-container>
</subsystem>
但是当我启动 Keycloak 时,我得到了这个错误:
'persistence' isn't an allowed element here
.
问题:有没有一种简单的方法来配置 Keycloak 以将缓存数据保存在 Redis 或其他持久数据存储中?
解决方案
子系统版本urn:jboss:domain:infinispan:6.0
不知道您的 xml 的此架构,因此您必须更新子系统,或者如果您使用 Keycloak 的最新图像(6.0.1)可能会更容易实现一个新的 ,它只InfinispanConnectionProviderFactory
涉及基本上是用 Wildfly 做的:
/subsystem=keycloak-server/spi=connectionsInfinispan/:remove()
/subsystem=keycloak-server/spi=connectionsInfinispan/:add(default-provider=custom)
/subsystem=keycloak-server/spi=connectionsInfinispan/provider=custom/:add(properties={},enabled=true)
当然,为此,您必须实现一个扩展并部署它。但是在代码级别,您可以使用最新 Infinispan 的全部功能。
我看到您想使用 Redis,这是它的另一个大问题,请阅读此答案https://stackoverflow.com/a/57362238/571689我告诉您以下您会遇到的问题。
推荐阅读
- python - 石墨烯:“期望 XXX 类型的值,但收到:...”
- typescript - VS 2019:如何禁用警告“请安装 'Microsoft.TypeScript.MSBuild' NuGet 包以在您的项目中启用 TypeScript 编译。”
- postgresql - 如何转换整数,使它们在 PostgreSQL SELECT 中有 n 个小数位
- eclipse - Windows 中 Eclipse 中的 Apache Storm Starter 2.2.0 - 尝试从 localhost NimbusLeaderNotFound 获取领导者 nimbus 信息时出现异常
- algorithm - 有没有一种有效的方法可以在函数图中找到最短路径?
- python - 熊猫数据框的索引目前是两列的“分组依据”,但我希望它们作为自己的列,并带有标准数字索引
- jquery - 如何模仿 POST 在 R 中抓取网站
- r - 在 R Markdown 中显示图像的正确方法是什么?
- android - 使用提供枚举列表的 IN 运算符查询房间数据库
- javascript - 如何根据象限公式进行测验/调查