当前位置:网站首页>Nacos/sentinel网关限流和分组 (代码)
Nacos/sentinel网关限流和分组 (代码)
2022-04-23 06:23:00 【cqwoniu】
1.网关
1. 网关又称为网间连接器、协议转换器,在网络层以上实现网络互连,仅适用于两个高层协议不同的网络互连;
2.在OCI协议上来看:网关分为两类,一种是面向连接的网关,一种是无连接的网关;
3.在TCP协议上来看,网关实质上是一个网络通向其他网络地址的IP地址;不同的网络的主机处在不同的网络里,要实现通信,必须要通过网关。是只有设置好网关的IP地址,TCP/IP才能实现不同网络之间的相互通信。
2.API网关
API网关是一个服务器,是系统的唯一入口,API网关封装了系统的内部架构,为客户端提供了一个定制的API。Spring Cloud Gateway 是为微服务架构提供的一种简单有效的统一的API路由管理方式

3.网关应当具备的功能
1.性能:API高可用、负载均衡,容错机制
2.安全:权限身份认证、脱敏、流量清洗、后端签名(保证全链路可信调用),黑名单(非法调用的限制)
3.日志:日志记录,一旦涉及分布式,全链路跟踪必不可少
4.缓存:数据缓存
5.监控:记录请求响应数据,api耗时分析,性能监控
6.限流:流量控制,错峰流控,可以定义成多种限流规则
- 网关维度:针对某个微服务整体
粒度粗:全局配置 - API分组的维度:针对一系列URL地址 ,可以不在一个微服务里面
粒度细:局部配置
添加依赖:
<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>
修改配置文件application.yml:
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
添加限流规则gw-flow.json
[
{
"resource": "资源的名称:网关时routes.id,api分组:api分组的名称",//
"resourceMode": 0 , //0是网关,1是
"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"
}
]
}
]
添加Controller获取限流规则
@RestController
public class FlowRulesController {
@GetMapping("/gw/flow/rules")
public Set<GatewayFlowRule> getGatewayFlowRules(){
return GatewayRuleManager.getRules() ;
}
@GetMapping("/gw/api/groups")
public Set<ApiDefinition> getApiGroupRules(){
return GatewayApiDefinitionManager.getApiDefinitions();
}
}
- 使用的动态规则的数据源 (使用nacos配置)
sentinel:
transport:
dashboard: sentinel-server:8858 # sentinel-dashboard 放在ecs 里面
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持久化规则,需要添加依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
7.灰度:线上灰度部署,可以减小风险
8.路由:动态路由规则
4. nginx与gatewau的区别:
1.nginx,是作为整个全局的网关,是对外的,出于最外层的;gateway,更像是业务网关,主要用来对应不同的客户端提供服务的,用于聚合业务的。各个微服务独立部署,职责单一,对外提供服务的时候需要有一个东西把业务聚合起来
2.nginx是用不同的语言编写的,不宜与扩展,而gateway就不同,它像是java写的,易于扩展和维护
5. 使用Spring Cloud Gateway
1.在gateway-server里添加依赖
<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>
2.在gateway-server里面添加启动类
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class ,args) ;
}
}
3.在gateway-server里面添加配置文件
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/** filters: - StripPrefix=1 # 当前端访问/admin/login - >login 将admin自动的去掉 - id: member-service_router uri: lb://member-service # 转发到那个目的地 predicates: - Path=/user/** filters: - StripPrefix=1 # 当前端访问/user/login - >login 将user自动的去掉 - id: finance-service_router uri: lb://finance-service # 转发到那个目的地 predicates: - Path=/finance/** filters: - StripPrefix=1 # 当前端访问/finance/xx,去掉finance - id: exchange-service_router # 交易系统的路由 uri: lb://exchange-service # 转发到那个目的地 predicates: - Path=/exchange/** filters: - StripPrefix=1 # 当前端访问/exchange/xx,exchange - id: test_router uri: http://www.aliyun.com predicates: - Path=/product # /test->http://www.baidu.com 网关自己内部会构建请求去访问我们要访问的地址 GET:http://www.baidu.com/test # 通过网关来完成对2 种维度的限流: 网关维度 + API 分组 sentinel: transport: dashboard: sentinel-server:8858 # sentinel-dashboard 放在ecs 里面 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 # 我们演示了sentinel-dashboard 的规则的定义,而且规则定义好了后,我们的网关能立马的感知到(生效)(nacos无法感知),但是我们下次重启,会丢失规则。--》nacos redis: host: redis-server port: 6380 password: Ltd3411??
版权声明
本文为[cqwoniu]所创,转载请带上原文链接,感谢
https://blog.csdn.net/cqwoniu/article/details/120226032
边栏推荐
猜你喜欢

Jupyter Notebook 安装

数据分析入门 | kaggle泰坦尼克任务(四)—>数据清洗及特征处理

Meishe helps Baidu "Duka editing" to make knowledge creation easier
![[Educational Codeforces Round 80] 解题报告](/img/54/2fd298ddce3cd3e28a8fe42b3b8a42.png)
[Educational Codeforces Round 80] 解题报告

H5案例开发

Typora操作技巧说明(一).md

manjaro安装与配置(vscode,微信,美化,输入法)

Javscript gets the real suffix of the file

简单易懂的子集dp

Patrol inspection intercom communication system in power industry
随机推荐
Dirichlet 前缀和(数论优化式子复杂度利器)
PC端一次启动多个微信
LATEX使用
后台管理系统框架,总有你想要的
特殊成员与魔法方法
可视化常见问题解决方案(八)共享绘图区域问题解决方案
记录一个查询兼容性的网站,String.replaceAll()兼容性报错
Intelligent communication solution of Hainan Phoenix Airport
H5 case development
DMR system solution of Kaiyuan MINGTING hotel of Fengqiao University
数论之拓展欧几里得
青龙面板拉库命令更新【2022/4/20】收藏不走丢
Meishe helps Baidu "Duka editing" to make knowledge creation easier
F-牛妹的苹果树(直径合并)
Failed to install Tui editor, quick solution
Machine vision series (01) -- Overview
Object. Create() principle, object Create() specification, handwritten object create(),Object. Create() usage
Pycharm
How does the public and Private Integrated walkie talkie realize cooperative work under multi-mode communication?
HuggingFace