Create New Microservice CurrencyConverser

List of Dependencied for Createing Currency Conversion Service

user same Group as com.khan.microserservices

  1. Web
  2. DevTools
  3. Actuator
  4. Config Client


Also configure application.properties

spring.application.name=currency-conversion-service

server.port=8100


Make sure to add

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-bootstrap</artifactId>

</dependency>


in pom.xml to avoid the spring.config.import error




 

With Currency Calculation we have to calculate the FROM to To like USD to INR and convert
how many 1000 USD to INR will be 86000 INR like that
simply 1 USD is worth of 86 INR so 1000 USD will be 86000 INR
Create Dummy Currency Conversion service

STEP 1 Controller Class

@RestController

public class CurrencyConversionController {


@GetMapping("/currency-converter/from/{from}/to/{to}/quantity/{quantity}")

public CurrencyConversionBean convertCurrency(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity) {

return new CurrencyConversionBean(1L, from, to, BigDecimal.ONE, quantity, quantity);

}

}

STEP 2: Bean Class

public class CurrencyConversionBean {

private Long id;

private String from;

private String to;

private BigDecimal conversionMultiple;

private BigDecimal quantity;

private BigDecimal totalCalculatedAmount;

private int port;

//Getter Setter

//Constructore argument and without argument

}



To get the Currency Converstion through Currency

Exchange Service


@RestController

public class CurrencyConversionController {


@GetMapping("/currency-converter/from/{from}/to/{to}/quantity/{quantity}")

public CurrencyConversionBean convertCurrency(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity) {

Map<String, String> uriVariables = new HashMap<String, String>();

uriVariables.put("from", from);

uriVariables.put("to", to);

ResponseEntity<CurrencyConversionBean> responseEntity = new RestTemplate()

.getForEntity("http://localhost:8000/currency-exchange/from/{from}/to/{to}",

CurrencyConversionBean.class,

uriVariables);

CurrencyConversionBean response = responseEntity.getBody();

return new CurrencyConversionBean(response.getId(), from, to, response.getConversionMultiple(), quantity,

quantity.multiply(response.getConversionMultiple()), response.getPort());

}

}

 

Comments

Popular posts from this blog

Microservice with Java Spring Cloud

STEP 10 Configure Porfile for Limit service

STEP 9 Connect Limit service to Spring Cloud Config Server