首先要复习一下Gateway
服务网关在微服务架构中充当了请求访问入口的角色,是非常重要的一个部分,在高并发的系统中我们通常会在网关层通过流控降级等手段把多余的请求拒绝在外来防止微服务被高并发请求打垮。
alibaba专门为gateway提供了一个适配包“spring-cloud-alibaba-sentinel-gateway”
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel-datasource-nacos 后续sentinel做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel gateway -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- SpringCloud Ailibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
server:
port: 8408
spring:
application:
name: cloudalibaba-sentinel-gateway
cloud:
nacos:
discovery:
server-addr: 119.91.99.85:8849
sentinel:
transport:
dashboard: localhost:8080 #配置Sentinel dashboard地址
port: 8719 #应用与Sentinel控制台交互的端口
datasource:
ds1:
nacos: #nacos限流持久配置
server-addr: 119.91.99.85:8849 #nacos地址
dataId: ${spring.application.name} #获取限流的数据源的dataId
groupId: DEFAULT_GROUP #获取限流的数据源的分组
data-type: json #数据类型
rule-type: flow #规则类型
eager: true #取消懒加载
gateway:
discovery:
locator:
lower-case-service-id: true # 表明gateway开启服务注册和发现的功能
routes: #添加路由规则
- id: nacos-payment-provider #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: lb://nacos-payment-provider #匹配后提供服务的路由地址
predicates:
- Path=/payment/nacos/** # 断言,路径相匹配的进行路由
management:
endpoints:
web:
exposure:
include: '*'
在这里插入代码片