Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load balancing (ribbon) and routing (zuul) Spring REST API (Spring Data JPA) requests to multiple copies of same service

I couldn't seem to find a clear example/solution to the following scenario; any help/information would be appreciated!

I have a Spring REST API service that uses Spring Data JPA to interface to a backend database server. I am running this exact same service on multiple machines, and I have a reverse proxy that routes client requests (using Netflix Zuul) to these API services in order to access the data in the database. However, I want to load balance the requests (via the reverse proxy and possibly Netflix Ribbon) so that each request will only be sent to a single API server (in a load-balanced fashion).

Spring Data JPA automatically configures endpoints based on the entity classes and rest repositories I define. One solution would be to generate an equivalent endpoint on the reverse proxy for each and every endpoint on the API services, and then use a Ribbon client to load balance each endpoint individually. However, this does not seem like the appropriate solution.

My question is, does Netflix Zuul or Ribbon provide any functionality to handle this sort of scenario? Essentially, I would like to be able to set a YAML config that simply tells Zuul to use a Ribbon client and automatically load balance all requests to a given endpoint, according to the available listOfServers Ribbon config.

application.yml

Example:

DatabaseAPI:
  ribbon:
    eureka:
      enabled: false
  listOfServers: localhost:8080, localhost:8181, localhost:8282
  ServerListRefreshInterval: 15000

zuul:
  routes:
    data:
      path: /db/**
      ServiceId: DatabaseAPI

Now, for example, I want each request to /db/** to be sent to one of the available servers in the server list, in a round-robin fashion. Note: With this example config, every request is sent to ALL the available servers simultaneously. I only want ONE of them to receive each request.

like image 232
corecase Avatar asked Dec 14 '22 03:12

corecase


1 Answers

by default Zuul uses Ribbon to locate an instance to forward to via discovery (e.g Eureka in your case). So, in Gateway application (Zuul) you dont need to have Ribbon in the classpath.

in case you dont see the loadbalancing between your instances through Zuul, try multiplying requests (e.g > 200 request)

like image 66
redoff Avatar answered Feb 13 '23 23:02

redoff