-1

I am developing a microservice platform with Spring. So far I have a few microservices and a service discovery server where all microservices register themselves. I also have an Angular client application, which right now is calling the address and port of one of the services. However, what if the service gets replicated due to high load? How can I load balance the client calls between both instances? I know I could utilize an API gateway, but the same question arises when the API gateway scales itself.

1

You've got a couple of options to you:

Local Proxy

A local proxy like Zuul or Spring Gateway allow you to take advantage of a load balancing at the proxy server level.

If you are using Consul, there should be a module to generate NGINX configs and tell it to reload.

Client Lookup

As a Eureka client, for example, a very simple load balancing solution exists for your application. Instead of resolving directly to your specific instance of a microservice, you use the service name in your URL.

Let's say you have an Auth Service that talks to a User Management Service. Your User Management Service registers itself under the name "user-mgmt" (spring.application.name), and the Auth Service registers itself as "auth".

When you create the URL for the User Management Service, you simply use it's spring.application.name in your URL where the server name would be. Example:

http://user-mgmt/profile

The plumbing in Spring knows how to intercept that URL and find the actual instance registered by that name. The Eureka client bit periodically checks the listing of services registered to that name, and selects one of the values to substitute. The substitution should take care of the host name, port, and protocol (http vs https).

As far se client libraries that leverage this functionality you have the options:

  • Feign - generates client bindings to call services from a Java interface (also works with Hystrix)
  • RestTemplate - core component of Spring Boot
  • Direct use of Eureka Client - it's pretty low level, but you have all the power of how you pick one instance over another.

Honestly, anything that uses RestTemplate will work as I described.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.