LoadBalancer Filter

The LoadBalancer Filter takes a serviceId parameter. The LoadBalancerClient uses this to choose an instance for routing. The LoadBalancer filter needs to be explicitly used in the Java DSL. When using the LoadBalancer Filter, use the empty http() method in org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.

RouteConfiguration.java
import static org.springframework.cloud.gateway.server.mvc.filter.LoadBalancerFilterFunctions.lb;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;

@Configuration
class RouteConfiguration {

    @Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsAddReqHeader() {
		return route("api_route")
				.GET("/api/**", http())
					.filter(lb("apiservice"))
					.build();
    }
}

Using The LoadBalancer Filter In Configuration

The LoadBalancer Filter may be used in configuration by using a URI with the lb scheme (such as lb://myservice), it uses the Spring Cloud LoadBalancerClient to resolve the name (myservice in this example) to an actual host and port and replaces the URI in the same attribute. The following listing configures a LoadBalancer Filter:

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: api_route
          uri: lb://apiservice
          predicates:
          - Path=/api/**
By default, when a service instance cannot be found by the ReactorLoadBalancer, a 503 is returned.
The isSecure value of the ServiceInstance returned from the LoadBalancerClient overrides the scheme specified in the request made to the Gateway. For example, if the request comes into the Gateway over HTTPS but the ServiceInstance indicates it is not secure, the downstream request is made over HTTP. The opposite situation can also apply.
Gateway supports all the LoadBalancer features. You can read more about them in the Spring Cloud Commons documentation.