赞
踩
sentinel官方使用文档:Sentinel · alibaba/spring-cloud-alibaba Wiki · GitHub
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
- </dependency>
下载对应版本的sentinel.jar Releases · alibaba/Sentinel · GitHub
运行jar
java -jar .\sentinel-dashboard-1.6.3.jar
如若8080端口冲突,可使用
-Dserver.port=新端口
进行设置。
application.yml
- spring:
- cloud:
- sentinel:
- transport:
- port: 8719 #默认
- dashboard: localhost:8080
第一次发送请求显示
新增流控规则
超出请求频率
pom添加依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
application.yml 添加配置
management.endpoints.web.exposure.include=*
添加配置
- package com.hdb.pingmoweb.seckill.config;
-
- import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
- import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
- import com.alibaba.csp.sentinel.slots.block.BlockException;
- import com.alibaba.fastjson.JSON;
- import com.hdb.pingmoweb.common.utils.R;
- import org.springframework.context.annotation.Configuration;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- @Configuration
- public class SentinelConfig {
-
- public SentinelConfig() {
- WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
- @Override
- public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
- R error = R.error("请求流量过大");
- httpServletResponse.setCharacterEncoding("UTF-8");
- httpServletResponse.setContentType("application/json");
- httpServletResponse.getWriter().write(JSON.toJSONString(error));
- }
- });
- }
- }
测试返回
配置文件打开 Sentinel 对 Feign 的支持:
feign.sentinel.enabled=true
创建feign失败回调类,返回熔断后数据,保证其他服务能正常运行。
- package com.hdb.pingmoweb.seckill.feign.fallback;
-
- import com.hdb.pingmoweb.common.utils.R;
- import com.hdb.pingmoweb.seckill.feign.ProductFeignService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Component;
-
- import java.util.Map;
-
- @Slf4j
- @Component
- public class ProductFeignServiceFallBack implements ProductFeignService {
- @Override
- public R list(Map<String, Object> params) {
- log.info("熔断方法调用...list");
- return R.error("太多请求");
- }
- }
feign服务接口配置熔断回调类
- package com.hdb.pingmoweb.seckill.feign;
-
- import com.hdb.pingmoweb.common.utils.R;
- import com.hdb.pingmoweb.seckill.feign.fallback.ProductFeignServiceFallBack;
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
-
- import java.util.Map;
-
- @FeignClient(name = "pingmoweb-product", fallback = ProductFeignServiceFallBack.class)
- public interface ProductFeignService {
-
- @RequestMapping("/product/brand/list")
- public R list(@RequestParam Map<String, Object> params);
-
- }
调用方手动指定远程降级策略触发熔断机制,返回熔断数据
提供方手动指定远程降级策略,返回的是默认降级数据(限流数据)(提供方配置降级相当于全局降级)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。