In this quick tutorial we will learn how to use Docker to run a cluster of Infinispan servers in no time!
First of all, we need to find out the Docker image to pull:
$ docker search infinispan INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/jboss/infinispan-server 2 [OK]
The list is quite large, however we will rely on the first one (jboss/infinispan-server) which is the official one. You can run it with just one command:
$ docker run -it jboss/infinispan-server
The first time you will execute this command, the image will be pulled from the Docker Hub repository so it will take some time. Once done, it will display the console as you have started it in interactive mode.
From another shell, execute the following command that will retrieve the IP Address of the last started container:
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' $(docker ps -q) 172.17.0.2
Great! Now let’s start another Infinispan server re-using the same Docker image:
$ docker run -it jboss/infinispan-server
This time the server will start instantly. As you can see from the server logs, the server has received a new cluster view which includes two servers:
14:56:47,304 INFO [org.infinispan.remoting.transport.jgroups.JGroupsTransport] (MSC service thread 1-5) ISPN000078: Starting JGroups channel clustered 14:56:47,316 INFO [org.infinispan.remoting.transport.jgroups.JGroupsTransport] (MSC service thread 1-5) ISPN000094: Received new cluster view for channel clustered: [f4cdbdfc1e53|1] (2) [f4cdbdfc1e53, b643a6cb879b]
Let’s pickup as well the second server’s IP Address:
$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' $(docker ps -q) 172.17.0.3
We can confirm that the Cluster is up and running by executing the ispn-cli.sh shell into one of the containers:
$ docker exec -it $(docker ps -q -l) /opt/jboss/infinispan-server/bin/ispn-cli.sh -c "/subsystem=datagrid-infinispan/cache-container=clustered:read-attribute(name=members)" { "outcome" => "success", "result" => "[f4cdbdfc1e53, b643a6cb879b]" }
Great! We have a cluster of infinispan up and running. Our final test will be connecting to the Cluster. For this purpose we can code a simple Hot Rod Client which contains the cluster view (For more details about Infinispan and Hot Rod see this tutorial: Infinispan 8 tutorial )
Here is our HotRod client class:
import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.UUID; import org.infinispan.client.hotrod.RemoteCache; import org.infinispan.client.hotrod.RemoteCacheManager; import org.infinispan.client.hotrod.ServerStatistics; import org.infinispan.client.hotrod.configuration.ConfigurationBuilder; public class Main { private RemoteCacheManager cacheManager; private RemoteCache<String, Object> cache; public Main() { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.addServer() .host("172.17.0.2") .port(Integer.parseInt("11222")); builder.addServer() .host("172.17.0.3") .port(Integer.parseInt("11222")); cacheManager = new RemoteCacheManager(builder.build()); cache = cacheManager.getCache("default"); cache.put(UUID.randomUUID().toString(), UUID.randomUUID().toString()); System.out.println("Dumping cache Data"); System.out.println("=========================="); Set set = this.cache.keySet(); Iterator i = set.iterator(); while (i.hasNext()) { System.out.println(i.next()); } //Print cache statistics ServerStatistics stats = cache.stats(); for (Map.Entry stat : stats.getStatsMap().entrySet()) { System.out.println(stat.getKey() + " : " + stat.getValue()); } } public static void main(String[] args) { Main manager = new Main(); } }
As you can see from the Client logs, the Hot Rod library correctly received the Cluster view and updated the cache “default” with a random String:
INFO: ISPN004006: /172.17.0.2:11222 sent new topology view (id=3) containing 2 addresses: [/172.17.0.3:11222, /172.17.0.2:11222] Dec 29, 2016 5:18:08 PM org.infinispan.client.hotrod.RemoteCacheManager start INFO: ISPN004021: Infinispan version: 8.0.1.Final Dec 29, 2016 5:18:08 PM org.infinispan.client.hotrod.impl.protocol.Codec20 readNewTopologyAndHash INFO: ISPN004006: /172.17.0.2:11222 sent new topology view (id=3) containing 2 addresses: [/172.17.0.3:11222, /172.17.0.2:11222] Dumping cache Data ========================== 281542e5-85de-44b4-942d-f6bff573a7b2 1752fd2a-21ed-4424-8426-863ab69a4bce currentNumberOfEntries : 2 hits : 0 removeHits : 0 totalBytesRead : 15 timeSinceStart : 7 removeMisses : 0 totalNumberOfEntries : 1 stores : 1 misses : 0 retrievals : 0 totalBytesWritten : 0
That’s all! You can find the complete list of Jboss.org Docker images available at: http://www.jboss.org/docker/