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
- part 1 - getting started with microservices architecture
- current part - part 2 - creating forex microservice
- part 3 - creating currency conversion microservice
- part 4 - using ribbon for load balancing
- part 5 - using eureka naming server
you will learn
- how to create a microservice with spring boot.
- how to create a jpa entity and resource.
- how to get spring mvc, spring boot, jpa, hibernate, and h2 to work together.
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:
- springbootmicroserviceforexserviceapplication.java - the spring boot application class generated with spring initializer. this class acts as the launching point for the application.
-
pom.xml
- contains all the dependencies needed to build this project. we will use spring boot starter web and jpa. -
exchangevalue.java
- exchange value entity -
exchangevaluerepository.java
- exchangevalue jpa repository. this is created using spring data jparepository. -
forexcontroller.java
- spring rest controller exposing the forex conversion service. - data.sql - initial data for the exchange_value table. spring boot would execute this script after the tables are created from the entities.
tools you will need
- maven 3.0+ is your build tool
- your favorite ide. we use eclipse.
- jdk 1.8+
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:
- launch spring initializr and choose the following
- choose
com.in28minutes.springboot.microservice.example.forex
as group - choose
spring-boot-microservice-forex-service
as artifact - choose following dependencies
- choose
- click generate project.
- import the project into eclipse. file -> import -> existing maven 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:
-
@entity
: specifies that the class is an entity. this annotation is applied to the entity class. -
@id
: specifies the primary key of an entity.
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:
-
public interface exchangevaluerepository extends jparepository<exchangevalue, long>
- we are extending jparepository using two generics - exchangevalue & long. exchangevalue is the entity that is being managed and the primary key of exchangevalue is long. -
exchangevalue findbyfromandto(string from, string to);
- we would want to query the conversion value from one currency to another. we are defining a query method for it.
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:
-
@restcontroller public class forexcontroller {
- create a controller to expose a rest service -
@autowired private environment environment
- we would want to return the server port back. this will help us identify which instance service is giving the response back. -
@autowired private exchangevaluerepository repository
- autowire the repository. -
exchangevalue exchangevalue = repository.findbyfromandto(from, to)
- get the exchange value from the database. -
exchangevalue.setport(integer.parseint(environment.getproperty("local.server.port")))
- get the port from environment and set it into the response bean.
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() {
}
}