当前位置:   article > 正文

Sentinel简单实现_sentinel启动命令

sentinel启动命令

一:下载、安装、运行Sentinel

1.window

下载:

           GitHub上面的官方下载地址:Releases · alibaba/Sentinel · GitHub

          或 sentinel-dashboard-1.7.0.jar百度网盘 请输入提取码 提取码:0rlf

安装:在Sentinel的jar包目录地址栏中输入cmd,点击回车打开DOS窗口,如下图:

 输入java -jar Sentinel的jar包名称,回车之后就启动了Sentinel,如下图:

如果出现端口已经被占用的提示,那就需要指定端口启动,即输入java -jar Sentinel的jar包名称 --server.port=端口号,点击回车就可以启动sentinel了

访问:

在地址栏输入http://localhost:端口号(默认是8080)就可以访问了,初始用户名和密码都是sentinel,如下图:

 进入操作台:

  

 2.linux

下载:

启动

执行 nohup java -jar sentinel-dashboard-1.7.1.jar & 命令,后台启动 Sentinel 控制台。通过查看 nohup.out 日志输入,如果有如下内容,说明启动成功:

 二:添加项目

1.在 pom.xml 文件中,引入相关依赖

  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. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.2.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <modelVersion>4.0.0</modelVersion>
  12. <artifactId>lab-46-sentinel-demo</artifactId>
  13. <dependencies>
  14. <!-- 实现对 SpringMVC 的自动化配置 -->
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-web</artifactId>
  18. </dependency>
  19. <!-- Sentinel 核心库 -->
  20. <dependency>
  21. <groupId>com.alibaba.csp</groupId>
  22. <artifactId>sentinel-core</artifactId>
  23. <version>1.7.1</version>
  24. </dependency>
  25. <!-- Sentinel 接入控制台 -->
  26. <dependency>
  27. <groupId>com.alibaba.csp</groupId>
  28. <artifactId>sentinel-transport-simple-http</artifactId>
  29. <version>1.7.1</version>
  30. </dependency>
  31. <!-- Sentinel 对 SpringMVC 的支持 -->
  32. <dependency>
  33. <groupId>com.alibaba.csp</groupId>
  34. <artifactId>sentinel-spring-webmvc-adapter</artifactId>
  35. <version>1.7.1</version>
  36. </dependency>
  37. <!-- Sentinel 对【热点参数限流】的支持 -->
  38. <dependency>
  39. <groupId>com.alibaba.csp</groupId>
  40. <artifactId>sentinel-parameter-flow-control</artifactId>
  41. <version>1.7.1</version>
  42. </dependency>
  43. <!-- Sentinel 对 Spring AOP 的拓展 -->
  44. <dependency>
  45. <groupId>com.alibaba.csp</groupId>
  46. <artifactId>sentinel-annotation-aspectj</artifactId>
  47. <version>1.7.1</version>
  48. </dependency>
  49. </dependencies>
  50. </project>

2.Sentinel 配置文件

在 resources 目录下,创建 Sentinel 自定义的sentinel.properties 配置文件。内容如下:

3.配置项目启动端口。内容如下:

4.Application

创建 Application.java 类,配置 @SpringBootApplication 注解即可。代码如下:

  1. package cn.iocoder.springboot.lab46.sentineldemo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class Application {
  6. public static void main(String[] args) {
  7. // 设置系统属性 project.name,提供给 Sentinel 读取
  8. System.setProperty("project.name", "demo-application");
  9. // 启动 Spring Boot 应用
  10. SpringApplication.run(Application.class, args);
  11. }
  12. }

 5.SpringMvcConfiguration

在 cn.iocoder.springboot.lab46.sentineldemo.config 包下,创建 SpringMvcConfiguration 配置类,自定义 sentinel-spring-webmvc-adapter 提供的拦截器。代码如下:

  1. package cn.iocoder.springboot.lab46.sentineldemo.config;
  2. import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor;
  3. import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebTotalInterceptor;
  4. import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig;
  5. import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcTotalConfig;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  9. @Configuration
  10. public class SpringMvcConfiguration implements WebMvcConfigurer {
  11. @Override
  12. public void addInterceptors(InterceptorRegistry registry) {
  13. // Add Sentinel interceptor
  14. // addSentinelWebTotalInterceptor(registry);
  15. addSentinelWebInterceptor(registry);
  16. }
  17. private void addSentinelWebInterceptor(InterceptorRegistry registry) {
  18. // 创建 SentinelWebMvcConfig 对象
  19. SentinelWebMvcConfig config = new SentinelWebMvcConfig();
  20. config.setHttpMethodSpecify(true); // 是否包含请求方法。即基于 URL 创建的资源,是否包含 Method。
  21. // config.setBlockExceptionHandler(new DefaultBlockExceptionHandler()); // 设置 BlockException 处理器。
  22. // config.setOriginParser(new RequestOriginParser() { // 设置请求来源解析器。用于黑白名单控制功能。
  23. //
  24. // @Override
  25. // public String parseOrigin(HttpServletRequest request) {
  26. // // 从 Header 中,获得请求来源
  27. // String origin = request.getHeader("s-user");
  28. // // 如果为空,给一个默认的
  29. // if (StringUtils.isEmpty(origin)) {
  30. // origin = "default";
  31. // }
  32. // return origin;
  33. // }
  34. //
  35. // });
  36. // 添加 SentinelWebInterceptor 拦截器
  37. registry.addInterceptor(new SentinelWebInterceptor(config)).addPathPatterns("/**");
  38. }
  39. private void addSentinelWebTotalInterceptor(InterceptorRegistry registry) {
  40. // 创建 SentinelWebMvcTotalConfig 对象
  41. SentinelWebMvcTotalConfig config = new SentinelWebMvcTotalConfig();
  42. // 添加 SentinelWebTotalInterceptor 拦截器
  43. registry.addInterceptor(new SentinelWebTotalInterceptor(config)).addPathPatterns("/**");
  44. }
  45. }

 结构如下:

6.GlobalExceptionHandler

在 cn.iocoder.springboot.lab46.sentineldemo.web 包下,创建 GlobalExceptionHandler 配置类,自定义 sentinel-spring-webmvc-adapter 提供的拦截器。代码如下:

  1. package cn.iocoder.springboot.lab46.sentineldemo.web;
  2. import com.alibaba.csp.sentinel.slots.block.BlockException;
  3. import org.springframework.web.bind.annotation.ControllerAdvice;
  4. import org.springframework.web.bind.annotation.ExceptionHandler;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. @ControllerAdvice(basePackages = "cn.iocoder.springboot.lab46.sentineldemo.controller")
  7. public class GlobalExceptionHandler {
  8. @ResponseBody
  9. @ExceptionHandler(value = BlockException.class)
  10. public String blockExceptionHandler(BlockException blockException) {
  11. return "请求过于频繁";
  12. }
  13. }

 结构如下:

7.DemoController

  1. package cn.iocoder.springboot.lab46.sentineldemo.controller;
  2. import com.alibaba.csp.sentinel.Entry;
  3. import com.alibaba.csp.sentinel.SphU;
  4. import com.alibaba.csp.sentinel.annotation.SentinelResource;
  5. import com.alibaba.csp.sentinel.slots.block.BlockException;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.RestController;
  10. @RestController
  11. @RequestMapping("/demo")
  12. public class DemoController {
  13. @GetMapping("/echo")
  14. public String echo() {
  15. return "echo";
  16. }
  17. @GetMapping("/test")
  18. public String test() {
  19. return "test";
  20. }
  21. @GetMapping("/sleep")
  22. public String sleep() throws InterruptedException {
  23. Thread.sleep(100L);
  24. return "sleep";
  25. }
  26. // 测试热点参数限流
  27. @GetMapping("/product_info")
  28. @SentinelResource("demo_product_info_hot")
  29. public String productInfo(Integer id) {
  30. return "商品编号:" + id;
  31. }
  32. // 手动使用 Sentinel 客户端 API
  33. @GetMapping("/entry_demo")
  34. public String entryDemo() {
  35. Entry entry = null;
  36. try {
  37. // 访问资源
  38. entry = SphU.entry("entry_demo");
  39. // ... 执行业务逻辑
  40. return "执行成功";
  41. } catch (BlockException ex) {
  42. return "被拒绝";
  43. } finally {
  44. // 释放资源
  45. if (entry != null) {
  46. entry.exit();
  47. }
  48. }
  49. }
  50. // 测试 @SentinelResource 注解
  51. @GetMapping("/annotations_demo")
  52. @SentinelResource(value = "annotations_demo_resource",
  53. blockHandler = "blockHandler",
  54. fallback = "fallback")
  55. public String annotationsDemo(@RequestParam(required = false) Integer id) throws InterruptedException {
  56. if (id == null) {
  57. throw new IllegalArgumentException("id 参数不允许为空");
  58. }
  59. return "success...";
  60. }
  61. // BlockHandler 处理函数,参数最后多一个 BlockException,其余与原函数一致.
  62. public String blockHandler(Integer id, BlockException ex) {
  63. return "block:" + ex.getClass().getSimpleName();
  64. }
  65. // Fallback 处理函数,函数签名与原函数一致或加一个 Throwable 类型的参数.
  66. public String fallback(Integer id, Throwable throwable) {
  67. return "fallback:" + throwable.getMessage();
  68. }
  69. }

 8. 简单测试

使用浏览器,访问下 http://127.0.0.1:7070/ 地址,进入 Sentinel 控制台。此时,我们可以看到 demo-application 应用。如下图所示:

使用浏览器,访问下 http://127.0.0.1:8080/demo/echo 接口 10 次。然后点击 Sentinel 控制台的「实时监控」菜单,可以看到该接口的请求情况。如下图所示: 

 9.根据自己的需求设置:流量规则,降级规则,热点规则,系统规则等。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/889253
推荐阅读
相关标签
  

闽ICP备14008679号