webContextUnify的作用

为了更好理解spring.cloud.sentinel.webContextUnify的作用,我直接提供一个详细的场景,包括完整的目录结构和URL路径。

假设我们有两个微服务应用:用户服务(UserService)和商品服务(ProductService)。它们的目录结构和URL路径如下:

  1. UserService:
/userapp
├── api
│   ├── users
│   │   ├── list
│   │   └── details
│   └── auth
│       ├── login
│       └── logout
└── internal
    └── health
  1. ProductService:
/productapp
├── api
│   ├── products
│   │   ├── list
│   │   └── details
│   └── categories
│       └── list
└── internal
    └── health

现在,让我们看看在不同 webContextUnify 设置下,Sentinel 如何处理这些路径:

当 webContextUnify = true (默认)

在这种情况下,Sentinel 会忽略根路径(/userapp 和 /productapp),但保留其余的路径结构。

UserService 路径:

/userapp/api/users/list -> /api/users/list
/userapp/api/users/details -> /api/users/details
/userapp/api/auth/login -> /api/auth/login
/userapp/internal/health -> /internal/health

ProductService 路径:

/productapp/api/products/list -> /api/products/list
/productapp/api/products/details -> /api/products/details
/productapp/api/categories/list -> /api/categories/list
/productapp/internal/health -> /internal/health

在这种配置下:
"/internal/health" 的规则会应用于两个服务的健康检查端点。

当 webContextUnify = false

此时,Sentinel 会考虑完整的URL路径,包括根路径。

UserService 路径保持不变:

/userapp/api/users/list
/userapp/api/users/details
/userapp/api/auth/login
/userapp/internal/health

ProductService 路径也保持不变:

/productapp/api/products/list
/productapp/api/products/details
/productapp/api/categories/list
/productapp/internal/health

在这种配置下:
健康检查端点 "/userapp/internal/health""/productapp/internal/health" 需要单独配置。

实际应用场景

  1. 使用 webContextUnify = true:
  • 适合当你想为多个服务的相似功能设置统一的规则。
  • 例如,可以为所有 “/api/*/list” 端点设置通用的限流规则,无论它们属于哪个服务。
  1. 使用 webContextUnify = false:
  • 适合当你需要精确控制每个服务的每个端点。
  • 例如,你可能想对 UserService 的登录接口 (/userapp/api/auth/login)
    设置特定的限流规则,而不影响其他服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

重剑DS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值