CRecordGetMicroServiceInstances

This message is used when you need one instance of each of the specified microservices.

This message belongs to the microservice registry API.

{
  "id": "c5557e3e-2a2c-45c7-89b5-39d9286d317b",
  "name": "GET_MICRO_SERVICE_INSTANCES",
  "isService": "false",
  "description": "Get the target addresses of the given microservice IDs.\nIf more than one instance is registered, random instance are chosen.",
  "slots": [
    {
      "key": "1",
      "name": "MICRO_SERVICE_IDS",
      "direction": "REQUEST",
      "mandatory": "true",
      "type": "ID_ARRAY",
      "description": "The microservices to search for."
    },
    {
      "key": "2",
      "name": "MICRO_SERVICE_INSTANCES",
      "direction": "ANSWER",
      "mandatory": "false",
      "type": "RECORD_ARRAY",
      "description": "The instances of the given microservices."
    }
  ]
}

Example of use (after generating of the class CRecordGetMicroServiceInstances):

// in constructor:
addMessageHandler(CRecordGetMicroServiceInstances.ID,
                  this::asyncGetInstances);

// request
private void getMicroServiceInstances(@NotNull final CTargetAddress aAddressOfMicroserviceRegistry,
                                      @NotNull final IId[] aMicroserviceIds) throws CException
{
    final CEnvelope env = CEnvelope.forSingleTarget(aAddressOfMicroserviceRegistry);
    final CRecord rec = CRecordGetMicroServiceInstances.create();
    CRecordGetMicroServiceInstances.setMicroServiceIds(rec,
                                                       aMicroserviceIds);
    sendRequest(env,
                rec);
}

// fetch data from reply
private boolean asyncGetInstances(@NotNull final CEnvelope aEnvelope,
                                  @NotNull final CRecord aRecord)
{
    if (aEnvelope.isAnswer())
    {
        if (aEnvelope.getResultCode() == CResultCode.SUCCESS)
        {
            final CRecord[] instances = CRecordGetMicroServiceInstances.getMicroServiceInstances(aRecord,
                                                                                                 null);
            for (CRecord instance : instances)
            {
                final IId instanceId = CRecordMicroServiceInstance.getMicroServiceInstanceId(instance,
                                                                                             null);
                final IId microServiceId = CRecordMicroServiceInstance.getMicroServiceId(instance,
                                                                                         null);
                final CTargetAddress address = CRecordMicroServiceInstance.getMicroServiceAddress(instance,
                                                                                                  null);
                final ZonedDateTime timeAdded = CRecordMicroServiceInstance.getTimeAdded(instance,
                                                                                         null);
                // ...
            }
        }
        return true;
    }
    return false;
}

nyssr.net - Innovative Distributed System