Search code examples
spring-bootload-balancingspring-cloud-gateway

How to load balance request to list of URIs in Spring Cloud Gateway


I don't have eureka, load balancer and so on. I want to load balance the request to list of URIs on which instances of backend run.

spring:
  cloud:
    gateway:
      routes:
        - id: myService-endpoints
          uri: myServiceIp1, myServiceIp2
          predicates:
            - Path=/myservice/**

myServiceIp1 and myServiceIp2 are my backend service IPs. How can I achieve load balancing in this scenario?


Solution

  • If you are not using service registry, you can still use load-balancing in your app. You will just need to use the SimpleDiscoveryClient that allows you to set your service instance URIs via a property file, like so:

    spring.cloud.discovery.client.simple.instances.service1[0].uri=http://s11:8080
    

    Then you can just follow the guides for setting up load-balanced routes in Gateway, like so:

    spring:
     cloud:
       gateway:
         routes:
         - id: myRoute
           uri: lb://service
           predicates:
           - Path=/service/**