springcloud gateway概念精讲
Java资料站
共 11876字,需浏览 24分钟
·
2021-05-23 13:21
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
作者 | 如何在3年拿到50K
来源 | urlify.cn/iYZZfe
一、定义
网关解决方案对比
基础说明
二、核心术语
Route路由
Predicate谓词、断言
Filter过滤器
三、简单原理
四、概念讲解
Route Predcate Factories
配置形式
模糊匹配和精确匹配
spring:
cloud:
gateway:
routes:
- id: {唯一标识}
uri: http://www.itmuch.com
spring:
cloud:
gateway:
routes:
- id: {唯一标识}
uri: http://www.itmuch.com/spring-cloud/spring-cloud-stream-pan-ta/
路由转发到微服务
routes:
- id: user_route
uri: lb://user-center
predicates:
- Path=/users/**
自定义predicate工厂
routes:
- id: after_route
uri: lb://user-center
predicates:
# 当且仅当请求时的时间After配置的时间时,才会转发到用户微服务
# 目前配置不会进该路由配置,所以返回404
# 将时间改成 < now的时间,则访问localhost:8040/** -> user-center/**
# eg. 访问http://localhost:8040/users/1 -> user-center/users/1
- After=2010-01-20T17:42:47.789-07:00[America/Denver]
- TimeBetween=00:00:00,12:44:13
@Data
public class TimeBetweenBean {
private String start;
private String end;
}
@Component
public class TimeBetweenRoutePredicateFactory
extends AbstractRoutePredicateFactory <TimeBetweenBean>{
public TimeBetweenRoutePredicateFactory() {
super(TimeBetweenBean.class);
}
/**
* 谓词工厂的核心方法,控制路由的判断人条件
* @param config
* @return
*/
@Override
public Predicate<ServerWebExchange> apply(TimeBetweenBean config) {
String start = config.getStart();
String end = config.getEnd();
return serverWebExchange ->{
LocalTime now = LocalTime.now();
System.out.println(now.isAfter(LocalTime.parse(start)));
return now.isAfter(LocalTime.parse(start)) && now.isBefore(LocalTime.parse(end));
};
}
/**
* 控制配置类和配置文件映射关系,和配置文件顺序一致
* @return
*/
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList("start","end");
}
}
discovery:
locator:
enabled: true # 开启从注册中心动态创建路由
三、Filter
生命周期
gateway自带过滤器讲解
1. RewritePath 请求地址重定向
pring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: https://example.org
predicates:
- Path=/red/**
filters:
- RewritePath=/red/?(?<segment>.*), /$\{segment}
2. PrefixPath 添加前缀
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: https://example.org
filters:
- PrefixPath=/mypath
3. StripPrefix 删除前缀
spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: https://nameservice
predicates:
- Path=/name/**
filters:
- StripPrefix=2
全局过滤器GlobalFilter
自定义过滤器工厂
自定义网关过滤器
@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("**** com int mylogglbalfiltert{}",new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if(uname==null){
log.info("非法用户");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); //返回状态码
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
@Configuration
public class GateWayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route("path_route_kouryoushine",
r->r.path("guonei").
uri("http://news.baidu.com/guonei")).build();
return routes.build() ;
}
}
全局过滤器
核心API
filters:
- AddRequestHeader=X-Request-Foo,Bar
- PreLog=a,b
@Slf4j
@Component
public class PreLogGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
@Override
public GatewayFilter apply(NameValueConfig config) {
log.info("请求建立了{}{}",config.getName(),config.getValue());
return (exchagne,chain)->{
log.info("请求建立了{}{}",config.getName(),config.getValue());
ServerHttpRequest request = exchagne.getRequest()
.mutate()
.build();
ServerWebExchange exchange = exchagne.mutate()
.request(request)
.build();
return chain.filter(exchange);
};
}
}
全局过滤器
全局监控
粉丝福利:Java从入门到入土学习路线图
👇👇👇
👆长按上方微信二维码 2 秒
感谢点赞支持下哈
评论