核心知识点:
API 网关(Gateway)诞生背景(why):
没有网关,增加客户端代码和配置的复杂性,每个服务在调用时,都需要独立的认证。为了简化调用的逻辑,简化内部服务调用的复杂程度,所以提出了网关的概念;
网关(what):提供了一个各种服务访问的入口
市场服务网关
(Spring Cloud Gateway,zuul,…)
API Gateway实现服务的保护和转发(重点)
How:
1.创建模块,配置pom文件、yml文件:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
server:
port: 9000
spring:
application:
name: sca-gateway
cloud:
gateway:
routes: #配置网关路由规则
- id: route01 #路由id,自己指定一个唯一值即可
uri: http://localhost:8081/ #网关帮我们转发的url
predicates: ###断言(谓词):匹配请求规则
- Path=/nacos/provider/echo/** #请求路径定义,此路径对应uri中的资源
filters: ##网关过滤器,用于对谓词中的内容进行判断分析以及处理
- StripPrefix=1 #转发之前去掉path中第一层路径,例如nacos
id:路由的名字
uri:真实的网址
predicates:规则(符合才可以转发)
filters:过滤器
注:Gateway 服务的启动底层是通过谁去实现的?(Netty网络编程框架-ServerSocket)
API Gateway层面的负载均衡实现(重点,lb://sca-provider)
How:
1.添加nacos配置
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2.修改yml文件
server:
port: 9000
spring:
application:
name: sca-gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true #开启通过服务注册中心的serviceId创建路由
routes:
- id: route01
##uri: http://localhost:8081/
uri: lb://sca-provider # lb为服务前缀(负载均衡单词的缩写),不能随意写
predicates: ###匹配规则
- Path=/nacos/provider/echo/**
filters:
- StripPrefix=1 #转发之前去掉path中第一层路径,例如nacos
uri: lb://sca-provider # lb为服务前缀(负载均衡单词的缩写),不能随意写
API Gateway请求处理原理分析(重点:官方图,了解关键代码,执行过程)
客户端向Spring Cloud Gateway发出请求。 如果Gateway Handler Mapping 通过谓词predicates(predicates)的集合确定请求与路由(Routers)匹配,则将其发送到Gateway Web Handler。 Gateway Web Handler 基于路由配置调用过滤链中的过滤器(也就是所谓的责任链模式)进一步的处理请求。 Filter由虚线分隔的原因是, Filter可以在发送请求之前和之后执行拓展逻辑。
- 面试问题:
- 网关层面是如何实现负载均衡的?(通过服务名去查找具体的服务实例)
- 网关层面是如何通过服务名查找服务实例的?(Ribbon)
- 你了解Ribbon中的哪些负载均衡算法?(轮询,权重,hash,……可通过IRule接口进行查看分析)
- 网关进行请求转发的流程是怎样,有哪些关键对象?(XxxHandlerMapping,Handler,。。。)
- 网关层面服务的映射方式怎样的?(谓词-path,…,服务名/服务实例)
- 网关层如何记录服务的映射?(通过map,并要考虑读写锁的应用)
API Gateway中常用谓词(predicate)对象及实践。
what:
断言,为真才会执行路由
why:
内置工厂实现了RoutePredicateFactory接口
server:
port: 9000
spring:
application:
name: sca-gateway
cloud:
nacos:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true #开启通过服务中心的serviceId 创建路由的功能
routes:
- id: bd-id
##uri: http://localhost:8081/
uri: lb://sca-provider
predicates: ###匹配规则
- Path=/nacos/provider/echo/**
- Before=2021-01-30T00:00:00.000+08:00
- Method=GET
filters:
- StripPrefix=1 # 转发之前去掉1层路径
- 面试问题:
- 何为谓词?(网关中封装了判断逻辑的一个对象)
- 谓词逻辑的设计是怎样的?(谓词判断逻辑返回值为true则进行请求转发)
- 你了解哪些谓词逻辑?(path,请求参数,请求方式,请求头,….)
- 我们可以自己定义谓词工厂对象吗?(可以的)
API Gateway中过滤器(Filter)的类型及实践。
what:
过滤器(Filter)就是在请求传递过程中,对请求和响应做一个处理。
Gateway 的Filter从作用范围可分为两种:GatewayFilter与GlobalFilter。
GatewayFilter:应用到单个路由或者一个分组的路由上。
GlobalFilter:应用到所有的路由上(例如负载均衡过滤器,请求转发过滤器等)。
其余了解
API Gateway中基于sentinel实现服务限流
配置方法:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
sentinel:
transport:
dashboard: localhost:8180 #Sentinel 控制台地址
eager: true #取消Sentinel控制台懒加载,即项目启动即连接
How:API,routeID
API Gateway中基于自定义限流结果的处理(GatewayCallbackManager)。
@Configuration
public class GatewayConfig {
public GatewayConfig(){//构造方法
GatewayCallbackManager.setBlockHandler( new BlockRequestHandler() {//调用方法
@Override
public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {//重写
Map<String,Object> map=new HashMap<>();
map.put("state",429);
map.put("message","two many request");
String jsonStr=JSON.toJSONString(map);//转json串
return ServerResponse.ok().body(Mono.just(jsonStr),String.class);
}
});
}
}
- 面试
- 网关层面结合sentinel实现限流,其限流的类型有几种?(两种-route id,api)
- 网关层面可以自定义限流后的异常处理结果吗?(可以)
- 你知道Sentinel底层限流的算法有哪些?(滑动窗口,令牌桶,漏斗,。。。)
总结(Summay)
重难点分析
网关(Gateway)诞生的背景?(第一:统一微服务访问的入口,第二:对系统服务进行保护,第三进行统一的认证,授权,限流)
网关的选型?(Netifix Zuul,Spring Cloud Gateway,…)
Spring Cloud Gateway的入门实现(添加依赖,路由配置,启动类)
Spring Cloud Gateway中的负载均衡?(网关服务注册,服务的发现,基于uri:lb://服务id方式访问具体服务实例)pom+yml
Spring Cloud Gateway中的断言配置?(掌握常用几个就可,用时可以通过搜索引擎去查)
Spring Cloud Gateway中的过滤器配置?(掌握过滤器中的两大类型-局部和全局)
Spring Cloud Gateway中的限流设计?(Sentinel)
FAQ 分析
Gateway在互联网架构中的位置?(nginx->gateway–>微服务–>微服务)
Gateway底层负载均衡的实现?(Ribbon)
Gateway应用过程中设计的主要概念?(路由id,路由uri,断言,过滤器)
Gateway中你做过哪些断言配置?(after,header,path,cookie,…)
Gateway中你用的过滤器有哪些?(添加前缀,去掉前缀,添加请求头,…,负载均衡,…)