当前位置:   article > 正文

springboot整合sentinel_springboot 集成 sentinel

springboot 集成 sentinel

sentinel官方使用文档:Sentinel · alibaba/spring-cloud-alibaba Wiki · GitHub

引入依赖

  1. <dependency>
  2. <groupId>com.alibaba.cloud</groupId>
  3. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  4. </dependency>

下载sentinel控制台

下载对应版本的sentinel.jar Releases · alibaba/Sentinel · GitHub

运行jar

java -jar .\sentinel-dashboard-1.6.3.jar

如若8080端口冲突,可使用 -Dserver.port=新端口 进行设置。 

配置控制台信息

application.yml

  1. spring:
  2. cloud:
  3. sentinel:
  4. transport:
  5. port: 8719 #默认
  6. dashboard: localhost:8080

控制台显示

第一次发送请求显示

流控规则简单使用

新增流控规则

 超出请求频率

实时监控配置

pom添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>

application.yml 添加配置

management.endpoints.web.exposure.include=*

 自定义流控响应

添加配置

  1. package com.hdb.pingmoweb.seckill.config;
  2. import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
  3. import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
  4. import com.alibaba.csp.sentinel.slots.block.BlockException;
  5. import com.alibaba.fastjson.JSON;
  6. import com.hdb.pingmoweb.common.utils.R;
  7. import org.springframework.context.annotation.Configuration;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. @Configuration
  12. public class SentinelConfig {
  13. public SentinelConfig() {
  14. WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
  15. @Override
  16. public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
  17. R error = R.error("请求流量过大");
  18. httpServletResponse.setCharacterEncoding("UTF-8");
  19. httpServletResponse.setContentType("application/json");
  20. httpServletResponse.getWriter().write(JSON.toJSONString(error));
  21. }
  22. });
  23. }
  24. }

测试返回 

Feign 远程调用熔断

配置文件打开 Sentinel 对 Feign 的支持:

feign.sentinel.enabled=true

创建feign失败回调类,返回熔断后数据,保证其他服务能正常运行。

  1. package com.hdb.pingmoweb.seckill.feign.fallback;
  2. import com.hdb.pingmoweb.common.utils.R;
  3. import com.hdb.pingmoweb.seckill.feign.ProductFeignService;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.stereotype.Component;
  6. import java.util.Map;
  7. @Slf4j
  8. @Component
  9. public class ProductFeignServiceFallBack implements ProductFeignService {
  10. @Override
  11. public R list(Map<String, Object> params) {
  12. log.info("熔断方法调用...list");
  13. return R.error("太多请求");
  14. }
  15. }

 feign服务接口配置熔断回调类

  1. package com.hdb.pingmoweb.seckill.feign;
  2. import com.hdb.pingmoweb.common.utils.R;
  3. import com.hdb.pingmoweb.seckill.feign.fallback.ProductFeignServiceFallBack;
  4. import org.springframework.cloud.openfeign.FeignClient;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import java.util.Map;
  8. @FeignClient(name = "pingmoweb-product", fallback = ProductFeignServiceFallBack.class)
  9. public interface ProductFeignService {
  10. @RequestMapping("/product/brand/list")
  11. public R list(@RequestParam Map<String, Object> params);
  12. }

调用方手动指定远程降级策略触发熔断机制,返回熔断数据

提供方手动指定远程降级策略,返回的是默认降级数据(限流数据)(提供方配置降级相当于全局降级)

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

闽ICP备14008679号