🔎这里是【微服务~Sentinel】,关注我学习微服务不迷路
👍如果对你有帮助,给博主一个免费的点赞以示鼓励
欢迎各位🔎点赞👍评论收藏⭐️
【微服务~Sentinel】 目前主要更新微服务,一起学习一起进步。
本期主要介绍Sentinel之dashboard控制面板
Sentinel Dashboard 是一个独立的项目,sentinel-dashboard-1.8.4.jar,需要使用 java -jar 运行
java -jar -Dserver.port=18080 sentinel-dashboard-1.8.4.jar
下载地址
添加坐标(已有)
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
- dependency>
配置yml
- #server.port=8071
- #spring.application.name=service-consumer
- #spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
- #spring.cloud.sentinel.transport.dashboard=192.168.152.153:8080
- #端口号
- server:
- port: 8071
-
- spring:
- application:
- name: service-consumer #服务名
- cloud:
- nacos:
- discovery:
- server-addr: 127.0.0.1:8848 #nacos服务地址
- sentinel:
- transport:
- dashboard: 127.0.0.1:18080
- feign:
- sentinel:
- enabled: true
-
测试
先访问资源 http://localhost:8071/feign/echo/123
dashboard 登录
查看控制面板 http://localhost:18080/
通过 @SentinelResource 注解,设置监控点(定义控制资源、配置控制策略)
- package com.czxy.nacos.controller;
-
- import com.alibaba.csp.sentinel.annotation.SentinelResource;
- import com.czxy.nacos.feign.TestFeign;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
-
- import javax.annotation.Resource;
-
-
- @RestController
- @RequestMapping("/feign")
- public class TestFeignController {
- @Resource
- private TestFeign testMyFeign;
-
- @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
- @SentinelResource("/feign/echo")
- public String echo(@PathVariable String str) {
- return testMyFeign.echo(str);
- }
- }
-
测试
- package com.czxy.controller;
-
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
-
- @RestController
- @RequestMapping("/user")
- public class UserController {
-
- @GetMapping("/login")
- public String login(String str) {
- return "登录成功" + str;
- }
-
-
- @GetMapping("/register")
- public String register(String str) {
- return "注册成功";
- }
- }
通过@SentinelResource
注解的blockHandler
属性制定限流的处理函数
- package com.czxy.nacos.controller;
-
- import com.alibaba.csp.sentinel.annotation.SentinelResource;
- import com.alibaba.csp.sentinel.slots.block.BlockException;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/user")
- public class UserController {
-
- @GetMapping("/login")
- // 限流设置
- @SentinelResource(value="login", blockHandler = "loginBlockHandler")
- public String login(String str) {
- return "登录成功" + str;
- }
-
- public String loginBlockHandler(String str , BlockException e) {
- return str + ": 请稍后重试";
- }
-
- @GetMapping("/register")
- public String register(String str) {
- return "注册成功";
- }
- }
-
运行 sentinel-dashboard-1.8.4.jar
通过 dashboard 设置限流
连续快速2次访问测试功能
使用@SentinelResource注解的fallback属性来指定降级的方法名
- package com.czxy.controller;
-
- import com.alibaba.csp.sentinel.annotation.SentinelResource;
- import com.alibaba.csp.sentinel.slots.block.BlockException;
- import org.apache.commons.lang.math.RandomUtils;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
-
- @RestController
- @RequestMapping("/user")
- public class UserController {
-
- @GetMapping("/login")
- // 限流设置
- @SentinelResource(value="login", blockHandler = "loginBlockHandler")
- public String login(String str) {
- return "登录成功" + str;
- }
-
- public String loginBlockHandler(String str , BlockException e) {
- return str + ": 请稍后重试";
- }
-
-
- @GetMapping("/register")
- // 熔断降级
- @SentinelResource(value="register", fallback = "registerFallback")
- public String register(String str) {
- int r = RandomUtils.nextInt(10);
- if(r < 5) {
- int i = 1 / 0;
- }
- return "注册成功";
- }
-
- public String registerFallback(String str) {
- return str + ": 熔断降级";
- }
- }
成功
熔断降级
慢调用比例:
RT:平均响应时间
比例阈值:
熔断时长:
最小请求数:
异常比例:每秒异常总数占通过量的比值超过阈值(DegradeRule 中的 count)之后,资源进入降级状态
异常数:当资源近 1 分钟的异常数目超过阈值之后会进行熔断
限流是通过设置QPS(每秒查询率)/线程数,将超过阈值部分拒绝处理;
服务降级是监控请求响应时间、响应异常比例、异常数量;超过限定阈值,将进行服务降级熔断,一定时间内不可用;