Configuring Multiple Instances

Now let’s say we want to run forex-microservice on two instances, so configure the instances in file under src/main/resources of currency-conversion-microservice. The entire file looks as below:


It’s obvious from the above file that our forex-microservice will run on localhost:8080 and localhost:8085. So we have to configure forex-microservice to run on 8085 port because the forex-microservice already running on port 8080.

Configuring Forex Microservice

Do right click on the spring-boot-reactive-forex-microservice and select Run As -> Run Configurations…

When a popup window opens, under the Java Application, search ReactiveForexMicroSvcApp or ReactiveForexMicroSvcApp(1) and do right click on this and click on Duplicate.

Now put server port under VM arguments: section under Arguments tab as shown in below image:

spring mongodb functional reactive microservices

Now run two instances of forex-microservice and one instance of currency-conversion-microservice by executing main class.

So forex-microservice instances are up on 8080 and 8085 ports, whereas currency-conversion-microservice instance is up on port 9090.

Testing the Microservices

USD to INR Amount

Request Method – GET

Request URL – http://localhost:9090/currency-exchange/from/USD/to/INR/quantity/25



The above response comes from the currency-conversion-microservice through forex-microservice that runs on port 8080. It does not mean that the above response always will come from the same server running on 8080.

EUR to INR Amount

Request Method – GET

Request URL – http://localhost:9090/currency-exchange/from/EUR/to/INR/quantity/25



The above response comes from the currency-conversion-microservice through forex-microservice that runs on port 8085. It does not mean that the above response always will come from the same server running on 8085.

We are using Ribbon to distribute the load between the two instances of forex-service.

However, we are hardcoding the URLs of both instances of forex-microservice in currency-conversion-microservice. Therefore every time if there is a new instance of forex-microservice, we need to change the configuration of currency-conversion-microservice and it’s not what we want.

So we will use Eureka Naming Server to fix this problem.

Configuring Eureka Server

Creating Spring Boot Project

Now we will create another Spring Boot project in Eclipse. So create gradle based project in Eclipse with the name spring-boot-reactive-microservice-eureka-server-config.

Updating Build Script

Modify the build.gradle script to include Eureka server.

buildscript {
    ext {
        springBootVersion = '2.1.6.RELEASE'
    repositories {
    dependencies {
plugins {
    id "io.spring.dependency-management" version "1.0.8.RELEASE"
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
dependencies {
dependencyManagement {
    imports {
        mavenBom ''
Creating Configuration File

Now add below content to src/main/resources/ to include some configurations.

The eureka server will run on port 8761.


Notice we have added two properties with false value. If we do not make them false (as by default they are true), you may face following exception while running Eureka server: Cannot execute request on any known server

eureka.client.register-with-eureka=false : if we make this property true then while the server starts the inbuilt client will try to register itself with the Eureka server.

eureka.client.fetch-registry=false : if we make this property true then the inbuilt client will try to fetch the Eureka registry, which is not yet available. As a result, we would get TransportException.

Creating Main Class

Create below main class in order to deploy application into embedded eureka server on port 8761.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class ReactiveEurekaConfigMicroSvcApp {
	public static void main(String[] args) {, args);
Running the Application

Now run the main class to deploy eureka-server-config application. Your eureka server starts on port 8761.

If you hit the URL http://localhost:8761 in the browser, you will get a Spring Eureka page.

You will see that currently there is no instance running on eureka server.

Configuring Eureka on Microservices

Now we will connect forex-microservice and currency-conversion-microservice using eureka server.

Now add below dependency to both of the build scripts – forex-microservice and currency-conversion-microservcie.

Make sure you stop the microservices before you add to build script and later build the microservices.


Configuring Eureka URL on Microservices

Next we need to configure eureka URL into file of both microservices – forex-microservice and currency-conversion-microservice:


Enabling DiscoveryClient on Microservices

Now put @EnableDiscoveryClient annotation on main classes of forex-microservice and currency-conversion-microservice. We need this annotation to register with eureka server. The @EnableDiscoveryClient activates the Netflix Eureka Discovery Client implementation.

Running Microservices

Now run two instances of forex-microservice and one instance of currency-conversion-microservice.

Refreshing Eureka Server Page

Now refresh the eureka server page URL – http://localhost:8761, you will see one instance of currency-conversion-microservice and two instances of forex-microservice are up and running.

We have now created two microservices – forex-microservice & currency-conversion-microservice and established communication between them.

We are using Ribbon to distribute load between the two instances of forex-microservice and Eureka as the naming server.

When we launch new instances of forex-microservice, you would see that load is automatically distribute to them.

Now you can continue the same tests you performed before we created eureka-server-config application.

That’s all on how to create Spring MongoDB Functional Reactive Microservices example.

Source Code

Thanks for reading.

Pages: 1 2 3

Leave a Reply

Your email address will not be published. Required fields are marked *