依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>springcloud-demo</artifactId>
  7. <groupId>demo</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>demo-sentinel</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. <!--sentinel-->
  18. <dependency>
  19. <groupId>com.alibaba.cloud</groupId>
  20. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  21. </dependency>
  22. <!--健康监控-->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-actuator</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>com.alibaba.cloud</groupId>
  29. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>com.alibaba.cloud</groupId>
  33. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  34. </dependency>
  35. <!-- mybatis 和SpringBoot 整合-->
  36. <dependency>
  37. <groupId>org.mybatis.spring.boot</groupId>
  38. <artifactId>mybatis-spring-boot-starter</artifactId>
  39. </dependency>
  40. <!-- MySQL 驱动 -->
  41. <dependency>
  42. <groupId>mysql</groupId>
  43. <artifactId>mysql-connector-java</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>com.alibaba</groupId>
  47. <artifactId>druid</artifactId>
  48. </dependency>
  49. <!-- jdbc -->
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-starter-jdbc</artifactId>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-devtools</artifactId>
  57. <scope>runtime</scope>
  58. <optional>true</optional>
  59. </dependency>
  60. <dependency>
  61. <groupId>org.projectlombok</groupId>
  62. <artifactId>lombok</artifactId>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.springframework.boot</groupId>
  66. <artifactId>spring-boot-starter-test</artifactId>
  67. <scope>test</scope>
  68. </dependency>
  69. <dependency>
  70. <groupId>junit</groupId>
  71. <artifactId>junit</artifactId>
  72. </dependency>
  73. </dependencies>
  74. </project>

配置

  1. server:
  2. port: 8002
  3. spring:
  4. application:
  5. name: demo-nacos
  6. datasource:
  7. type: com.alibaba.druid.pool.DruidDataSource
  8. driver-class-name: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://192.168.2.20:30569/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
  10. username: root
  11. password: root
  12. cloud:
  13. nacos:
  14. server-addr: 192.168.2.20:30010
  15. username: nacos
  16. password: nacos
  17. sentinel:
  18. transport:
  19. dashboard: 192.168.2.20:31596 # 控制台的安装位置
  20. port: 8719 # 与sentinel单独连接的端口
  21. client-ip: 192.168.2.20 # 本机的ip,如果sentinel装在虚拟机,必须配这个
  22. # 暴露应用信息
  23. management:
  24. endpoints:
  25. web:
  26. exposure:
  27. include: '*'

接口示例

  1. package com.test.sentinel.controller;
  2. import com.alibaba.csp.sentinel.annotation.SentinelResource;
  3. import com.alibaba.nacos.common.model.RestResult;
  4. import com.test.sentinel.common.handler.CustomerBlockHandler;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. /**
  9. * @author jia
  10. * @since 2022-04-04 22:06
  11. */
  12. @RestController
  13. @RequestMapping("/sentinel")
  14. public class TestController {
  15. @GetMapping("/test1")
  16. @SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class,
  17. blockHandler = "handlerException")
  18. public RestResult test1() {
  19. return new RestResult(200, "success");
  20. }
  21. @GetMapping("/test2")
  22. @SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class,
  23. blockHandler = "handlerException2")
  24. public RestResult test2() {
  25. return new RestResult(200, "success");
  26. }
  27. }

流控处理类

  1. package com.test.sentinel.common.handler;
  2. import com.alibaba.csp.sentinel.slots.block.BlockException;
  3. import com.alibaba.nacos.common.model.RestResult;
  4. /**
  5. * @author jia
  6. * @since 2022-04-04 22:03
  7. */
  8. public class CustomerBlockHandler {
  9. public static RestResult handlerException(BlockException ex) {
  10. return new RestResult(40001, "自定义异常1");
  11. }
  12. public static RestResult handlerException2(BlockException ex) {
  13. return new RestResult(40002, "自定义异常2");
  14. }
  15. }