Connecting a Java Client with Apache Geode Docker Container

Connecting a Java Client with Apache Geode Docker Container

In the previous article, we started a simple apache geode docker instance. However, for simplicity, we had not started any locator, server process in that article, which we will do now, and not only that, we also try to connect to the geode cluster using a simple Java Apache Geode client and put some data on the running docker instance.

Getting started with Apache Geode CLI

Let’s try to run a geode cluster with one locator and server process inside the docker container. Run below command as mentioned in the previous article.

docker run -it — rm apachegeode/geode

By default, it will open up gfsh CLI where you can run the below commands to start the cluster.

start locator
start server

Once you have both locator and server up and running you can run the below command to verify the count of the running members.

list members
Image for post

We have 2 members up and running. Additionally, you can use the status command also to verify the status on both locator and server.

You can also run the docker exec command to ssh into the docker container and see the process running.

docker exec -it container id bash
ps -ef | grep geode

Getting started non-interactive mode

If you are interested in starting a geode cluster non-interactively with docker you can follow this link. It may seem like that cluster is working properly which it is but there are some unresolved issues that we will discuss now. Geode pulse may not work as well and we will see in a few minutes why.

Problem

One problem with the interactive mode is that as soon as you exit from the gfsh your container will exit also. This is not useful for us. You can apply a simple trick here by pressing Ctrl+P and Ctrl+Q(works on a mac) so that your container is running in the background and then you can run the docker attach command to go back to gfsh command prompt.

For non-interactive mode, you can follow the official document.

Creating Geode Region

Now we have the required process up and running. First, we would need to create a geode region on the server-side and then use the same region name while accessing from the client application.

For simplicity, we create a replicated region with no persistence.

gfsh>create region — name=Test — type=REPLICATE

Member | Status | Message

— — — — — — — | — — — | — — — — — — — — — — — — — — — — — — — — — 
stay-rich-kite | OK | Region “/Test” created on “stay-rich-kite”

Inserting data via Java Client

Now we will create a simple java application that connects to our geode cluster and insert some test data. Let’s do that now. You can view the source code here. Let’s run the main method of the java client and see what happens.

Problem

You will be welcomed by this exception message. This means that we are not able to connect with the locator running inside a docker container. Ideally, it should have worked with localhost. You may also think that probably docker container died but that is not the case.

Exception in thread “main” org.apache.geode.cache.client.NoAvailableLocatorsException: Unable to connect to any locators in the list [LocatorAddress [socketInetAddress=localhost/127.0.0.1:10334, hostname=localhost, isIpString=false]]

I might have not exposed the ports for my host so it may be failing because of that. Let’s try exposing ports also and see what happens.

docker run -it -p 10334:10334 -p 1099:1099 -p 7070:7070 -p 8080:8080 -p 40404:40404 apachegeode/geode

Now run docker ps

docker ps

In the output of the above command, you will this 0.0.0.0:1099->1099/tcp in the ports tab. This means the port is accessible to our host also. Like many others, ports are also exposed. You can verify this by running the geode pulse URL. Pulse is a monitoring web application for geode and you can run geode OQL query also with this UI interface.

http://localhost:7070/pulse/clusterDetail.html

With earlier docker run command Pulse was not working but now it is. Let’s check again if our java client can access the geode cluster now. Unfortunately, you will get the below exception with your java client.

Exception in threadmainorg.apache.geode.cache.client.NoAvailableServersException

Still, we are not able to access the geode cluster from outside of the docker container. The problem is cluster will be started using docker non-public IPs (probably on a 172.17.0.0/16) any externally connecting client will not be able to connect directly to these addresses.

The solution is to use the geode property — hostname-for-clients while starting locator and server.

The description provided on geode documentation for this flag.

hostname-for-clients:-Hostname or IP address that will be sent to clients so they can connect to this locator.

Default value:-uses the bind-address to which the locator is bound.

So this default value is our problem as inside the container it may bind to the different IP addresses and it may change on each run also so it’s better to localhost in hostname-for-clients while running geode with docker. You may not need to provide it if you are not running geode inside docker but anyway better to use this.

So correct commands to use are below.

start locator — hostname-for-clients=localhost
start server — hostname-for-clients=localhost

No change in the region creation command or client code. Just start the docker and using gfsh run the above commands to start the geode locator and servers.

I was able to connect and put some test data to the geode test region. Here is the output from the Geode pulse after running the query.

Image for post

Here is the sample code we used to insert data to the geode region.

public class ExamplePut {
    public static void main(String[] args) {
        ClientCacheFactory clientCacheFactory = new ClientCacheFactory();
        clientCacheFactory.addPoolLocator("localhost",10334);
        ClientCache clientCache = clientCacheFactory.create();
        clientCache.createClientRegionFactory(ClientRegionShortcut._PROXY_).create("Test");
        Region<String, String> region = clientCache.getRegion("Test");
        region.put("one","Hello");
    }
}

Thanks.

Support me

If you like what you just read then you can buy me a coffee

Buy Me A Coffee

Further reading

You can also read some of my previous articles.

Did you find this article valuable?

Support Ashish Choudhary by becoming a sponsor. Any amount is appreciated!