## 加依赖 ``` <!--alibaba sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> ``` ## 加配置 ![](https://img.kancloud.cn/d1/88/d1883e4443bbafc9548e2d90bf665666_1358x423.png) ![](https://img.kancloud.cn/aa/c3/aac30276fa2eaac7e1e9c31f6b11e726_1919x544.png) ## 加资源名称 * 接口名称默认是controller暴露的接口地址名称(方法级别) * 也可以按服务名称设计规则,以下小demo,按照服务名称加的资源名称 ![](https://img.kancloud.cn/e9/78/e978c4fe116107d15626c5128de9b7f2_1611x576.png) * 按照服务名称加的资源名称,需要使用@SentinelResource注解配合使用资源名称 ![](https://img.kancloud.cn/cb/57/cb57fb8ef499bdb8169cb4fbf3457c83_1920x873.png) ## 根据资源设置流控规则 ![](https://img.kancloud.cn/59/51/5951439d8778473ae90d2acc7f0713bd_1460x390.png) ## 测试流控api ### 正常结果 ![](https://img.kancloud.cn/96/ac/96ac3cbb7742ea10ace4c3d896c9ea36_1902x506.png) ### 异常结果 ![](https://img.kancloud.cn/5d/70/5d703821d39000671be55d2a50fb1c56_1908x533.png) ## 查看sentinel-dashboard ![](https://img.kancloud.cn/8a/b6/8ab69919f9071eea5ed8624d798d87cb_1897x898.png) ![](https://img.kancloud.cn/d0/e8/d0e8169dca63382be90a831122af85e3_1920x847.png) ![](https://img.kancloud.cn/1f/d2/1fd271d6819b8114d2f0ab20aef4a204_1901x581.png) ![](https://img.kancloud.cn/a9/b3/a9b3d5f2ff91580e7dc6738f10c13ebf_1920x567.png) ## 查看sentinel日志 ![](https://img.kancloud.cn/c4/f9/c4f9bee14b2b6d53d683e10539e90955_941x528.png) cat sentinel-record.log.2019-10-23.0.1 |more ``` 2019-10-23 14:56:20.634 INFO [FlowRuleManager] Flow rules loaded: {} 2019-10-23 14:56:20.664 INFO App name resolved: user-center 2019-10-23 14:56:20.666 INFO [SentinelConfig] JVM parameter overrides csp.sentinel.charset: UTF-8 -> UTF-8 2019-10-23 14:56:20.667 INFO [SentinelConfig] JVM parameter overrides csp.sentinel.flow.cold.factor: 3 -> 3 2019-10-23 14:56:20.667 INFO [SentinelConfig] Application type resolved: 0 2019-10-23 14:56:20.671 INFO [MetricWriter] Creating new MetricWriter, singleFileSize=52428800, totalFileCount=6 2019-10-23 14:56:20.674 INFO [DynamicSentinelProperty] Config will be updated to: [FlowRule{resource=user-center, limitApp=default, grade=1, count=1.0, strategy=0, refResource=null, controlBehavior=0, warmUpPer iodSec=10, maxQueueingTimeMs=500, clusterMode=false, clusterConfig=null, controller=null}] 2019-10-23 14:56:20.679 INFO [FlowRuleManager] Flow rules received: {user-center=[FlowRule{resource=user-center, limitApp=default, grade=1, count=1.0, strategy=0, refResource=null, controlBehavior=0, warmUpPeri odSec=10, maxQueueingTimeMs=500, clusterMode=false, clusterConfig=null, controller=com.alibaba.csp.sentinel.slots.block.flow.controller.DefaultController@6f78c132}]} 2019-10-23 15:00:43.809 INFO Add child <sentinel_default_context> to node <machine-root> ```