Spring Cloud Gateway - URL의 하위 부분 전체를 프록시/전송
Spring Cloud Gateway 2.0.0을 사용하고 있습니다.M6가 심플 게이트웨이를 테스트하고 있습니다.** regex를 사용하여 URL을 다른 URL로 전송하고 싶습니다.
예 1: /integration/sbl/foo/bar => localhost: 4178/a-integration/sbl/foo/bar
예 2: /integration/sbl/baz/bad => localhost: 4178/a-integration/sbl/baz/bad
지금까지 다음과 같이 기술했습니다만, http://localhost:4178/a-integration/ 에만 전송 됩니다.
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
String test = "http://localhost:4178/a-integration/";
return builder.routes()
.route("integration-test",
r -> r.path("/integration/sbl/**")
.uri(test)
)
.build();
}
위의 코드를 수정하여 이 동작을 활성화하려면 어떻게 해야 합니까?
편집
아래의 답변을 바탕으로 다음을 시도했습니다.
String samtykke = "http://localhost:4178/";
return builder.routes()
.route("samtykke", r -> r
.path("/gb-integration/sbl/**")
.filters(f -> f.rewritePath("/gb-integration/sbl/(?<segment>.*)", "/gb-samtykke-integration/${segment}"))
.uri(samtykke))
.build();
GET http://localhost:4177/gb-integration/sbl/api/sbl/income/을 시도했는데 http://localhost:4178/gb-samtykke-integration/api/sbl/income/을 기대했지만 작동하지 않았습니다.
출력은 다음과 같습니다.
2018-02-23 09:46:35.197 TRACE 6364 --- [ctor-http-nio-2] o.s.c.g.h.p.RoutePredicateFactory : Pattern "/gb-integration/sbl/**" matches against value "[path='/gb-integration/sbl/api/sbl/income/']"
2018-02-23 09:46:35.198 DEBUG 6364 --- [ctor-http-nio-2] o.s.c.g.h.RoutePredicateHandlerMapping : Route matched: samtykke
2018-02-23 09:46:35.198 DEBUG 6364 --- [ctor-http-nio-2] o.s.c.g.h.RoutePredicateHandlerMapping : Mapping [Exchange: GET http://localhost:4177/gb-integration/sbl/api/sbl/income/] to Route{id='samtykke', uri=http://localhost:4178/, order=0, predicate=org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$245/1803714790@1d0042df, gatewayFilters=[OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory$$Lambda$247/485237151@77da026a, order=0}]}
2018-02-23 09:46:35.200 DEBUG 6364 --- [ctor-http-nio-2] o.s.c.g.handler.FilteringWebHandler : Sorted gatewayFilterFactories: [OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@5c534b5b}, order=-1}, OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory$$Lambda$247/485237151@77da026a, order=0}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@396639b}, order=10000}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@a18649a}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@2b22a1cc}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@62573c86}, order=2147483647}]
2018-02-23 09:46:35.232 TRACE 6364 --- [ctor-http-nio-2] o.s.c.g.filter.RouteToRequestUrlFilter : RouteToRequestUrlFilter start
2018-02-23 09:46:35.314 TRACE 6364 --- [ctor-http-nio-1] o.s.c.g.filter.NettyWriteResponseFilter : NettyWriteResponseFilter start
패스 필터에서는, 다음의 메뉴얼에 기재되어 있는 대로, rewritePath 기능을 사용할 수 있습니다.
https://cloud.spring.io/spring-cloud-gateway/reference/html/ #syslogpath-syslogfilter-factory
관련 부품:
5.12 RewritePath GatewayFilter 팩토리
RewritePath GatewayFilter Factory는 path regexp 파라미터와 치환 파라미터를 사용합니다.이 경우 Java 정규 표현을 사용하여 요청 경로를 유연하게 다시 씁니다.
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: http://example.org
predicates:
- Path=/foo/**
filters:
- RewritePath=/foo/(?<segment>.*), /$\{segment}
요청 경로가 /foo/bar인 경우 다운스트림 요청을 수행하기 전에 경로를 /bar로 설정합니다.YAML 사양으로 인해 $로 대체되는 Barracuda에 주목하십시오.
이 예에서는 다음과 같습니다.
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
String test = "http://localhost:4178";
return builder.routes()
.route("integration-test", r -> r
.path("/integration/sbl/**")
.filters(f->f.rewritePath("/integration/(?<segment>.*)","/a-integration/${segment}"))
.uri(test)
)
.build();
}
여기서도 같은 문제가 발생하고 있습니다.Boyen의 응답에 동의하지만 "uri" 파라미터가 URI의 "path" 컴포넌트를 무시한다는 점을 지적하는 것이 도움이 될 수 있습니다.이것은 설명서에 명확하지 않기 때문에(혹은 아직 발견되지 않았기 때문에) 다른 사람에게 도움이 되었으면 합니다.
/foo에서 수신한 모든 요청을 http://example.org/bar으로 리다이렉트한다고 가정합니다.
예: /foo/x/y/z --> http://example.org/bar/x/y/z
예를 들어, 이것은 예상대로 동작합니다.
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: http://example.org
predicates:
- Path=/foo/**
filters:
- RewritePath=/foo/(?<segment>.*), /bar/$\{segment}
이것은 예상대로 동작하지 않습니다(/bar는 무시합니다).
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: http://example.org/bar
predicates:
- Path=/foo/**
filters:
- RewritePath=/foo/(?<segment>.*), /$\{segment}
아래에서는 풀 셋업과 함께 두 가지 유형의 구성을 찾아보십시오.두 방법 모두 동일한 결과를 생성합니다.
셋업:
- 하고 있다
http://localhost:8090
- 패스라고 하는 패스
/context
로서 기능합니다. - 라는 서비스라는
my-resources
로의 실행http://localhost:8091/my-resources
★★★★★★★★★★★★★★★★★./my-resources
파라미터 없이 호출되며 모든 리소스가 반환됩니다.됩니다.RID
하는 경우)
변수 없음)가 「」( 「」)에 .http://localhost:8090/context/my-resources/
's "uri's 'uri's 'uri's 'uri'로 전송됩니다.http://localhost:8091/my-resources/
.
1: 사용방법 1: 사용방법application.yml
spring:
cloud:
gateway:
routes:
- id: route_id
predicates:
- Path=/context/my-resources/**
filters:
- RewritePath=/context/my-resources/(?<RID>.*), /my-resources/$\{RID}
uri: http://localhost:8091
방법 2: Java와 같은 설정 사용
@Bean
public RouteLocator routes(RouteLocatorBuilder routeBuilder) {
return routeBuilder.routes()
.route("route_id",
route -> route
.path("/context/my-resources/**")
.filters(f -> f.rewritePath("/context/my-resources/(?<RID>.*)", "/my-resources/${RID}"))
.uri("http://localhost:8091")
)
.build();
}
rewritePath를 사용할 때 contextPath를 변경하는 기능이 Spring Boot 2.3.X 변경으로 인해 파손된 것으로 보입니다.Rewrite Path Gateway Filter Factory 입니다.구성에는 Hoxton에서 새 contextPath를 전달하기 위한 필드가 없습니다.SR7+.
회피책으로 커스텀 RewritePathGatewayFilter를 생성하여 다음과 같이 사용했습니다.
ServerHttpRequest request = exchange.getRequest().mutate().contextPath(newContextPath).path(newPath).build();
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI());
return chain.filter(exchange.mutate().request(request).build());
언급URL : https://stackoverflow.com/questions/48865174/spring-cloud-gateway-proxy-forward-the-entire-sub-part-of-url
'code' 카테고리의 다른 글
Ajax를 사용하여 게시하는 동안 로드 이미지 표시 (0) | 2023.02.27 |
---|---|
Woocommerce 체크아웃 페이지에 배송지 주소가 먼저 표시되고 청구지 주소가 표시됩니까? (0) | 2023.02.27 |
다음 Oracle 오류는 무엇을 의미합니까: 잘못된 열 색인 (0) | 2023.02.27 |
React의 다른 반환문으로 여러 줄 JSX를 반환하려면 어떻게 해야 합니까? (0) | 2023.02.27 |
WP_Query에서 WooCommerce 특집 제품 가져오기 (0) | 2023.02.27 |