环境搭建告一段落后,现在就是项目的搭建了,我们首先需要一个入口,一个所有请求的入口,这个入口就是我们的Api网关,那么什么是我们项目的网关,什么样的组件可以拿来就用呢,就是这个,Spring Cloud Gateway。为什么用这个不用Nginx,都是当入口,其实Spring Cloud Gateway更适合当服务的转发,用来对不同的业务做一个聚合。而Nginx适合对外,在最外层。
先说一下Api的作用。
- 性能:API高可用,负载均衡,容错机制。
- 安全:权限身份认证、脱敏,流量清洗,后端签名(保证全链路可信调用),黑名单(非法调用的限制)。
- 日志:日志记录(spainid,traceid)一旦涉及分布式,全链路跟踪必不可少。
- 缓存:数据缓存。
- 监控:记录请求响应数据,api耗时分析,性能监控。
- 限流:流量控制,错峰流控,可以定义多种限流规则。
- 灰度:线上灰度部署,可以减小风险。
- 路由:动态路由规则。
然后记录一下怎么和项目结合起来。
首先搭建我们的工程,需要这两个模块authorization-server gateway-server
用来认证客户的登录信息以及,对登录的信息拦截,配置我们的限流规则
先说gateway-server,在pom中引入我们需要的包
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
这只是引入了我们的基础包,就是说我们现在是有了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>
先上配置
server:
port: 80
spring:
application:
name: gateway-server
cloud:
nacos:
discovery:
server-addr: nacos-server:8848 # 修改本机的host 文件
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true # admin-service ADMIN-SERVICE /admin-service/** -> 微服务(ADMIN-SERVICE)
routes:
- id: admin-service_router
uri: lb://admin-service # 转发到那个目的地
predicates:
- Path=/admin/**
- id: test_router
uri: http://www.aliyun.com
predicates:
- Path=/product
# http://www.baidu.com/test
# http://www.aliyun.com/product
sentinel:
filter:
enabled: true
datasource:
ds1.file:
file: classpath:router-flow.json
ruleType: gw-flow
ds2.file:
file: classpath:api-flow.json #api-flow.json 接口的分组
ruleType: gw_api_group
可以注意到我们的Sentinel配置中有两个datasource,配置如下
gw-flow.json
[
{
"resource": "admin-service_router",
"resourceMode": 0 ,
"count": 2,
"intervalSec": 60
},
{
"resource": "admin-service-api",
"resourceMode":"1",
"count": 1,
"intervalSec": 60
}
]
api-group.json
[
{
"apiName": "admin-service-api",
"predicateItems": [
{
"pattern": "/admin/login"
}
]
}
]
这是两个不同的限流规则,针对请求限流和针对路由限流,
resource:资源名称,可以是网关中的 route 名称或者用户自定义的 API 分组名称。
resourceMode:规则是针对 API Gateway 的 route(RESOURCE_MODE_ROUTE_ID)还是用户在 Sentinel 中定义的 API 分组(RESOURCE_MODE_CUSTOM_API_NAME),默认是 route。
grade:限流指标维度,同限流规则的 grade 字段。
count:限流阈值
intervalSec:统计时间窗口,单位是秒,默认是 1 秒。
简单的限流就配置好了,我们请求访问时,超过了限制就访问不到了。
我们现在的限流配置是通过json的方式写在代码中,这一点也不智能,也不优美。是的,我们需要持久化。这就是我们需要Nacos的原因,
添加依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
然后修改我们的配置
datasource:
# ds1.file:
# file: classpath:gw-flow.json # 网关 + API分组的限流
# ruleType: gw_flow
ds1.nacos: #com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource 使用Nacos持久化我的sentinel 数据时,需要添加nacos-datasource的依赖
serverAddr: nacos-server:8848
dataId: gw-flow
ruleType: gw_flow
#
# ds2.file:
# file: classpath:api-group.json
# ruleType: gw_api_group
ds2.nacos:
serverAddr: nacos-server:8848
dataId: api-group
ruleType: gw_api_group
接下来就是去我们搭建好的nacos上配置我们的限流规则了
搭建好,点击发布,OK
记得分组也加上
好了,我们的简单限流就完成了。不用写代码,我们的功能就达到了。