• springboot整合Sentinel


    /**

    • 1、整合Sentinel
    • 1)、导入依赖 spring-cloud-starter-alibaba-sentinel
    • 2)、下载sentinel控制台
    • 3)、配置 sentinel 控制台地址信息
    • 4)、在控制台调整参数、【默认所有的流控规则保存在内存中,重启失效】
    • 2、每一个微服务都导入 actuator :并配合 management.endpoints.web.exposure.include=*
    • 3、自定义 sentinel 流控返回的数据
    • 4、使用Sentinel来保护feign远程调用,熔断;
    • 1)、调用方的熔断保护:feign.sentinel.enable=true
    • 2)、调用方手动指定远程服务的降级策略。远程服务被降级处理。触发我们的熔断回调方法
    • 3)、超大浏览的时候,必须牺牲一些远程服务。在服务的提供方(远程服务)指定降级策略;
    •  提供方是在运行,但是不允许自己的业务逻辑,返回的是默认的降级数据(限流的数据)
      
      • 1
    • 5、自定义受保护的资源
    • 1)、代码
    •      try (Entry entry = SphU.entry("seckillSkus")) {
      
      • 1
    •          //业务逻辑
      
      • 1
    •      } catch(Exception e) {}
      
      • 1
    • 2)、基于注解
      配置到common,因为所有的服务都是需要进行管理,配置文件每一个服务都是需要进行配置
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    
     <dependency>
         <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    启动的时候: --service port 8888 设置启动端口

     sentinel:
          transport:
            #配置sentinel dashboard地址
            dashboard: localhost:8080
            #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
            port: 8719
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    #暴露所有端点
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述
    在这里插入图片描述
    需要限制的业务类加上注解;

        /**
         * 获取到当前可以参加秒杀商品的信息
         * @return
         */
        @SentinelResource(value = "getCurrentSeckillSkusResource",blockHandler = "blockHandler")
        @Override
        public List<SeckillSkuRedisTo> getCurrentSeckillSkus() 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    自定义异常信息:

    
    /**
     * @Description: 自定义阻塞返回方法
     * @Created: with IntelliJ IDEA.
     * @author: 夏沫止水
     * @createTime: 2020-07-13 11:30
     **/
    
    @Configuration
    public class GulimallSeckillSentinelConfig {
        public GulimallSeckillSentinelConfig() {
            WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
                @Override
                public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException {
                    R error = R.error(BizCodeEnum.TO_MANY_REQUEST.getCode(), BizCodeEnum.TO_MANY_REQUEST.getMessage());
                    response.setCharacterEncoding("UTF-8");
                    response.setContentType("application/json");
                    response.getWriter().write(JSON.toJSONString(error));
                }
            });
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    性能优化:Redis使用优化(1)
    Nginx请求处理时间过长问题解决
    LabVIEW为什么浮点数会丢失精度
    项目管理之合同管理
    SpringBoot笔记
    【LockSupport】概述、阻塞⽅法park、唤醒⽅法unpark(thread)、 解决的痛点、带来的⾯试题_JUC19
    面试突击34:如何使用线程池执行定时任务?
    JSR303和拦截器
    Centos7安装MongoDB
    3.容器的学习(2/2)
  • 原文地址:https://blog.csdn.net/qq_44833327/article/details/125427534