Distributing calls using Eureka and Ribbon
In the previous section, we have registered currency-exchange-service and currency-conversion-service with the Eureka naming server.
When CurrencyCalculationService (currency-conversion-service) invokes CurrencyExchangeService, the Eureka naming server finds the details of the currency-exchange-service.
Instead of hardcoding the URLs for Ribbon, the Ribbon talks to the Eureka naming server and retrieve the details of all the instances of microservices.
Step 1: Open the file application.properties.
In this file, we have configured the Eureka naming server and disable the list of servers that we have configured earlier. In the currency-conversion-service, we have already configured the URL for Eureka. Now, we have configured the Eureka naming server in both the services.
Now the CurrencyExchangeService, starts talking to the Eureka naming server.
Note: In the above code, hash (#) denotes the comment.
Here one thing is to notice that in the application.properties file, we don’t have any source of currency-exchange-service. We did not hardcode the URLs of currency-exchange-service in the currency-conversion-service.
Step 2: Kill all the running applications.
Step 3: First, run the NetflixEurekaNamingServerApplication.java.
Step 4: Open the browser and refresh the Eureka server.
Step 5: Run an instance of CurrencyExchangeServiceApplication.java on port 8000.
Step 6: Run the CurrencyConversionServiceApplication.java.
Step 7: Clear the console and let it be warm-up.
Step 8: Open the browser and type the URL http://localhost:8100/currency-converter-feign/from/EUR/to/INR/quantity/1000.
When we refresh the Eureka server, it always returns the port 8000 in the response.
Step 9: Run another instance of CurrencyExchangeServiceApplication.java on port 8001.
Step 10: Again, refresh the Eureka server. We see that there are two instances of currency-exchange-service that are running on port 8000 and 8001.
Step 11: Refresh the URL http://localhost:8100/currency-converter-feign/from/EUR/to/INR/quantity/1000.
When we refresh the Eureka server, it changes the port again and again.
Step 12: Kill the CurrencyExchangeServiceApplication8000.
Step 13: Again, refresh the URL http://localhost:8100/currency-converter-feign/from/EUR/to/INR/quantity/1000. It returns the port 8001 in the response.
Hence, service will never go down even if an instance of the same service is down or interrupted by other reasons.