1.引入pom依赖
<!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <!-- SpringCloud ailibaba sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-transport-simple-http</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-web-servlet</artifactId> </dependency> <!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <!-- 引入 Sentinel 数据源 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-annotation-aspectj</artifactId> </dependency>
版本好统一按照
<version>1.8.3</version>
为例。
2.本机启动
首先需要再idea的启动脚本添加命令
-Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8081 -Dproject.name=项目别名
其中,-Dserver.port是自己的项目名称,-Dcsp.sentinel.dashboard.server 是sentinel开启控制台的端口号,-Dproject是自己的项目名称
3.再项目里面使用sentinel做限流
sentinel 有两种方式都可以做限流和降级,一种方式是直接再spring bean里面使用fallback以及blockHandler注解,另外一种方式则是和openFeign整合。目前项目里面使用的是第一种方式。
注意一点,使用sentinel需要相应的bean被spring容器加载进去。
@Service public class QueryRecommentServiceImpl{ @SentinelResource(value = "queryRecommend", fallback = "getStaticData",blockHandler = "blockExceptionHandler") public List<String> queryRecommend(HomeRecommendStreamQuery query, HomeRecommendStreamDto streamDto, String sourceApp) { return null; } public List<String> getStaticData(HomeRecommendStreamQuery query, HomeRecommendStreamDto streamDto, String sourceApp) { logger.info("==========start sentinel 降级策略=========="); try { Random random = new Random(); int r = random.nextInt(100) + 1; logger.debug("========随机从缓存获取一组帖子id:" + r); Set<String> stringSet = gwmRedisTemplate.opsForValue().get(RedisConstants.THREAD_HOT_SCORE_KEY + r); logger.debug("=========随机从缓存获取帖子result=======" + JSONObject.toJSON(stringSet)); if (CollectionUtils.isNotEmpty(stringSet)) { List<String> stringList = new ArrayList<>(stringSet); return stringList; } else { logger.debug("============读取默认配置数据========" + config.getPostIds()); return Optional.ofNullable(config.getPostIds()).orElse(Collections.emptyList()); } } catch (Exception e) { throw new RuntimeException(e); } } /** * blockHandler需要设置为static * * @param ex * @return */ public List<String> blockExceptionHandler(HomeRecommendStreamQuery query, HomeRecommendStreamDto streamDto, String sourceApp,BlockException ex) { try { Random random = new Random(); int r = random.nextInt(100) + 1; logger.debug("========随机从缓存获取一组帖子id:" + r); Set<String> stringSet = gwmRedisTemplate.opsForValue().get(RedisConstants.THREAD_HOT_SCORE_KEY + r); logger.debug("=========随机从缓存获取帖子result=======" + JSONObject.toJSON(stringSet)); if (CollectionUtils.isNotEmpty(stringSet)) { List<String> stringList = new ArrayList<>(stringSet); return stringList; } else { logger.debug("============读取默认配置数据========" + config.getPostIds()); return Optional.ofNullable(config.getPostIds()).orElse(Collections.emptyList()); } } catch (Exception e) { throw new RuntimeException(e); } } }
Sentinel 提供了 @SentinelResource
注解用于定义资源,并提供了 AspectJ 的扩展用于自动定义资源、处理 BlockException
等。使用 Sentinel Annotation AspectJ Extension 的时候需要引入以下依赖:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>x.y.z</version>
</dependency>
之后再控制台界面就可以进行设置了。
另外就是如果想要每次启动让sentinel生效,需要再配置一个sentienl的yaml配置,每次项目启动可以自动生效,大概格式如下:
[ { "resource":"queryRecommend", "controlBehavior":0, "count":20, "grade":1, "limitApp":"default", "strategy":0 } ]
标签:记录,接入,stringSet,alibaba,sentinel,logger,com,csp From: https://www.cnblogs.com/thinkingandworkinghard/p/18457878