Skip to content

REST Client Microservice Provider

Radek Koubsky edited this page Dec 29, 2016 · 2 revisions

Provides a wrapper for calling an arbitrary REST service that is deployed anywhere. The REST service is described by an interface that is injected into a SilverWare microservice. The injected interface is used within the SilverWare microservice to call the actual REST service. The main advantage is that a user does not have to care about underlying HTTP calls, closing resources etc., they only need to call the interface methods.

How it works

Let's define the HelloRestServiceProxy interface that describes our REST service:

   public interface HelloRestServiceProxy {
   @Path("hello")
   @GET
   @Produces(MediaType.TEXT_PLAIN)
   String hello();
}

In the next step, we create the ClientService microservice that has the HelloRestServiceProxy interface injected via CDI. In order to make the REST client microservice provider work correctly, we need to add the @ServiceConfiguration annotation to the injected interface. We also need to specify the endpoint of the injected REST service.

@Microservice
@Path("/client_service")
public class ClientService {
   @Inject
   @MicroserviceReference
   @ServiceConfiguration(endpoint = "http://www.example.com/hello_service")
   HelloRestServiceProxy restService;

   @Path("call_rest_service")
   @GET
   @Produces(MediaType.TEXT_PLAIN)
   public Response callRestService() {
      return Response.ok(this.restService.hello()).build();
   }
}

When the ClientService is created, SilverWare will create a proxy of the interface that is injected into the ClientService microservice using CDI Microservice Provider, this proxy translates the interface method calls to the REST calls via HTTP.

JAX-RS 2.0 Client API and Resteasy Proxy Framework

The proxy behind the interface representing our REST service is created using Resteasy Client API.