Chapter 6. Consuming microservices

 

This chapter covers

  • How to consume a microservice
  • Your choices when consuming a microservice

Consuming a microservice can mean many things to many people. Clients of a microservice could be scripts, web pages, other microservices, or pretty much anything that can make HTTP requests. If we covered them all, this chapter would be a whole book by itself!

Developing a microservice is interesting, but it doesn’t get you far until you introduce many microservices interacting with each other. To enable two services to interact with each other, you need a method by which one service can call another.

This chapter provides examples focused on one microservice consuming another with Java-based libraries, but the methods shown can be equally applied to any generic Java client consuming a microservice.

With Enterprise Java, two services would interact with a direct service call, as shown in figure 6.1.

Figure 6.1. Enterprise Java business service calls

The service call could be accomplished by the following:

  • @EJB injection when using EJBs
  • @Inject with CDI
  • Retrieving an instance of a service via a static method or variable
  • Spring dependency injection, either XML or annotation based

All these options require that your two services reside in the same JVM and runtime, as shown in figure 6.1.

6.1. Consuming a microservice with a Java client library

6.2. Consuming a microservice with a JAX-RS client library

Summary