Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a micro service

I looked for the similar question but could not find any . I have a micro service created with drop-wizard which is running in localhost:9000.

I am working in another project(with spring mvc) running in 8080. I wan to call the above service which gives me string back from any of my controllers in main project .lets say the path is "localhost:9000/giveMeString" .

like image 424
Narayan Avatar asked Oct 21 '14 16:10

Narayan


People also ask

How do you call a microservice in Java?

With gRPC gateway, it's possible to integrate JSON-based REST API with gRPC. The synchronous call is the simplest way to communicate two services. It also bonds them together, since the calling microservice needs to wait for a response from remote.

How can microservice call each other?

The most common type is single-receiver communication with a synchronous protocol like HTTP/HTTPS when invoking a regular Web API HTTP service. Microservices also typically use messaging protocols for asynchronous communication between microservices.

Can we call the API in microservices?

Microservices is an approach to building an application that breaks its functionality into modular components. APIs are part of an application that communicates with other applications. So, APIs can be used to enable microservices.


1 Answers

You can use Apache's HTTP Client. See this example borrowed from their documentation:

  // Create an instance of HttpClient.
  HttpClient client = new HttpClient();

  // Create a method instance.
  GetMethod method = new GetMethod("http://localhost:9000/giveMeString");

  // Execute the method.
  int statusCode = client.executeMethod(method);

  // Read the response body.
  byte[] responseBody = method.getResponseBody();

  //Print the response
  System.out.println(new String(responseBody));
like image 166
th3morg Avatar answered Sep 18 '22 18:09

th3morg