Setting up Currency Conversion Microservice
In the previous section, we have created currency-exchange-service. Now we will create a currency-conversion-service that talks to currency-exchange-service.
Step 1: Open the browser and type https://start.spring.io/.
- Provide the Group name tutoraspire.microservice and Artifact currency-conversion-service.
- Add the dependencies: Spring web, DevTools, Actuator, and Config Client.
- Click on the Generate It downloads the created project.
Step 2: Import the downloaded project in Spring Tool Suite (STS).
File -> Import -> Existing Maven Projects -> Next -> Browse -> Select the project -> Finish.
It takes some time to import the project.
Step 3: Open the application.properties file and configure the application name and port number.
application.properties
The currency-conversion-service runs on port 8100.
In the next section, we will create a service that talks to the currency-exchange-service.
Creating a Service for currency-conversion-service
In the previous section, we have used EUR to INR that returns what the conversionMultiple is. The currency-exchange-service tells what is the exchange value when we convert currency from EUR to INR.
In this section, we will create CurrencyCalculationService. It defines a lot of functionality related to conversion.
We will create a service currency-converter that accepts two path parameters “from” and “to“. It also accepts the quantity (amount which we want to convert).
Let’s create a currency-conversion-service.
Step 1: Create a class with the name CurrencyConversionController.
Step 2: Add an annotation @RestController.
Step 3: Create a GetMapping.
CurrencyConversionController.java
Step 4: Create a class with the name CurrencyConversionBean and define the following fields:
Step 5: Generate Getters and Setters.
Step 6: Generate constructor and also create a default constructor.
CurrencyConversionBean.java
Step 7: Restart the application and type the following URI in the browser:
http://localhost:8100/currency-converter/from/USD/to/INR/quantity/1000
In the above response, “from,” “to,” and “quantity” variables picked up from the path. We have hardcoded the other variables.
In the next step, from the currency-conversion-service, we will call the currency-exchange-service. We will also determine what the conversion multiple is, and will use that amount (conversion multiple) to calculate the total amount. We will also use the port that comes in the response.