Flat Preloader Icon

Invoking currency-exchange-service

Invoking the currency-exchange-service from the currency-conversion-service in a microservices architecture typically involves making an HTTP request to the currency-exchange-service API. Here’s how you can achieve this:

Step 1: Create or Use the currency-exchange-service

Ensure that you have a working currency-exchange-service that provides exchange rate information for different currency pairs. This service should have API endpoints to fetch exchange rates.

Step 2: Configure the currency-conversion-service

In the currency-conversion-service, you need to configure how to communicate with the currency-exchange-service. This configuration should include the base URL of the currency-exchange-service.

In Spring Boot, you can define the base URL in the application.properties or application.yml file:



Step 3: Create a REST Template or Use WebClient

To make HTTP requests to the currency-exchange-service, you can use either RestTemplate or WebClient, depending on your preference and the version of Spring you are using.

Here’s an example of how to do it using RestTemplate:

public class CurrencyConversionController 

    private String exchangeServiceBaseUrl;

    private RestTemplate restTemplate;

    public CurrencyConversion convertCurrency(
        @PathVariable String from,
        @PathVariable String to,
        @PathVariable BigDecimal quantity
    ) {
       // Make an HTTP request to currency
    ResponseEntity<CurrencyConversion> response 
        = restTemplate.exchange(
    + "/currency-exchange/from/{from}/to/{to}",
            from, to
     CurrencyConversion exchangeRate =
        // Calculate the total calculated amount
        BigDecimal totalCalculatedAmount
        = exchangeRate

        // Create a CurrencyConversion object
        return new CurrencyConversion(


The above code sends an HTTP GET request to the currency-exchange-service to fetch the exchange rate for the given currency pair (from and to). It then calculates the total converted amount and constructs a CurrencyConversion response.

Step 4: Run and Test

Run the currency-conversion-service and test it by making HTTP requests to the /currency-converter endpoint. Ensure that it can successfully retrieve exchange rates from the currency-exchange-service and perform currency conversion.

By following these steps, you can invoke the currency-exchange-service from the currency-conversion-service to get exchange rates and perform currency conversions in a microservices architecture. Make sure to handle error scenarios, implement proper exception handling, and consider security aspects as needed in your application.

Share on: