SpingCloud Gatway网关的架构
目录
1 基本介绍
1.1 课程视频
1.2 功能
2 应用gateway
2.1 加依赖
2.2 yml 文件配置
3 集成 sentinel 限流 某个微服务器限流 或 url 分组限流
3.1 添加依赖
3.2 配置文件yml
4 nacos管理配置 限流规则文件放入http://nacos-server:8848/nacos
4.1课程https://www.bilibili.com/video/BV173411P7M2?p=25&spm_id_from=pageDriver&vd_source=ff8b7f852278821525f11666b36f180a
4.2添加依赖
4.3在nacos中写配置文件
5 测试
6 Sentinel 监控限流 配置限流规则
6.1 课程视频
6.2 配置文件yml
6.3 运行程序发布到 服务器 上才能监控, 开发阶段要在开发的机器上安装jar包监控
6.4 查看监控和配置 http://sentinel-server:8858/
6.5 Sentinel 配置的文件 需要复制到nacos 不然重启后消失
1 基本介绍
1.1 课程视频
https://www.bilibili.com/video/BV173411P7M2?p=23&vd_source=ff8b7f852278821525f11666b36f180a
1.2 功能
1. 性能: 高可用, 负载均衡, 容错机制
2. 安全: 权限身份认证, 脱敏. 流量清洗, 后端签名 , 黑名单
3: 日志: 分布式 全链路跟踪记录
4: 限流
5: 路由规则
2 应用gateway
2.1 加依赖
<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.2 yml 文件配置
server:port: 80
spring:application:name: gateway-servercloud:nacos:discovery:server-addr: nacos-server:8848 # 修改本机的host 文件 cmd -> drivers -> etc -> host文件gateway:discovery:locator:enabled: truelower-case-service-id: true # admin-service ADMIN-SERVICE /admin-service/** -> 微服务(ADMIN-SERVICE) // 打开驼峰规则routes:- id: admin-service_routeruri: lb://admin-service # 转发到那个目的地predicates:- Path=/admin/** # 判断filters:- StripPrefix=1 # 当前端访问/admin/login - >login 将admin自动的去掉- id: member-service_routeruri: lb://member-service # 转发到那个目的地predicates:- Path=/user/**filters:- StripPrefix=1 # 当前端访问/user/login - >login 将user自动的去掉
3 集成 sentinel 限流 某个微服务器限流 或 url 分组限流
3.1 添加依赖
<!--限流--><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>
3.2 配置文件yml
sentinel: # 限流transport:dashboard: sentinel-server:8858 # sentinel-dashboard 放在ecs 里面datasource:ds1: # com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource 使用Nacos持久化我的sentinel 数据时,需要添加nacos-datasource的依赖nacos:server-Addr: nacos-server:8848group-id: DEFAULT_GROUPdataId: gw-flowruleType: gw_flowds2:nacos:serverAddr: nacos-server:8848group-id: DEFAULT_GROUPdataId: api-groupruleType: gw_api_group # 我们演示了sentinel-dashboard 的规则的定义,而且规则定义好了后,我们的网关能立马的感知到(生效)(nacos无法感知),但是我们下次重启,会丢失规则。--》nacos
4 nacos管理配置 限流规则文件放入http://nacos-server:8848/nacos
4.1课程https://www.bilibili.com/video/BV173411P7M2?p=25&spm_id_from=pageDriver&vd_source=ff8b7f852278821525f11666b36f180a
4.2添加依赖
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId></dependency>
4.3在nacos中写配置文件
5 测试
@RestController
public class GatewayFlowRulesController {/*** 获取当前系统的限流策略*/@GetMapping("/gw/flow/rules")public Set<GatewayFlowRule> getCurrentGatewayFlowRules(){ //java中的<>叫做泛型,括号里面的内容就是泛型的类型return GatewayRuleManager.getRules() ;}/*** 获取我定义的api分组*/@GetMapping("/gw/api/groups")public Set<ApiDefinition> getApiGroups(){return GatewayApiDefinitionManager.getApiDefinitions() ;}
}
6 Sentinel 监控限流 配置限流规则
6.1 课程视频
https://www.bilibili.com/video/BV173411P7M2?p=26&spm_id_from=pageDriver&vd_source=ff8b7f852278821525f11666b36f180a
6.2 配置文件yml
sentinel: # 限流transport:dashboard: sentinel-server:8858 # sentinel-dashboard 放在ecs 里面