|
| 1 | +[[loadbalancer-filter]] |
| 2 | += LoadBalancer Filter |
| 3 | + |
| 4 | +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`. |
| 5 | + |
| 6 | +.RouteConfiguration.java |
| 7 | +[source,java] |
| 8 | +---- |
| 9 | +import static org.springframework.cloud.gateway.server.mvc.filter.LoadBalancerFilterFunctions.lb; |
| 10 | +import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route; |
| 11 | +import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http; |
| 12 | +
|
| 13 | +@Configuration |
| 14 | +class RouteConfiguration { |
| 15 | +
|
| 16 | + @Bean |
| 17 | + public RouterFunction<ServerResponse> gatewayRouterFunctionsAddReqHeader() { |
| 18 | + return route("api_route") |
| 19 | + .GET("/api/**", http()) |
| 20 | + .filter(lb("apiservice")) |
| 21 | + .build(); |
| 22 | + } |
| 23 | +} |
| 24 | +---- |
| 25 | + |
| 26 | +== Using The LoadBalancer Filter In Configuration |
| 27 | + |
| 28 | +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. |
| 29 | +//The unmodified original URL is appended to the list in the `ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR` attribute. |
| 30 | +The following listing configures a LoadBalancer Filter: |
| 31 | + |
| 32 | +.application.yml |
| 33 | +[source,yaml] |
| 34 | +---- |
| 35 | +spring: |
| 36 | + cloud: |
| 37 | + gateway: |
| 38 | + mvc: |
| 39 | + routes: |
| 40 | + - id: api_route |
| 41 | + uri: lb://apiservice |
| 42 | + predicates: |
| 43 | + - Path=/api/** |
| 44 | +---- |
| 45 | + |
| 46 | +NOTE: By default, when a service instance cannot be found by the `ReactorLoadBalancer`, a `503` is returned. |
| 47 | +// TODO: implement use404 |
| 48 | +// You can configure the gateway to return a `404` by setting `spring.cloud.gateway.loadbalancer.use404=true`. |
| 49 | + |
| 50 | +NOTE: The `isSecure` value of the `ServiceInstance` returned from the `LoadBalancerClient` overrides |
| 51 | +the scheme specified in the request made to the Gateway. |
| 52 | +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`. |
| 53 | +The opposite situation can also apply. |
| 54 | +//However, if `GATEWAY_SCHEME_PREFIX_ATTR` is specified for the route in the Gateway configuration, the prefix is stripped and the resulting scheme from the route URL overrides the `ServiceInstance` configuration. |
| 55 | + |
| 56 | +TIP: Gateway supports all the LoadBalancer features. You can read more about them in the https://docs.spring.io/spring-cloud-commons/docs/current/reference/html/#spring-cloud-loadbalancer[Spring Cloud Commons documentation]. |
0 commit comments