网关Gateway断⾔+过滤器整合注册中心Nacos项目实战
一、前言
提示:本文讲到的代码部分来自上文
上文链接地址
提示:以下是本篇文章正文内容,下面案例可供参考
二、网关介绍
API Gateway,是系统的唯一对外的入口,介于客户端和服务器端之间的中间层,处理非业务功能 提供路由请求、鉴权、监控、缓存、限流等功能
- 主流的网关
- zuul:是Netflix开源的微服务网关,和Eureka,Ribbon,Hystrix等组件配合使用,依赖组件比较多,性能教差
- kong: 由Mashape公司开源的,基于Nginx的API gateway
- nginx+lua:是一个高性能的HTTP和反向代理服务器,lua是脚本语言,让Nginx执行Lua脚本,并且高并发、非阻塞的处理各种请求
- springcloud gateway: Spring公司专门开发的网关,替代zuul
三、基本网关转发
1、创建Gateway项目
添加依赖
<!--gateway依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2、配置
server:
port: 8888
spring:
application:
name: api-gateway
cloud:
gateway:
routes: #数组形式
- id: spring-other2 #路由唯一标识,这里自定义,一般是服务名
uri: http://127.0.0.1:9000 #想要转发到的地址
order: 1 #优先级,数字越小优先级越高
predicates: #断言 配置哪个路径才转发,前端转发的前缀路径
- Path=/spring-other2/**
filters: #过滤器,请求在传递过程中通过过滤器修改
- StripPrefix=1 #去掉第一层前缀(例如下面的spring-other2)
#访问路径 http://localhost:8888/spring-other2/other2/getusername4?name=sb
#转发路径 http://localhost:9000/other2/getusername4?name=sb
#需要过滤器去掉前面第一层
效果图
四、整合注册中心Nacos
1、添加Nacos依赖
<!--添加nacos客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2、启动类开启支持
@EnableDiscoveryClient
3、修改配置文件
server:
port: 8888
spring:
application:
name: api-gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos地址
gateway:
routes: #数组形式
- id: spring-other2 #路由唯一标识,这里自定义,一般是服务名
# uri: http://127.0.0.1:9000 #想要转发到的地址
uri: lb://spring-other2 #想要转发到的地址,从nacos转发进行,后面跟服务名(lb负载均衡的意思)
order: 1 #优先级,数字越小优先级越高
predicates: #断言 配置哪个路径才转发,前端转发的前缀路径
- Path=/spring-other2/**
filters: #过滤器,请求在传递过程中通过过滤器修改
- StripPrefix=1 #去掉第一层前缀(例如下面的spring-other2)
discovery:
locator:
enabled: true #开启网关拉取nacos服务
#访问路径 http://localhost:8888/spring-other2/other2/getusername4?name=sb
#转发路径 http://localhost:9000/other2/getusername4?name=sb
#需要过滤器去掉前面第一层
Gateway内置的路由断言
predicates:
- Host=
- Path=
- Method=
- Header=
- Query=
- Cookie=
4、网关访问的代码
@RestController
@RequestMapping("other2")
public class OtherController2 {
/**
* 测试gateway负载
* @return
*/
@RequestMapping("getusername8")
String getusername8( HttpServletRequest request) {
return "other2的getUserName7反回的数据-由"+request.getServerName()+":"+request.getServerPort()+"发起";
}
}
效果图是一种默认轮询
五、Gateway内置断言实现接口定时下线与强制参数
- 需求:接口需要在指定时间进行下线,过后不可以在被访问
- 使用Before ,只要当前时间小于设定时间,路由才会匹配请求
- 东8区的2020-09-11T01:01:01.000+08:00后,请求不可访问
predicates:
- Before=2020-09-09T01:01:01.000+08:00
强制参数,如果没有source参数则拒绝访问
predicates:
- Query=source
六、自定义全局过滤器实现鉴权
网关不要加太多业务逻辑,否则会影响性能
//实现GlobalFilter, Ordered接口
@Component
public class UserGlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//业务逻辑
//从请求中获取token
String token = exchange.getRequest().getHeaders().getFirst("token");
System.out.println(token);
//如果token为空的业务逻辑
if(StringUtils.isBlank(token)){
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
//如果成功继续往下执行
return chain.filter(exchange);
}
//数字越小,优先级越高
@Override
public int getOrder() {
return 0;
}
}
七、SpringCloud+gateway+nacos整合踩过的坑
path不能和服务名一样,例如服务名为spring-other2则path可以叫api-spring-other2不能为spring-other2,否则routes配置无效
predicates:
- Path=