网关的使用
一、网关的概念
1.介绍
路由转发 + 执行过滤器链。
网关,旨在为微服务架构提供一种简单有效的统一的API路由管理方式。同时,基于Filter链的方式提供了网关的基本功能,比如:鉴权、流量控制、熔断、路径重写、黑白名单、日志监控等。
2.作用
- 反向代理(请求的转发)
- 路由和负载均衡
- 身份认证和权限控制
- 对请求限流
二、使用
1.加pom
<!--加入gateway的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2. 添加配置文件
spring:
cloud:
gateway:
routes:
- id: order
order: 1
uri: http://localhost:8081/
predicates:
- Path=/order/**
filters:
- StripPrefix=1
简化版
spring:
cloud:
gateway:
discovery:
locator:
enabled: true # 根据微服务的名字进行转发的 8808/order
lower-case-service-id: true # 微服务的名字小写
id,路由标识符,区别于其他 Route。唯一 不写 默认的唯一的值
uri,路由指向的目的地 uri,即客户端请求最终被转发到的微服务。
order,用于多个 Route 之间的排序,数值越小排序越靠前,匹配优先级越高。
predicate,断言的作用是进行条件判断,只有断言都返回真,才会真正的执行路由。
fifilter,过滤器用于修改请求和响应信息。
2.断言
Predicate(断言, 谓词) 用于进行条件判断,只有断言都返回真,才会真正的执行路由。
2.1设置断言
spring:
cloud:
gateway:
routes:
- id: order
order: 1
uri: http://localhost:8081/
predicates:
- Path=/order/**
- After=2019-12-31T23:59:59.789+08:00[Asia/Shanghai] #断言
filters:
- StripPrefix=1
2.2自定义断言
基于仅仅让age在(min,max)之间的人来访问。
config
@Component
public class AgeRoutePredicateFactory extends AbstractRoutePredicateFactory<AgeRoutePredicateFactory.Config> {
public static final String MIN_AGE = "minAge";
public static final String MAX_AGE = "maxAge";
public AgeRoutePredicateFactory() {
super(Config.class);
}
public List<String> shortcutFieldOrder() {
return Arrays.asList("minAge", "maxAge");
}
public Predicate<ServerWebExchange> apply(final Config config) {
// Assert.isTrue(config.getDatetime1().isBefore(config.getDatetime2()), config.getDatetime1() + " must be before " + config.getDatetime2());
return new GatewayPredicate() {
public boolean test(ServerWebExchange serverWebExchange) {
// 年龄是不是在最小值和最大值之间
// age 作为一个参数传过来
ServerHttpRequest request = serverWebExchange.getRequest();
List<String> age = request.getQueryParams().get("age");
System.out.println("________________________"+age);
String s = age.get(0);
int i = Integer.parseInt(s);
return i>=config.getMinAge()&&i<= config.getMaxAge(); // true false
}
};
}
@Validated
public static class Config {
private @NotNull Integer minAge;
private @NotNull Integer maxAge;
public Config() {
}
public Integer getMinAge() {
return minAge;
}
public void setMinAge(Integer minAge) {
this.minAge = minAge;
}
public Integer getMaxAge() {
return maxAge;
}
public void setMaxAge(Integer maxAge) {
this.maxAge = maxAge;
}
}
}
.yml配置
spring:
cloud:
gateway:
routes:
- id: order
order: 1
uri: http://localhost:8081/
predicates:
- Path=/order/**
- After=2019-12-31T23:59:59.789+08:00[Asia/Shanghai]
- Age=18,60
filters:
- StripPrefix=1
三、自定义全局过滤器
内置的过滤器已经可以完成大部分的功能,但是对于企业开发的一些业务功能处理,还是需要我们自己编写过滤器来实现的,那么我们一起通过代码的形式自定义一个过滤器,去完成统一的认证校验。
下面的我们自定义一个GlobalFilter,去校验所有请求的请求参数中是否包含“token”,如何不包含请求参数“token”则不转发路由,否则执行正常的逻辑
自定义全局过滤器 要求:必须实现GlobalFilter,Ordered接口
@Component
public class MyGlobaFile implements GlobalFilter, Ordered {
//过滤器
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//1 获取token
String token = exchange.getRequest().getHeaders().getFirst("token");
//2有token就让过 没有token就不让过
if (StringUtils.isNotBlank(token)) {
//成功 放行
return chain.filter(exchange);
}else{
// 返回json数据
Map map =new HashMap<>();
map.put("code",5000);
map.put("msg","错误!!!");
map.put("data","50000");
// 转化为json数据
String s = JSONUtil.toJsonStr(map);
// 返回json数据
ServerHttpResponse response = exchange.getResponse();
//
// Mono<Void> voidMono = response.setComplete();
DataBuffer dataBuffer;
try {
byte[] bytes = s.getBytes("utf8");
dataBuffer = response.bufferFactory().wrap(bytes);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return response.writeWith(Mono.just(dataBuffer));
}
}
//优先级
@Override
public int getOrder() {
return 0;
}
}