yml配置 gateway
在符合时间范围内才匹配
spring:
cloud:
gateway:
routes:
- id: before_route
uri: http://localhost:8001
predicates:
# 在某个时间之前的请求才会被转发到 http://localhost:8001,
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
# 在某个时间之后的请求才会被转发
# - After=2017-01-20T17:42:47.789-07:00[America/Denver]
# 在某个时间段之间的才会被转发
# - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
按表单名进行匹配
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: http://example.org
predicates:
# 名为chocolate的表单或者满足正则ch.p的表单才会被匹配到进行请求转发
- Cookie=chocolate, ch.p
按请求头匹配
spring:
cloud:
gateway:
routes:
- id: header_route
uri: http://example.org
predicates:
# 携带参数X-Request-Id或者满足\d+的请求头才会匹配
- Header=X-Request-Id, \d+
按Host主机名匹配
* 访问特定的主机名才能匹配,若网关的地址是localhost:8080, 主机名是qiying.com:8080(80端口被占了),那么只有访问http:qiying.com:8080/app/test 请求才会转发到localhost:8001/app/test。
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://localhost:8001
predicates:
- Host=qiying.com:8080
若直接访问localhost:8080/test 则网关会匹配失败
按请求方法匹配
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://example.org
predicates:
# 只有GET方法才会匹配
- Method=GET
按请求路径匹配
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://localhost:8001
predicates:
- Path=/app/{path}
# - Path=/app/test
结合一些filter更好用哦
GatewayFilter 配置例子
package com.ympay.gateway.filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
public class AuthSignFilter implements GatewayFilter,Ordered {
private final Logger logger = LoggerFactory.getLogger(AuthSignFilter.class);
@Override
public Mono < Void > filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return responseBody(ServerWebExchange exchange);
}
@Override
public int getOrder() {
// 数值越大 优先级越低
return 0;
}
private Mono < Void > responseBody(ServerWebExchange exchange) {
//返回错误提示
logger.info("商户密钥 签名错误 ");
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
ServerHttpResponse response = exchange.getResponse();
HttpHeaders httpHeaders = response.getHeaders();
httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
DataBuffer bodyDataBuffer = response.bufferFactory().wrap(ResultUtil.systemError("签名错误").toJSONString().getBytes());
return response.writeWith(Mono.just(bodyDataBuffer));
}
}
网关header 添加 base_url
package com.ymmobile.gateway.filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* @author jinghan
* @title: HeaderFilter
* @projectName mobilepay
* @description: 网关拦截器
* @date 2020/3/26 16:11
*/
@Component
public class HeaderFilter implements GlobalFilter, Ordered {
private static final Logger logger = LoggerFactory.getLogger(HeaderFilter.class);
/**
* 全局过滤器 核心方法
*
* @param exchange
* @param chain
* @return
*/
@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String scheme = exchange.getRequest().getURI().getScheme();
String host = exchange.getRequest().getURI().getAuthority();
exchange.getRequest().getHeaders();
String baseUrl = scheme+ "://" + host;
logger.info("baseUrl {}", baseUrl);
//向headers中放文件,记得build
ServerHttpRequest baseUrlHeader = exchange.getRequest().mutate().header("baseUrl", baseUrl).build();
//将现在的request 变成 change对象
ServerWebExchange build = exchange.mutate().request(baseUrlHeader).build();
return chain.filter(build);
}
@Override
public int getOrder() {
return -200;
}
}
本文由 风越清 创作,采用 知识共享署名4.0
国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: 三月 26,2020