ZooKeeper discovery

ZooKeeper discovery is recommended for 1000-2000-node deployments and linear scalability. ZooKeeper discovery needs a ZooKeeper cluster. The Apache Ignite nodes connect to the ZooKeeper cluster to build a star topology where discovery events go through the ZooKeeper server cluster. It is recommended to use more than one ZooKeeper server for high availability, so if one ZooKeeper server goes down, the Apache Ignite nodes can still exchange discovery events through the other instance. 

To work with the ZooKeeper topology, you need to install ZooKeeper. Download Apache ZooKeeper from the following link: https://www.apache.org/dyn/closer.cgi/zookeeper/ :

Follow these steps to configure ZooKeeper:

  1. Extract the tar file to a directory such as C:apache-zookeeper-3.4.12.
  2. Go to the bin directory and create a file, called zoo.cfg.
  3. Edit the file and add the following lines:
      # The number of milliseconds of each tick
tickTime=2000

# The number of ticks that the initial
# synchronization phase can take
initLimit=10

# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5

# the directory where the snapshot is stored.
dataDir=C:\apache-zookeeper-3.4.12data

# the port at which the clients will connect
clientPort=2181
  1. Now, start zkServer.cmd or zkServer.sh based on your operating system. The console will look as follows:

Now, our ZooKeeper server is up and running, and waiting for connections on port 2181.

Apache Ignite needs a client jar to connect to Apache ZooKeeper. Open the Java project that we created in Chapter 1, Getting Familiar with Apache Ignite. Modify the build.gradle file to include the ignite-zookeeper dependency:

Refresh the project to update the dependency graph. It will download zookeeper.jar and its dependencies:

We need to configure ZookeeperDiscoverySpi to connect to ZooKeeper. The ZookeeperDiscoverySpi needs to know two things, zKConnectionString and sessionTimeout. zKConnectionString is a comma-separated ZooKeeper server IP and port list, and sessionTimeout is required to tell the ZooKeeper cluster how soon it will close an Ignite node session if the node gets disconnected from the ZooKeeper cluster. This value should be greater than the ZooKeepr's tickTime * syncLimit defined in zoo.cfg. ZooKeeper sends a tick to its nodes every tickTime (here, this is 2,000 milliseconds) and waits for syncLimit*tickTime before declaring a node as disconnected and closing the session. In our case, the sessionTimeout should be greater than 2000*5 = 10,000 milliseconds.

The following code snippet will configure the discovery spi. Check that the session timeout is set to 30,000 and that ZooKeeper's server URL is set to 127.0.0.1:2181. The Zookeeper's client connection port is 2181 (we configured it in the zoo.cfg file):

ZookeeperDiscoverySpi zkDiscoSpi = new ZookeeperDiscoverySpi();
zkDiscoSpi.setZkConnectionString("127.0.0.1:2181");
zkDiscoSpi.setSessionTimeout(30_000);
zkDiscoSpi.setZkRootPath("/apacheIgnite");
zkDiscoSpi.setJoinTimeout(10_000);

Now, create an Ignite configuration and pass zkDiscoSpi as discoverySpi:

 IgniteConfiguration cfg = new IgniteConfiguration();
// Override default discovery SPI.
cfg.setDiscoverySpi(zkDiscoSpi);

Pass the config to the start method to start an ignite instance:

 try (Ignite ignite = Ignition.start(cfg))

The following is the full code snippet:

public class HelloWorld {
public static void main(String[] args) throws InterruptedException {
ZookeeperDiscoverySpi zkDiscoSpi = new ZookeeperDiscoverySpi();
zkDiscoSpi.setZkConnectionString("127.0.0.1:2181");
zkDiscoSpi.setSessionTimeout(30_000);
zkDiscoSpi.setZkRootPath("/apacheIgnite");
zkDiscoSpi.setJoinTimeout(10_000);

IgniteConfiguration cfg = new IgniteConfiguration();
// Override default discovery SPI.
cfg.setDiscoverySpi(zkDiscoSpi);

try (Ignite ignite = Ignition.start(cfg)) {
IgniteCache<Integer, String> cache = ignite.getOrCreateCache("myFirstIgniteCache");
for (int i = 0; i < 10; i++)
cache.put(i, Integer.toString(i));
for (int i = 0; i < 10; i++)
System.out.println("Fetched [key=" + i + ", val=" + cache.get(i) + ']');
//sleep few seconds
Thread.sleep(999999999);
}
}
}

Now, run the program. It will connect to the ZooKeeper server:

Check the ZooKeeper console:

The program is still running as we put a thread.sleep at the end.

Now, launch the same program again. It will try to connect to the ZooKeeper cluster and log information to the ZooKeeper console:

The Program console will tell you that the new ignite node is connected to the old ZooKeeper cluster:

After the sessionTimeout time, in our case, set to 30,000 milliseconds, the ZooKeeper terminates the session if there is no communication between an Apache Ignite node and the ZooKeeper:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.57.126