简体   繁体   中英

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. And I would like this api to be available through gateway with 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. I am trying to write a simple gateway using spring cloud, configuration of which as following:

@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.

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

EDIT

As spencergibb mentioned that there are some options to do that. I started with RewritePath, subsequently my config has been changed as following:

@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

How can I change the final endpoint to: 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. You just have to set your endpoint as below

exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, yourEndpoint);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM