栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloudGateway的两种使用方式,结合StripPrefixFilterFilterFactory和PrefixPathGatewayFilterFactory核心代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloudGateway的两种使用方式,结合StripPrefixFilterFilterFactory和PrefixPathGatewayFilterFactory核心代码

Gateway的作用

Spring Cloud Gateway 组件的核心是通过一系列的过滤器,通过这些过滤器可以将客户端发送的请求转发(路由)到对应的微服务。

核心配置
  • route:由id、uri、predicate、filters组成
  • predicate:即断言,匹配路由的部件,支持正则表达式
  • filters:过滤器,在断言匹配后执行,对路由信息进行操作,也可以操作header、参数等
方法一(配置文件) 1. 依赖

注意:由于gateway默认依赖webflux,所以不需要加入springboot-web的依赖,否则会报错;
同时,本次实验使用的是Zookeeper作为注册中心,所以不需要加入eureka-client的依赖,否则启动报错




	org.springframework.cloud
	spring-cloud-starter-gateway



	org.springframework.cloud
	spring-cloud-starter-zookeeper-discovery
	
	
		
			org.apache.zookeeper
			zookeeper
		
	


	org.springframework.cloud
	spring-cloud-starter-zookeeper-config

2. yml
server:
  port: 9527

spring:
  profiles:
	active:
  application:
	name: springcloud-gateway
  cloud:
	gateway:
	  routes:
		- id: devilvan-route-strip
		  uri: lb://devilvan-caches/caches
		  predicates:
			# 匹配满足条件的路由
			- Path=/caches/cachesController
						PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory();
						PathRoutePredicateFactory.Config pathRouteConfig = pathRoutePredicateFactory.newConfig();
						List pattern4StripPrefix = new ArrayList<>();
						pattern4StripPrefix.add("/caches/cachesController
						StripPrefixGatewayFilterFactory stripPrefixGatewayFilterFactory = new StripPrefixGatewayFilterFactory();
						StripPrefixGatewayFilterFactory.Config stripConfig = stripPrefixGatewayFilterFactory.newConfig();
						stripConfig.setParts(1);
						stripPrefixGatewayFilterFactory.apply(stripConfig);
						
						predicateSpec.predicate(pathRoutePredicateFactory.apply(pathRouteConfig))
								.filters(new Function() {
									@Override
									public UriSpec apply(GatewayFilterSpec gatewayFilterSpec) {
										return gatewayFilterSpec.filter(stripPrefixGatewayFilterFactory.apply(stripConfig));
									}
								});
						return predicateSpec.uri("lb://devilvan-caches/caches");
					}
				})
				.route("devilvan-route-prefix", new Function() {
					@Override
					public Route.AsyncBuilder apply(PredicateSpec predicateSpec) {
						PathRoutePredicateFactory pathRoutePredicateFactory = new PathRoutePredicateFactory();
						PathRoutePredicateFactory.Config pathRouteConfig = pathRoutePredicateFactory.newConfig();
						List pattern4PrefixPath = new ArrayList<>();
						pattern4PrefixPath.add("/**");
						pathRouteConfig.setPatterns(pattern4PrefixPath);
						// filters
						PrefixPathGatewayFilterFactory prefixPathGatewayFilterFactory = new PrefixPathGatewayFilterFactory();
						PrefixPathGatewayFilterFactory.Config prefixPathConfig = prefixPathGatewayFilterFactory.newConfig();
						prefixPathConfig.setPrefix("/cachesController");
						// 组装predicate和filter
						predicateSpec.predicate(pathRoutePredicateFactory.apply(pathRouteConfig))
								.filters(new Function() {
									@Override
									public UriSpec apply(GatewayFilterSpec gatewayFilterSpec) {
										return gatewayFilterSpec.filter(prefixPathGatewayFilterFactory.apply(prefixPathConfig));
									}
								});
						return predicateSpec.uri("lb://devilvan-caches/caches");
					}
				})
				.build();
	}
}
StripPrefixFilterFactory核心代码

PrefixPathGatewayFilterFactory

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/943430.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号