隨着微服務的流行,服務和服務之間的穩定性變得愈來愈重要。Sentinel 是面向分佈式服務架構的輕量級流量控制產品,主要以流量爲切入點,從流量控制、熔斷降級、系統負載保護等多個維度來幫助您保護服務的穩定性。java
打開cmd執行 java -jar sentinel-dashboard-1.7.2.jarweb
在瀏覽器訪問 http://localhost:8080/ ,咱們能夠看到以下頁面spring
用戶名密碼都是sentineljson
<dependencies> <dependency><!-- 引入本身定義的api通用包,能夠使用Payment支付Entity --> <groupId>com.gh.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <!--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> <!--openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- SpringBoot整合Web組件+actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.6.3</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
springboot核心配置文件application.ymlapi
server: port: 8401 spring: application: name: cloudalibaba-sentinel-service cloud: nacos: discovery: server-addr: localhost:8848 #Nacos服務註冊中心地址 sentinel: transport: dashboard: localhost:8080 #配置Sentinel dashboard地址 port: 8719 datasource: ds1: nacos: server-addr: localhost:8848 dataId: ${spring.application.name} groupId: DEFAULT_GROUP data-type: json rule-type: flow management: endpoints: web: exposure: include: '*'
@SpringBootApplication @EnableDiscoveryClient public class MainApp8401 { public static void main(String[] args) { SpringApplication.run(MainApp8401.class,args); } }
啓動項目訪問 http://localhost:8080/#/dashboard/home 咱們便可看到sentinel已經將服務進行監控瀏覽器
在sentinel控制檯流控規則處添加springboot
當1秒內連續點擊時就出現以下圖所示狀況架構
在sentinel控制檯降級規則處添加app
熱點參數限流會統計傳入參數中的熱點參數,並根據配置的限流閾值與模式,對包含熱點參數的資源調用進行限流,熱點參數限流能夠看作是一種特殊的流量控制,僅對包含熱點參數的資源生效.分佈式
blockHandler指定的deal_hotKey表明兜底方法
@GetMapping("testHotKey") @SentinelResource(value = "testHotKey",blockHandler = "deal_hotKey") public String testHotKey(@RequestParam(value = "p1",required = false) String p1, @RequestParam(value = "p2",required = false) String p2){ return "test HotKey"; } public String deal_hotKey(String p1, String p2, BlockException b){ return "deal_hotKey o(╥﹏╥)o"; }
在sentinel控制檯進行配置熱點規則
方法testHotKey裏面第一個參數只要QPS超過每秒一次,立刻降級處理,就會執行咱們本身設置的兜底方法
需求: 咱們指望p1參數當它是某個特殊值時,它的限流值和平時不同.
點擊編輯進行高級選項設置.
引入nacos作持久化所需依賴
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
相關配置信息
在nacos中進行配置
[ { "resource":"/rateLimit/byUrl", "limitApp":"default", "grade":1, "count":1, "strategy":0, "controlBehavior":0, "clusterMode":false } ]
配置信息: