Sentinel 集群限流

6月 28, 2023 |

Sentinel集群限流 官方文档非常含糊。通过查看源代码及调试sentinel-demo-cluster-embedded 总结如下

整体流程

xyz对应示例中的 appName, javacoder对应示例中的namespace.
使用从xyz-cluster-map 提取的ip和port连接token Server同步作用域的流控规则
SphU.entry()执行时查询配置的FlowRule, 发现是集群模式后提取flowId,根据flowId 获取作用域流控规则进行javacoder-flow-rules真正判断。

各个配置文件

xyz-flow-rules

单机的流控规则, 指定资源名, 集群模式和对应的flowId,需要和命名空间中的一致。

[
    {
        "resource": "sayHello",
        "clusterMode": true,
        "clusterConfig":{
            "flowId":123456
        }
    }
]

xyz-cluster-map

ipport定义初始machineId定义初始token server,而ip和port 是给token client连接token server使用的。 clientSet 记录集群内成员。

[
	{
		"clientSet": [
			"192.168.1.138@8729",
			"192.168.1.138@8727"
		],
		"ip": "192.168.1.138",
		"machineId": "192.168.1.138@8728",
		"port": 11111
	}
]

javacoder-flow-rules

以下是javacoder namespace对应的流控规则

[
    {
        "resource": "sayHello",
        "count": 0,
        "grade": 1,
        "limitApp": "default",
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": true,
        "clusterConfig":{
            "flowId":123456,
            "sampleCount":1,
            "windowIntervalMs":2000
        }
    }
]

Posted in: WEB开发

Comments are closed.