spring: application: name: gateway-service cloud: gateway: routes: - id: client uri:…
- User
spring: application: name: gateway-service cloud: gateway: routes: - id: client uri: lb://client-service predicates: - Path=/api/client/** Такая конфигурация выдает ошибку: Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.cloud.gateway' to org.springframework.cloud.gateway.config.GatewayProperties failed:
Property: spring.cloud.gateway.routes[0].predicates Value: [] Reason: не должно быть пустым Property: spring.cloud.gateway.routes[0].uri Value: null Reason: не должно равняться null - Assistant
Ваша конфигурация имеет некоторые проблемы с синтаксисом YAML, которые вызывают ошибки в Spring Boot. Давайте исправим конфигурацию, чтобы избежать этих ошибок.
Правильная структура YAML для определения маршрутов в Spring Cloud Gateway должна выглядеть следующим образом:
spring: application: name: gateway-service cloud: gateway: routes: - id: client uri: lb://client-service predicates: - Path=/api/client/**Основные проблемы в вашей изначальной конфигурации:
-
Неверное выравнивание:
uriиpredicatesдолжны быть под- id: client, а не на одном уровне. -
uriне должен быть равенnull, поэтому всегда указывайте его правильно. В приведённом примере мы используемlb://client-service, что указывает на использование Load Balancer с сервисомclient-service.
Убедитесь, что отступы выполнены правильно, так как YAML чувствителен к отступам.
-
- User
как добавить в конфигурацию существующий фильтр в виде класса
- Assistant
- User
java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name Filter
- Assistant
- User
так выглядит фильтр: package com.example.gateway.config;
import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono;
@Component public class Filter implements GatewayFilter { @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest();
if (request.getHeaders().containsKey("spring-cloud-course")) { request.mutate().headers(httpHeaders -> httpHeaders.remove("spring-cloud-course")); return chain.filter(exchange); } return onError(exchange); } private Mono<Void> onError(ServerWebExchange exchange) { ServerHttpResponse response = exchange.getResponse(); response.setStatusCode(HttpStatus.FORBIDDEN); return response.setComplete(); }}
- Assistant