Cloud-Native Microservices With TIBCO: Service Discovery Using Consul

DZone 's Guide to

Cloud-Native Microservices With TIBCO: Service Discovery Using Consul

Since they're dynamically changing, microservices make service discovery complicated. Learn how to use Consul for service discovery in cloud-native microservices.

· Microservices Zone ·
Free Resource

A microservices-based application typically runs in a virtualized or containerized environment where the number of instances and endpoints changes dynamically. Consequently, there must be a mechanism to enable consumers of these microservices to make requests to a dynamically changing set of service instances.

This caused one of the biggest problems in the microservices world: how does the client of a service - the API gateway or another service - discover the location of a service instance? Solution to this problem is to use service registry, which will maintain the endpoint information of these dynamically changing microservices and discover the endpoints through this service registry.

TIBCO enables developers to build cloud-native applications and supports many open-source tools in the microservices ecosystem. Consul is one of the tools in that ecosystem for service registry and discovery. The service registry and discovery mechanisms enable services to announce their availability and let clients dynamically find these services.

In this tutorial, we will be learning about cloud-native microservices with TIBCO using the service registry and discovery tool, Consul.

Let's take an example of a quote service. Here's a flow diagram of the quote service:

Image title

The quote service will make a call to different microservices in order to provide a quote to the customer. These four microservices - Product, Price, Inventory, and Discount - can be running on different hosts. The quote service needs to know the endpoints of each of these services.

Image title

Service registry plays a vital role in this scenario. Each service registers itself to service registry on startup. A client of the service query the service registry to find out the available instances of a service. The quote service will look as follows in a TIBCO design-time environment.

Image title

Service Discovery Using Consul

Each service needs to register itself to the service registry - in this case, to Consul. Here is the design time configuration for each of the microservices:

  • The HTTP connector resource is used to expose a REST service.

  • Give the name to the service in the "Service Name" field.

Image title

A developer needs to do only above mentioned configurations to HTTP connector in order to use Service registry mechanism.

For  Quote service to work, it needs to call every service deployed. To make successful calls, it needs to discover service endpoints at runtime. HTTP client is the resource used to configure any external service call. Users can configure endpoint details of the service to be called 

Following design time configuration required in HTTP client for service discovery mechanism.

  • Check the "Enable Service Discovery" checkbox.

  • Give a name to the service in the "Service Name" field.

Image title

Once development of the service is done, export deployment archive (EAR file) of the Service. The same EAR can be deployed to Docker or Cloud Foundry depending on your choice.

Following is the runtime configuration for Docker deployment:

  • Application docker image can be built by using EAR and base BusinessWorks Container edition docker image provided by TIBCO.

  • Once application image is ready, run docker container by providing the Consul server URL and Service Name property.

  • Provide CONSUL_SERVER_URL as an environment variable.

E.g. Following command will register the Price service with the name "getprice" to the Consul Server provided:

docker run -d -e CONSUL_SERVER_URL= -p 
18082:8085 -e PriceService=getprice tibco/bwce:price

Similarly, start the Docker containers of every service. Once all the services are up and running, the status of services can be seen on the Consul UI. 

Image title

Now, to check service discovery, create and deploy docker image of the Quote service. In this command Consul server where all services and registered is given along with service names to be discovered.

docker run -d -e CONSUL_SERVER_URL= 
-e discountService=getDiscount -e 
inventoryService=getInventory -e 
priceService=getprice -e productService=getproduct 
-e getQuote=getquote -e BW_PROFILE=Docker 
-p 18084:8085 -p 8092:8090 tibco/bwce:quote

Once the service is up and running, test the service with the Swagger UI.

Image title

cloud, consul, microservices, service discovery, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}