|
|
@@ -0,0 +1,63 @@
|
|
|
+package anyi.space.memberConsumer.config;
|
|
|
+
|
|
|
+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;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ProjectName: distributedSystemLearn
|
|
|
+ * @FileName: GatewayConfig
|
|
|
+ * @Author: 杨逸
|
|
|
+ * @Data:2025/9/10 16:33
|
|
|
+ * @Description: gateway配置类
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+public class GatewayConfig {
|
|
|
+ @Bean
|
|
|
+ public RouteLocator myRouteLocator01(RouteLocatorBuilder builder){
|
|
|
+ //根据配置文件改写为配置类的形式
|
|
|
+ RouteLocatorBuilder.Builder routes = builder.routes();
|
|
|
+ routes.route("member10001",r-> {
|
|
|
+ return r
|
|
|
+ //路径断言
|
|
|
+ .path("/member-gateway/member")
|
|
|
+ .and()
|
|
|
+ //请求方法断言
|
|
|
+ .method(HttpMethod.GET)
|
|
|
+ //过滤器重写路径
|
|
|
+ .filters(fn ->{
|
|
|
+ //路径重写
|
|
|
+ return fn.rewritePath("/member-gateway/(?<segment>.*)","/member-provider/$\\{segment}");
|
|
|
+ })
|
|
|
+ //转发目标地址
|
|
|
+ .uri("http://localhost:10001");
|
|
|
+
|
|
|
+ });
|
|
|
+ return routes.build();
|
|
|
+ }
|
|
|
+ @Bean
|
|
|
+ public RouteLocator myRouteLocator02(RouteLocatorBuilder builder){
|
|
|
+ RouteLocatorBuilder.Builder routes = builder.routes();
|
|
|
+ routes.route("member10002",r->{
|
|
|
+ //自定义断言
|
|
|
+ //r.predicate(new GatewayPredicate() {
|
|
|
+ // @Override
|
|
|
+ // public boolean test(ServerWebExchange serverWebExchange) {
|
|
|
+ // //断言是否为post请求方法
|
|
|
+ // HttpMethod.POST.equals(serverWebExchange.getRequest().getMethod());
|
|
|
+ // serverWebExchange.getRequest().getPath();
|
|
|
+ // return false;
|
|
|
+ // }
|
|
|
+ //});
|
|
|
+ return r.path("/member-gateway/member")
|
|
|
+ .and()
|
|
|
+ .method(HttpMethod.POST)
|
|
|
+ .filters(fn->{
|
|
|
+ return fn.rewritePath("/member-gateway/(?<segment>.*)","/member-provider/$\\{segment}");
|
|
|
+ }).uri("http://localhost:10002");
|
|
|
+ });
|
|
|
+ return routes.build();
|
|
|
+ }
|
|
|
+}
|