SpringCloud集成微服务API网关Gateway(详解)
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"))
.route("hystrix_route", r -> r.host("*.hystrix.org")
.filters(f -> f.hystrix(config -> config
.setName("hystrix_test")
.setFallbackUri("forward:/fallback")))
.uri("http://httpbin.org"))
.build();
}
}
这个配置类定义了几种不同类型的路由规则,包括基于路径的路由、基于主机名的路由、路径重写以及带有断路器的路由。每个路由都指向http://httpbin.org
这个公共测试服务,并且展示了如何使用Gateway的RouteLocatorBuilder API来定义路由。
评论已关闭