通过Dapr实现一个简单的基于.net的微服务电商系统(十六)——dapr+sentinel中间件实现服务保护

今天的案例我们简单的模拟一下使用sentinel的限流能力来对我们下游服务特定接口提供保护能力,依然使用我们目前这套电商demo来完成,由于sentinel是对下游服务进行保护,所以我们需要将相关的规则写入到我们的apigateway对应的sidecar中,这样确保来自于客户端的请求都会被正确的限制流量,接下来我们来限制一下对accountservice的accountquery/checkrolebasedaccesscontroler这个接口做流量限制,按照1秒10次的方式,component如下:

复制代码
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: sentinel
namespace: dapreshop
spec:
type: middleware.http.sentinel
version: v1
metadata:

  • name: appName
    value: “accountflowrule”
  • name: logDir
    value: “/tmp”
  • name: flowRules
    value: >-
    [
    {
    “resource”: “POST:/v1.0/invoke/accountservice/method/accountquery/checkrolebasedaccesscontroler”,
    “threshold”: 10,
    “tokenCalculateStrategy”: 0,
    “controlBehavior”: 0
    }
    ]
    复制代码
      这条规则告诉dapr我们需要启动sentinel中间件,并且注入一条规则规定对下游资源(resource)的访问维持在10次/秒(threshold),其中流量控制器的Token计算策略(tokenCalculateStrategy)采用默认也就是以threshold作为阈值,超出的请求部分采用的策略(controlBehavior)是拒绝服务。接着我们创建一个Configuration并注入到我们的apigateway这个deployment中:

复制代码
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: sentinelconfig
namespace: dapreshop
spec:
httpPipeline:
handlers:
- name: sentinel
type: middleware.http.sentinel
复制代码
复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
name: apigateway
namespace: dapreshop
spec:
selector:
matchLabels:
app: apigateway
replicas: 1
template:
metadata:
labels:
app: apigateway
version: v1
annotations:
dapr.io/enabled: “true”
dapr.io/app-id: “apigateway”
dapr.io/app-port: “80”
dapr.io/config: “sentinelconfig”

USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值