Consuming registered services

Now, it's time to consume our registered service. To achieve this, we implement another REST service, which acts as a client. The project structure for the client is as follows:

ClientInvokerResource is the actual service, with the implementation given in the following code block. We are injecting a Snoop-based client with the help of the @Snoop annotation, and the serviceName attribute matches the name that we used when registering the service:

@Path("/invoke")
@RequestScoped
public class ClientInvokerResource {
@Inject
@Snoop(serviceName = "weatherSnoop")
private SnoopServiceClient client;

@GET
@Produces(MediaType.APPLICATION_JSON)
public String invoke() {
String response = client.simpleGet("temperature")
.filter(r -> r.getStatus() == 200)
.map(r -> r.readEntity(String.class))
.orElse("Problem occurred!");
return response;
}
}

The following command sh0ws deploying the application onto Payara micro with port auto-binding enabled:

java -jar /path/to/payara-micro.jar --deploy /path/to/weather-snoop-client.war --autoBindHttp

We can use the URL http://localhost:8081/weather-snoop-client-1.0/client/invoke and it gives us the response from the Temperature service we deployed before. 

SnoopEE is not a load-balancer, so if you are running multiple instances of the same Microservice, it should be load balanced with a load-balancer framework.
..................Content has been hidden....................

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