1,概述
一句话总结:SpringCloud Gateway使用的是Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架。
2,gateway能做什么
反向代理
鉴权
流量控制
熔断
日志监控
...
3,微服务中的网关位置
非阻塞异步!非阻塞异步!非阻塞异步!
4,三大核心概念
路由(route)
路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如断言为true则匹配该路由
断言(predicate)
参考的是Java8的java.util.function.Predicate
开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
过滤(filter)
指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改.
5,gateway工作流程
核心逻辑 : 路由转发+执行过滤器链
6,Gateway网关路由有两种配置方式:
在配置文件yaml中配置:
server:
port: 9527
spring:
application:
name: cloud-gateway-service
cloud:
gateway:
routes: #多个路由
- id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
# uri: http://localhost:8001 #匹配后提供服务的路由地址
uri: lb://cloud-payment-service #路由的ID,没有固定规则但要求唯一,建议配合服务名
predicates: #断言
- Path=/payment/getById/** #断言 路径相匹配的进行路由
- id: payment_routh2
# uri: http://localhost:8001
uri: lb://cloud-payment-service
predicates:
- Path=/payment/lb/**
discovery:
locator:
enabled: true #开启从注册中心动态生成路由的功能,用微服务名进行路由
eureka:
instance:
hostname: cloud_gateway_service
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
代码中注入RouteLocator的Bean
package com.tigerhhzz.springcloud.config;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author tigerhhzz
* @date 2022/6/16 11:29
*/
@Configuration
public class GateWayConfig {
/*
* 配置了一个id为route-name的路由规则
* 当访问地址http://localhost:9527/guonei时自动转发到地址:http://news.baidu.com/guonei
* */
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
RouteLocatorBuilder.Builder routes = builder.routes();
routes.route("path_route",r->r.path("/guonei").uri("http://news.baidu.com/guonei"));
routes.route("path_route1",r->r.path("/guoji").uri("http://news.baidu.com/guoji"));
return routes.build();
}
}
7,开启注册中心动态创建路由,通过服务名实现动态路由
8,常用的8种gateway路由断言(Route Predicate)
C:\Users\Administrator>curl http://localhost:9527/payment/lb
{"timestamp":"2022-06-16T12:37:14.285+0000","path":"/payment/lb","status":404,"error":"Not Found","message":null,"requestId":"ee95c33b"}
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8001
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8002
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8001
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8002
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8001
C:\Users\Administrator>
总结:Predicate就是为了实现一组匹配规则, 让请求过来找到对应的Route进行处理!!
9,Filter的使用
实现两个接口 implments GlobalFilter,OrderId
filter的作用是 全局日志记录和统一网关鉴权 等等等
自定义过滤器
package com.tigerhhzz.springcloud.filter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Date;
/**
* @author tigerhhzz
* @date 2022/6/16 21:05
*/
@Component
public class MyGateWayFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println("**************come in MylogGateWayGilter: " + new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if (uname == null) {
System.out.println("********用户名为空,非法用户。");
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?uname=z3
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135763.html