How to make your app aware of its other members in the cluster

If you are running your app with multiple instances (scaling out), then occasionally you may need to make your app aware of its other members.

This is relatively simple with the Bees client library (open source).

In this example I am using Scala, but it can be converted to Java or Groovy etc pretty easily (here are some extra brackets you can add in as needed: {}}{{[])(}}).

Firstly, you will want to add the cloudbees library to your app, in sbt it is something like:

“com.cloudbees” % “cloudbees-api-client” % “1.5.2”

(in maven it would be:

dependency>
    groupId>com.cloudbees.sdkgroupId>
    artifactId>cloudbees-sdkartifactId>
    version>1.5.2version>
dependency>

)

Then, in your code - you can run something like the following to enumerate the members of the cluster:

      val client = new BeesClient(“https://api.cloudbees.com/api”, 
                                   key, secret, “xml”, “1.0”)
      client.setVerbose(false)
      val instances = client.applicationInstanceList(conf.appId).getInstances()

      instances map (i => println(“APP INSTANCE:” + i.getId +  ” HOST” + i.getSettings.get(“host”)))

This will iterate through all members and print out the host name - you can also then get the port and so on. 
Note that the membership is likely to change over time - so your code needs to be able to periodically check for membership - if that is important to your app. 
This does mean that you need the API key in your app - we are hoping to offer an api that can have a limited (via oauth) scope key just for these calls - but for now, you can use “bees config:set” to store your keys if you need to do this right now. 

Add new comment