Usage of the client module can support a wide variety of integration patterns to the destination
Obtaining the URL.
The first thing required is to get the location of the api you wish to call. This is done
using the Cluster interface, and passing a few parameters to a call to serviceToUrl.
staticClustercluster=SingletonServiceFactory.getBean(Cluster.class);// protocol, serviceId, environment, requestKey (load balancer key, not required in most cases)
Based on the location that the com.networknt.apib-1.0.0 service registers itself in the
instance of consul pointed to from service.yml, it will return an https url to that
one of the instances.
Calling the Service
1. Building the Request
Given a url, first open a connection to that server:
If the service fails to connect, an exception will be thrown.
The next thing we want to do is get a reference to the response, build our latches for asynchronous processing, and generate a request.
// Get a reference to the response
finalAtomicReference<ClientResponse>reference=newAtomicReference<>();// We only expect one response, so close the latch after it's received.
finalCountDownLatchlatch=newCountDownLatch(1);// Build the request object:
If we needed to include Client Credential Authorization in the request as a standalone client, then the following can be added:
After we have these objects created, we now only have to send the request, and wait for the response.
3. Sending the request
// Send the request
connection.sendRequest(request,client.createClientCallback(reference,latch));// Wait for the response, SLA is 100 milliseconds for example.
latch.await(100,TimeUnit.MILLISECONDS);// Read the response.
Closing the connection, if necessary could be done
How would I get the HTTP response code of the request:
How would I get the body (content) of the response: