1.网关简介
所谓的网关就是指系统的统一入口,它封装了运用程序的内部结构,为客户端提供统一的服务,一些与业务功能无关的公共逻辑可以在这里实现,诸如认证、鉴权、监控、路由转发等。
2.什么是spring cloud gateway
网关作为流量的入口,常用的功能包括路由转发、权限校验、限流等。
spring cloud gateway是spring cloud推出的第二代网关,是由WebFlux+Netty+Reactor实现的响应式的API网关,它不能在传统的servlet容器中工作,也不能构建成war包;旨在为微服务提供一种简单且有效的API路由的管理方式,并基于Filter的方式提供网关的基本功能,例如安全认证、监控、限流等。
spring cloud gateway功能特性:
(1)基于spring Framework5、Project Reactor和spring boot 2.0进行构建
(2)动态路由:能够匹配任何请求属性
(3)支持路径重写
(4)集成spring cloud服务发现功能(nacos)
(5)可集成流控级功能(sentinel)
(6)可以对路由指定易于编写的Predicate(断言)、Filter(过滤器)
2.1核心概念
路由(Route):
路由是网关中最重要的部分,路由信息包括一个ID、一个目的URL、一组断言工厂、一组Filter组成。如果断言为真,则说明请求的URL和配置的路由匹配。
断言(Predicate):
java8中的断言函数,spring cloud gateway中的断言函数类型是spring 5.0框架中的ServerWebExchange。断言函数运行开发者去定义匹配Http request中的任何信息,比如请求头和参数。
过滤器(Filter):
分为Gateway filter和Global filter,Filter可以对请求和响应进行处理。
3.Spring Cloud Gateway快速开始
(1)创建maven工程
(2)pom.xml中导入需要的依赖
<!-- gateway依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.1</version>
</dependency>
(3)application.properties中配置路由断言和过滤器
server.port=8086
spring.application.name=api-gateway
#gateway配置
#网关唯一标识,路由到order,routes是集合,使用数组索引来设置
spring.cloud.gateway.routes[0].id=order_route
#需要转发的地址
spring.cloud.gateway.routes[0].uri=http://localhost:8084
#断言规则,predicates也是一个集合,http://localhost:8086/order-serv/order/add 路由到
#http://localhost:8085/order-serv/order/add
spring.cloud.gateway.routes[0].predicates[0]=Path=/order-serv
@Component
public class CheckAuthRoutePredicateFactory extends AbstractRoutePredicateFactory<CheckAuthRoutePredicateFactory.Config> {
public static final String PARAM_KEY = "param";
public static final String REGEXP_KEY = "regexp";
public CheckAuthRoutePredicateFactory() {
super(CheckAuthRoutePredicateFactory.Config.class);
}
public List<String> shortcutFieldOrder() {
return Arrays.asList("param", "regexp");
}
public Predicate<ServerWebExchange> apply(CheckAuthRoutePredicateFactory.Config config) {
return new GatewayPredicate() {
public boolean test(ServerWebExchange exchange) {
if (!StringUtils.hasText(config.regexp)) {
return exchange.getRequest().getQueryParams().containsKey(config.param);
} else {
List<String> values = (List)exchange.getRequest().getQueryParams().get(config.param);
if (values == null) {
return false;
} else {
Iterator var3 = values.iterator();
String value;
do {
if (!var3.hasNext()) {
return false;
}
value = (String)var3.next();
} while(value == null || !value.matches(config.regexp));
return true;
}
}
}
public String toString() {
return String.format("Query: param=%s regexp=%s", config.getParam(), config.getRegexp());
}
};
}
@Validated
public static class Config {
@NotEmpty
private String param;
private String regexp;
public Config() {
}
public String getParam() {
return this.param;
}
public CheckAuthRoutePredicateFactory.Config setParam(String param) {
this.param = param;
return this;
}
public String getRegexp() {
return this.regexp;
}
public CheckAuthRoutePredicateFactory.Config setRegexp(String regexp) {
this.regexp = regexp;
return this;
}
}
}
(2)自定义的断言类名为CheckAuthRoutePredicateFactory,所以application.properties中使用CheckAuth作为断言规则配置
(3)修改CheckAuthRoutePredicateFactory类,定义静态类Config的字段,添加get和set方法,可以接收到application.properties中配置的CheckAuth的值
(4)结合中shortcutFieldOrder使用,添加name属性到集合中
(5)apply方法中获取Config中的name值,进行判断匹配,返回true或false
(6)访问系统可以正常访问
(7)当改了application.properties中的CheckAuth值后,访问不到服务
(8)完整自定义CheckAuthRoutePredicateFactory代码
package com.qingyun.predicate;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.server.ServerWebExchange;
import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import javax.validation.constraints.NotEmpty;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.server.ServerWebExchange;
@Component
public class CheckAuthRoutePredicateFactory extends AbstractRoutePredicateFactory<CheckAuthRoutePredicateFactory.Config> {
public CheckAuthRoutePredicateFactory() {
super(CheckAuthRoutePredicateFactory.Config.class);
}
public List<String> shortcutFieldOrder() {
return Arrays.asList("name");
}
public Predicate<ServerWebExchange> apply(CheckAuthRoutePredicateFactory.Config config) {
return new GatewayPredicate() {
public boolean test(ServerWebExchange exchange) {
if(config.getName().equals("qingyun")){
return true;
}
return false;
}
};
}
@Validated
public static class Config {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
7.Filter过滤器
官网参考:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories
(1)添加请求头AddRequestHeader
被调用接口中接收参数
参数传递成功
(2)为路由转发添加前缀PrefixPath
被调用服务需要配置context-path服务前缀
(3)RedirectTo重定向到其他服务,访问接口后跳转到配置的服务地址
8.自定义过滤器
注意:
类的命名需要以GatewayFilterFactory结尾;
必须使用spring的bean加载到容器中;
必须继承AbstractNameValueGatewayFilterFactory;
必须声明静态内部类,声明属性来接收配置文件中对应的断言信息;
需要结合shortcutFieldOrder进行绑定;
通过apply进行逻辑判断
(1)创建一个类CheckAuthGatewayFilterFactory,里面的处理代码,可以直接复制一份
RedirectToGatewayFilterFactory逻辑代码
(2)自定义的断言类名为CheckAuthGatewayFilterFactory,所以application.properties中使用CheckAuth作为过滤器配置
(3)修改CheckAuthGatewayFilterFactory类,定义静态类Config的字段,添加get和set方法,可以接收到application.properties中配置的CheckAuth的值
(4)结合中shortcutFieldOrder使用,添加value属性到集合中
(5)apply方法中获取Config中的value值,进行判断匹配,继续执行或者返回404状态
(6)不带参数或者带的参数不匹配时,访问不到系统
(7)当参数与application.properties中的CheckAuth匹配后,正常访问服务
(8)完整自定义CheckAuthGatewayFilterFactory代码
package com.qingyun.filter;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RedirectToGatewayFilterFactory;
import org.springframework.cloud.gateway.support.HttpStatusHolder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setResponseStatus;
@Component
public class CheckAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CheckAuthGatewayFilterFactory.Config> {
public CheckAuthGatewayFilterFactory() {
super(CheckAuthGatewayFilterFactory.Config.class);
}
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList("value");
}
@Override
public GatewayFilter apply(Config config) {
return new GatewayFilter() {
@Override
public Mono<Void> filter(ServerWebExchange exchange,
GatewayFilterChain chain) {
//获取到请求的参数值
String name = exchange.getRequest().getQueryParams().getFirst("name");
if(config.getValue().equals(name)){ //参数name的值等于配置的值
return chain.filter(exchange); //正常访问服务
}else{ //直接返回404
exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND); //设置状态码
return exchange.getResponse().setComplete(); //设置结束访问
}
}
};
}
public static class Config {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
9.自定义全局过滤器(Global Filters)
局部过滤器和全局过滤器区别:
局部:局部针对某个路由,需要在路由中进行配置
全局:针对所有路由请求,一旦配置就会投入使用
实现GlobalFilter接口,重写filter方法
@Component
public class GlobalLogFilter implements GlobalFilter {
Logger log = LoggerFactory.getLogger(this.getClass());
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("请求的路径:"+exchange.getRequest().getPath().value());
//直接返回验证通过
return chain.filter(exchange);
}
}
当访问接口时,全局过滤器拦截到请求信息
10.Gateway跨域配置(CORS Configuration)
官网参考:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#cors-configuration
跨域请求错误提示信息:在63342端口的页面调用8086端口的后台,出现跨域
在application.properties中配置:
#配置跨域允许(端口1的页面调用端口2的后台,出现跨域)
#允许跨域访问的资源:[
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsFilter(){
//配置允许的设置
CorsConfiguration config = new CorsConfiguration();
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
//配置添加到资源中
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("
@Configuration
public class GatewayConfig {
@PostConstruct
public void init(){
BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
@Override
public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable t) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("code", HttpStatus.TOO_MANY_REQUESTS);
map.put("message","被流控了");
return ServerResponse.status(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(map));
}
};
GatewayCallbackManager.setBlockHandler(blockRequestHandler);
}
}
此时在访问接口,被流控后返回自定义的流控信息
到此这篇关于Spring cloud alibaba之Gateway网关功能特征详解的文章就介绍到这了,更多相关Spring cloud alibaba内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!