【Spring Cloud】Gateway的配置与使用
warning:
这篇文章距离上次修改已过205天,其中的内容可能已经有所变动。
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("path_route", r -> r.path("/get")
.uri("http://httpbin.org"))
.route("host_route", r -> r.host("*.myhost.org")
.uri("http://httpbin.org"))
.route("rewrite_route", r -> r.host("*.rewrite.org")
.filters(f -> f.rewritePath("/foo/(?<segment>.*)", "/${segment}"))
.uri("http://httpbin.org"))
.build();
}
}
这个配置类定义了三个路由规则:
path_route
使用路径匹配来匹配请求,然后将请求转发到指定的URL。host_route
使用主机名匹配来匹配请求,然后将请求转发到指定的URL。rewrite_route
使用主机名匹配来匹配请求,并且使用了重写路径的过滤器,然后再将请求转发到指定的URL。这个例子展示了如何使用正则表达式来匹配和重写路径。
评论已关闭