[英]Is it possible to change the service endpoint path on gateway using spring cloud
I have a running api on my local machine with url http://localhost:8080/gnk-debt/service/taxpayer-debt.我在本地机器上运行 api url http://localhost:8080/gnk-debt/service/taxpayer-debt。 And I would like this api to be available through gateway with url http://localhost:8243/gnk/service/phystaxpayer/debt/v1 .
我希望这个 api 可以通过网关使用 url http://localhost:8243/gnk/service/phystaxpayer/debt/v1 。 To do so, first I set the port in property file.
为此,我首先在属性文件中设置端口。 But I am not able set custom url for the api endpoint.
但我无法为 api 端点设置自定义 url。 I am trying to write a simple gateway using spring cloud, configuration of which as following:
我正在尝试使用 spring 云编写一个简单的网关,其配置如下:
@Configuration
public class SpringCloudConfig {
@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder routeLocatorBuilder)
{
return routeLocatorBuilder.routes()
.route("gnkTaxpayerDebt", rt -> rt.path("/gnk-debt/**")
.uri("http://localhost:8080/"))
.build();
}
}
But at the end, gateway api endpoint is available at: http://localhost:8243/gnk-debt/service/taxpayer-debt.但最后,网关 api 端点位于:http://localhost:8243/gnk-debt/service/taxpayer-debt。
The question that I am curios about is that if it is possible to change gateway api endpoint to: http://localhost:8243/gnk/service/phystaxpayer/debt/v1我好奇的问题是,如果可以将网关 api 端点更改为: http://localhost:8243/gnk/service/phystaxpayer/debt/v1
EDIT编辑
As spencergibb mentioned that there are some options to do that.正如spencergibb提到的那样,有一些选择可以做到这一点。 I started with RewritePath, subsequently my config has been changed as following:
我从 RewritePath 开始,随后我的配置已更改如下:
@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder routeLocatorBuilder)
{
return routeLocatorBuilder.routes()
.route("gnkTaxpayerDebt", rt -> rt.path("/gnk/**")
.filters(f->f.rewritePath("/gnk/service/phystaxpayer/debt/v1(?<remains>.*)","/${remains}"))
.uri("http://localhost:8080"))
.build();
}
At the end I am able to access my gateway endpoint at http://localhost:8243/gnk/service/phystaxpayer/debt/v1/ gnk-physicaltaxpayer-debt/gnk/service/physical-taxpayer-debt最后,我可以访问我的网关端点 http://localhost:8243/gnk/service/phystaxpayer/debt/v1/ gnk-physicaltaxpayer-debt/gnk/service/physical-taxpayer-debt
How can I change the final endpoint to: http://localhost:8243/gnk/service/phystaxpayer/debt/v1如何将最终端点更改为:http://localhost:8243/gnk/service/phystaxpayer/debt/v1
One of the ways of setting your own endpoint is to create a GlobalFilter where you change the attribute "GATEWAY_REQUEST_URL_ATTR" that comes with ServerWebExchangeUtils.设置您自己的端点的方法之一是创建一个 GlobalFilter,您可以在其中更改 ServerWebExchangeUtils 附带的属性“GATEWAY_REQUEST_URL_ATTR”。 You just have to set your endpoint as below
您只需要如下设置端点
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, yourEndpoint);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.