基础知识
- 概念
Spring Cloud Gateway 使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架。能够进行反向代理,鉴权,熔断,流量监控,熔断,日志监控等操作。
一般大型系统架构图
图片源自:尚硅谷视频教学
2.gateway的三大核心概念
- 路由 路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由
- 断言 参考的是java8的java.util.function.Predicate开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
- 过滤 指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
工作流程图:
gateway的使用
gateway三大核心概念
- 路由:是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由
- 断言 :参考的是java8的java.util.function.Predicate开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
- 过滤器 指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
gateway使用
添加依赖,网关也需要注册到注册中心
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
## 通过yml文件来配置路由,还有第二种方式通过代码来配置,这个可以放到后面再介绍
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由
- id: payment_routh2
uri: http://localhost:8001
predicates:
- Path=/payment/lb/** #断言,路径相匹配的进行路由
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
注意:
如果这里添加了starter-web依赖启动会报错:Consider defining a bean of type ‘org.springframework.http.codec.ServerCodecConfigurer’ in your configuration.
<!-- 需要把这个依赖移除 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
启动服务我们访问原本的地址:
配置了网关以后,我们访问9527:
同样我们可以访问成功, 因为我们访问这个地址网关帮我们转发到了http://localhost:8001。
gateway路由配置的第二种方式
// 通过编码的方式实现
@Configuration
public class GateConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
// 这里新增了一个路由规则,访问国内的时候会被转发到http://news.baidu.com/guonei
routes.route("path_rote_atguigu", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
return routes.build();
}
}
访问http://localhost:9527/guonei
gateway 配置动态路由
通过网关来统一暴露一个服名,这个服务名对应着多个服务,由网关转发来实现负载均衡等功能。
配置
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
routes:
- id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
#uri的协议为lb,表示启用Gateway的负载均衡功能。cloud-payment-service是eureka注册的名字
uri: lb://cloud-payment-service
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由
- id: payment_routh2
#uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://cloud-payment-service
predicates:
- Path=/payment/lb/** #断言,路径相匹配的进行路由
访问:http://localhost:9527/payment/lb 既可以轮训的访问到8001和8002
断言的使用
断言相当于是对请求进行限制,比如说某个路径匹配上了,对访问时间,请求头等进行限制。
官网地址
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=Before=2022-06-02T11:20:27.446+08:00[Asia/Shanghai] #在这个时间之前才能进行访问,注意这是美国时间,我们要换成中国时间,见下文。
获取时间:
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
这个时候再访问http://localhost:9527/payment/lb会报错,不允许访问。
Filter的使用
分为单个请求和全局过滤器
官网地址
全局过滤器的使用
// 一个案例,需要请求携带username参数否则不允许访问
@Slf4j
@Component
public class CustomFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("*********come in MyLogGateWayFilter: "+new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("username");
if(StringUtils.isEmpty(uname)){
log.info("*****用户名为Null 非法用户,(┬_┬)");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);//给人家一个回应
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
访问:http://localhost:9527/payment/lb?username=zs
如果我们不携带username参数:
特别说明
本文部分图片及代码来源于尚硅谷视频教学。