Infinispan data can be persisted into a data store. In this setup, we going to use postgres as the datastore.
First we need the following xml and we called it - is-db.xml
<infinispan
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:15.0 https://infinispan.org/schemas/infinispan-config-15.0.xsd
urn:infinispan:server:15.0 https://infinispan.org/schemas/infinispan-server-15.0.xsd"
xmlns="urn:infinispan:config:15.0"
xmlns:server="urn:infinispan:server:15.0">
<cache-container name="default" statistics="true">
<transport cluster="${infinispan.cluster.name:cluster}" stack="${infinispan.cluster.stack:tcp}" node-name="${infinispan.node.name:}"/>
<security>
<authorization/>
</security>
</cache-container>
<distributed-cache name="sessions" mode="SYNC">
<memory max-count="1999" when-full="REMOVE"/>
<expiration lifespan="30"/>
<persistence>
<string-keyed-jdbc-store xmlns="urn:infinispan:config:store:jdbc:15.0"
dialect="POSTGRES">
<connection-pool driver="org.postgresql.Driver"
connection-url="jdbc:postgresql://host.docker.internal:5432/postgres"
username="keycloak"
password="password"/>
<string-keyed-table create-on-start="true"
prefix="ISPN_STRING_TABLE">
<id-column name="ID_COLUMN"
type="VARCHAR(255)" />
<data-column name="DATA_COLUMN"
type="VARCHAR(255)" />
<timestamp-column name="TIMESTAMP_COLUMN"
type="BIGINT" />
<segment-column name="SEGMENT_COLUMN"
type="INT"/>
</string-keyed-table>
</string-keyed-jdbc-store>
</persistence>
</distributed-cache>
<server xmlns="urn:infinispan:server:15.0">
<interfaces>
<interface name="public">
<inet-address value="${infinispan.bind.address:127.0.0.1}"/>
</interface>
</interfaces>
<data-sources>
<!-- Defines a unique name for the datasource and JNDI name that you
reference in JDBC cache store configuration.
Enables statistics for the datasource, if required. -->
<data-source name="ds"
jndi-name="jdbc/postgres"
statistics="true">
<!-- Specifies the JDBC driver that creates connections. -->
<connection-factory driver="org.postgresql.Driver"
url="jdbc:postgresql://host.docker.internal:5432/postgres"
username="keycloak"
password="password">
<!-- Sets optional JDBC driver-specific connection properties. -->
<connection-property name="name">value</connection-property>
</connection-factory>
<!-- Defines connection pool tuning properties. -->
<connection-pool initial-size="1"
max-size="10"
min-size="3"
background-validation="1000"
idle-removal="1"
blocking-timeout="1000"
leak-detection="10000"/>
</data-source>
</data-sources>
<socket-bindings default-interface="public" port-offset="${infinispan.socket.binding.port-offset:0}">
<socket-binding name="default" port="${infinispan.bind.port:11222}"/>
</socket-bindings>
<security>
<security-realms>
<security-realm name="default">
<!-- Uncomment to enable TLS on the realm -->
<!-- server-identities>
<ssl>
<keystore path="server.pfx"
password="password" alias="server"
generate-self-signed-certificate-host="localhost"/>
</ssl>
</server-identities-->
<properties-realm/>
</security-realm>
</security-realms>
</security>
<endpoints socket-binding="default" security-realm="default" />
</server>
</infinispan>
Then we will run the following command to ask infinispan to use the persistent store. Please ensure you have downloaded postgres sql driver too.
docker run --rm --name ispn -p 11222:11222 -p 9990:9990 -e USER=keycloak -e PASS=keycloak -e INFINISPAN_CONSOLE=enabled -e LOG_LEVEL=TRACE -v ${PWD}/is-db.xml:/opt/infinispan/server/conf/infinispan.xml -v ${PWD}/postgresql-42.7.8.jar:/opt/infinispan/server/lib/postgresql-42.7.8.jar quay.io/infinispan/server:15.0
Your infinispan should be running like this
And then if you go into your postgres instance, you should be able to see the table here:-
And finally your infinispan console should reflect this as well.
The configuration above will not allow you to manually create an entry. But you can create another cache that can do this. Noticed that we are using <encoding media-type="text/plain"/>
If you do not specify then encoding type will be application/x-java-object
<distributed-cache name="demo2" mode="SYNC" statistics="true">
<memory max-count="1999" when-full="REMOVE"/>
<encoding media-type="text/plain"/>
<expiration lifespan="30"/>
<persistence>
<string-keyed-jdbc-store xmlns="urn:infinispan:config:store:jdbc:15.0"
dialect="POSTGRES">
<connection-pool driver="org.postgresql.Driver"
connection-url="jdbc:postgresql://host.docker.internal:5432/postgres"
username="keycloak"
password="password"/>
<string-keyed-table create-on-start="true"
prefix="ISPN_STRING_TABLE">
<id-column name="ID_COLUMN"
type="VARCHAR(255)" />
<data-column name="DATA_COLUMN"
type="VARCHAR(255)" />
<timestamp-column name="TIMESTAMP_COLUMN"
type="BIGINT" />
<segment-column name="SEGMENT_COLUMN"
type="INT"/>
</string-keyed-table>
</string-keyed-jdbc-store>
</persistence>
</distributed-cache>
And your data persisted
Full xml can be obtained here
https://gist.github.com/kepungnzai/2135d59764bd03292971f92f3e827b7b
Comments