Microservices With Spring Boot - Part 2 - Creating a Forex Microservice

let's learn the basics of microservices and microservices architectures. we will also start looking at a basic implementation of a microservice with spring boot. we will create a couple of microservices and get them to talk to each other using eureka naming server and ribbon for client side load balancing.

this is part 2 of this series. in this part, we will focus on creating the forex microservice.

microservices with spring boot


you will learn

resources overview

forex service (fs) is the service provider. it provides currency exchange values for various currency. let's assume that it talks to a forex exchange and provides the current conversion value between currencies.

an example request and response is shown below:

get to http://localhost:8000/currency-exchange/from/eur/to/inr

{
  id: 10002,
  from: "eur",
  to: "inr",
  conversionmultiple: 75,
  port: 8000,
}

the request above is the currency exchange value for eur to inr. in the response, conversionmultiple is 75.

project code structure

the following screenshot shows the structure of the project we will create.

a few details:

tools you will need

our github repository has the complete maven project with all the code examples.

bootstrapping with spring initializr

creating a microservice with spring initializr is a cake walk. spring initializr is a great tool to bootstrap your spring boot projects. you can create a wide variety of projects using spring initializr.

the following steps have to be done for a web services project:

creating exchange value entity

@entity
public class exchangevalue {

  @id
  private long id;

  @column(name="currency_from")
  private string from;

  @column(name="currency_to")
  private string to;

  private bigdecimal conversionmultiple;
  private int port;

  public exchangevalue() {

  }


  public exchangevalue(long id, string from, string to, bigdecimal conversionmultiple) {
    super();
    this.id = id;
    this.from = from;
    this.to = to;
    this.conversionmultiple = conversionmultiple;
  }

  public long getid() {
    return id;
  }

  public string getfrom() {
    return from;
  }

  public string getto() {
    return to;
  }

  public bigdecimal getconversionmultiple() {
    return conversionmultiple;
  }

  public int getport() {
    return port;
  }

  public void setport(int port) {
    this.port = port;
  }

}

important things to note:

creating exchange value jpa repository

/spring-boot-microservice-forex-service/src/main/java/com/in28minutes/springboot/microservice/example/forex/exchangevaluerepository.java

package com.in28minutes.springboot.microservice.example.forex;
import org.springframework.data.jpa.repository.jparepository;

public interface exchangevaluerepository extends 
    jparepository<exchangevalue, long>{
  exchangevalue findbyfromandto(string from, string to);
}

notes:

create the resource - forexcontroller

/spring-boot-microservice-forex-service/src/main/java/com/in28minutes/springboot/microservice/example/forex/forexcontroller.java

@restcontroller
public class forexcontroller {

  @autowired
  private environment environment;

  @autowired
  private exchangevaluerepository repository;

  @getmapping("/currency-exchange/from/{from}/to/{to}")
  public exchangevalue retrieveexchangevalue
    (@pathvariable string from, @pathvariable string to){

    exchangevalue exchangevalue = 
        repository.findbyfromandto(from, to);

    exchangevalue.setport(
        integer.parseint(environment.getproperty("local.server.port")));

    return exchangevalue;
  }
}

notes:

configure application name and other configuration

/spring-boot-microservice-forex-service/src/main/resources/application.properties

spring.application.name=forex-service
server.port=8000

spring.jpa.show-sql=true
spring.h2.console.enabled=true

we are assigning a port of 8000 for this application and enabling debug logging.

insert test data into data.sql

let's insert some test data by creating a file called data.sql. spring boot auto configuration ensures that this data is loaded up when application starts up.

/spring-boot-microservice-forex-service/src/main/resources/data.sql

insert into exchange_value(id,currency_from,currency_to,conversion_multiple,port)
values(10001,'usd','inr',65,0);
insert into exchange_value(id,currency_from,currency_to,conversion_multiple,port)
values(10002,'eur','inr',75,0);
insert into exchange_value(id,currency_from,currency_to,conversion_multiple,port)
values(10003,'aud','inr',25,0);

test forex microservice

get to http://localhost:8000/currency-exchange/from/eur/to/inr

{
  id: 10002,
  from: "eur",
  to: "inr",
  conversionmultiple: 75,
  port: 8000,
}

complete code example

/spring-boot-microservice-forex-service/pom.xml

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>

  <groupid>com.in28minutes.springboot.microservice.example.forex</groupid>
  <artifactid>spring-boot-microservice-forex-service</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>

  <name>spring-boot-microservice-forex-service</name>
  <description>microservices with spring boot and spring cloud - forex service</description>

  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.0.0.m3</version>
    <relativepath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>finchley.m2</spring-cloud.version>
  </properties>

  <dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-data-jpa</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>

    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-devtools</artifactid>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupid>com.h2database</groupid>
      <artifactid>h2</artifactid>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <dependencymanagement>
    <dependencies>
      <dependency>
        <groupid>org.springframework.cloud</groupid>
        <artifactid>spring-cloud-dependencies</artifactid>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencymanagement>

  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>

  <repositories>
    <repository>
      <id>spring-snapshots</id>
      <name>spring snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>spring-milestones</id>
      <name>spring milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginrepositories>
    <pluginrepository>
      <id>spring-snapshots</id>
      <name>spring snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </pluginrepository>
    <pluginrepository>
      <id>spring-milestones</id>
      <name>spring milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginrepository>
  </pluginrepositories>


</project>


/spring-boot-microservice-forex-service/src/main/java/com/in28minutes/springboot/microservice/example/forex/exchangevalue.java

package com.in28minutes.springboot.microservice.example.forex;
import java.math.bigdecimal;

import javax.persistence.column;
import javax.persistence.entity;
import javax.persistence.id;

@entity
public class exchangevalue {

  @id
  private long id;

  @column(name="currency_from")
  private string from;

  @column(name="currency_to")
  private string to;

  private bigdecimal conversionmultiple;
  private int port;

  public exchangevalue() {

  }


  public exchangevalue(long id, string from, string to, bigdecimal conversionmultiple) {
    super();
    this.id = id;
    this.from = from;
    this.to = to;
    this.conversionmultiple = conversionmultiple;
  }

  public long getid() {
    return id;
  }

  public string getfrom() {
    return from;
  }

  public string getto() {
    return to;
  }

  public bigdecimal getconversionmultiple() {
    return conversionmultiple;
  }

  public int getport() {
    return port;
  }

  public void setport(int port) {
    this.port = port;
  }

}


/spring-boot-microservice-forex-service/src/main/java/com/in28minutes/springboot/microservice/example/forex/exchangevaluerepository.java

package com.in28minutes.springboot.microservice.example.forex;
import org.springframework.data.jpa.repository.jparepository;

public interface exchangevaluerepository extends 
    jparepository<exchangevalue, long>{
  exchangevalue findbyfromandto(string from, string to);
}


/spring-boot-microservice-forex-service/src/main/java/com/in28minutes/springboot/microservice/example/forex/forexcontroller.java

package com.in28minutes.springboot.microservice.example.forex;
import java.math.bigdecimal;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.core.env.environment;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.restcontroller;

@restcontroller
public class forexcontroller {

  @autowired
  private environment environment;

  @autowired
  private exchangevaluerepository repository;

  @getmapping("/currency-exchange/from/{from}/to/{to}")
  public exchangevalue retrieveexchangevalue
    (@pathvariable string from, @pathvariable string to){

    exchangevalue exchangevalue = 
        repository.findbyfromandto(from, to);

    exchangevalue.setport(
        integer.parseint(environment.getproperty("local.server.port")));

    return exchangevalue;
  }
}


/spring-boot-microservice-forex-service/src/main/java/com/in28minutes/springboot/microservice/example/forex/springbootmicroserviceforexserviceapplication.java

package com.in28minutes.springboot.microservice.example.forex;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;

@springbootapplication
public class springbootmicroserviceforexserviceapplication {

  public static void main(string[] args) {
    springapplication.run(springbootmicroserviceforexserviceapplication.class, args);
  }
}


/spring-boot-microservice-forex-service/src/main/resources/application.properties

spring.application.name=forex-service
server.port=8000

spring.jpa.show-sql=true
spring.h2.console.enabled=true


/spring-boot-microservice-forex-service/src/main/resources/data.sql

insert into exchange_value(id,currency_from,currency_to,conversion_multiple,port)
values(10001,'usd','inr',65,0);
insert into exchange_value(id,currency_from,currency_to,conversion_multiple,port)
values(10002,'eur','inr',75,0);
insert into exchange_value(id,currency_from,currency_to,conversion_multiple,port)
values(10003,'aud','inr',25,0);


/spring-boot-microservice-forex-service/src/test/java/com/in28minutes/springboot/microservice/example/forex/springbootmicroserviceforexserviceapplicationtests.java

package com.in28minutes.springboot.microservice.example.forex;

import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;

@runwith(springrunner.class)
@springboottest
public class springbootmicroserviceforexserviceapplicationtests {

  @test
  public void contextloads() {
  }

}


 

 

 

 

Top