parent
262720b658
commit
0427dcfa3d
7 changed files with 192 additions and 8 deletions
@ -1 +1,20 @@ |
|||||||
### 基于SpringCloudAlibaba |
### 基于SpringCloudAlibaba |
||||||
|
|
||||||
|
#### 版本关系 |
||||||
|
|
||||||
|
| 依赖 | 版本号 | |
||||||
|
| :-------------------------------------: | :----------: | |
||||||
|
| `SpringBoot` | `2.6.11` | |
||||||
|
| `Spring Cloud` | ` 2021.0.4` | |
||||||
|
| `Spring Cloud Alibaba` | `2021.0.4.0` | |
||||||
|
| `Sentinel jar ` | `1.8.5` | |
||||||
|
| `Nacos jar ` | `2.0.4` | |
||||||
|
| `RocketMQ` | `4.9.4` | |
||||||
|
| `Seata` | `1.5.2` | |
||||||
|
| `spring-cloud-starter-loadbalancer` | `3.1.4` | |
||||||
|
| `spring-cloud-starter-gateway` | `3.1.4` | |
||||||
|
| `spring-cloud-starter-alibaba-sentinel` | `2021.0.4.0` | |
||||||
|
|
||||||
|
#### 详细文档 |
||||||
|
|
||||||
|
地址:[https://hugo.bnblogs.cc/springcloud%E7%AC%94%E8%AE%B0/#%E5%9F%BA%E4%BA%8Enacos%E8%8E%B7%E5%8F%96%E5%85%B6%E4%BB%96%E6%9C%8D%E5%8A%A1](https://hugo.bnblogs.cc/springcloud%E7%AC%94%E8%AE%B0/#%E5%9F%BA%E4%BA%8Enacos%E8%8E%B7%E5%8F%96%E5%85%B6%E4%BB%96%E6%9C%8D%E5%8A%A1) |
||||||
|
@ -0,0 +1,108 @@ |
|||||||
|
package cc.bnblogs.gateway.config; |
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants; |
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition; |
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem; |
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem; |
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager; |
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule; |
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager; |
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter; |
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler; |
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager; |
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler; |
||||||
|
import org.springframework.beans.factory.ObjectProvider; |
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.core.Ordered; |
||||||
|
import org.springframework.core.annotation.Order; |
||||||
|
import org.springframework.http.HttpStatus; |
||||||
|
import org.springframework.http.MediaType; |
||||||
|
import org.springframework.http.codec.ServerCodecConfigurer; |
||||||
|
import org.springframework.web.reactive.function.BodyInserters; |
||||||
|
import org.springframework.web.reactive.function.server.ServerResponse; |
||||||
|
import org.springframework.web.reactive.result.view.ViewResolver; |
||||||
|
import org.springframework.web.server.ServerWebExchange; |
||||||
|
import reactor.core.publisher.Mono; |
||||||
|
|
||||||
|
import javax.annotation.PostConstruct; |
||||||
|
import java.util.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* 限流规则 |
||||||
|
*/ |
||||||
|
@Configuration // 标记为配置类
|
||||||
|
public class SentinelRouteConfiguration { // 添加分组api限流
|
||||||
|
private final List<ViewResolver> viewResolvers; |
||||||
|
private final ServerCodecConfigurer serverCodecConfigurer; |
||||||
|
|
||||||
|
public SentinelRouteConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider, // 构造函数
|
||||||
|
ServerCodecConfigurer serverCodecConfigurer) { |
||||||
|
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList); |
||||||
|
this.serverCodecConfigurer = serverCodecConfigurer; |
||||||
|
} |
||||||
|
|
||||||
|
@PostConstruct |
||||||
|
public void initGatewayRules() { |
||||||
|
//初始化限流规则
|
||||||
|
Set<GatewayFlowRule> rules = new HashSet<>(); |
||||||
|
// 路由限流
|
||||||
|
GatewayFlowRule gatewayFlowRule = new GatewayFlowRule("provider_router");// 资源名称,对应routeId的值 此处限流用户服务
|
||||||
|
gatewayFlowRule.setCount(1); // 限流阀值
|
||||||
|
gatewayFlowRule.setIntervalSec(10); // 统计时间窗口(单位:秒),默认是1秒
|
||||||
|
// rules.add(gatewayFlowRule); //为了测试分组限流,先把这个注释
|
||||||
|
|
||||||
|
// 定义两个api分组
|
||||||
|
// 加入分组限流
|
||||||
|
rules.add(new GatewayFlowRule("provider_api1").setCount(1).setIntervalSec(1)); |
||||||
|
rules.add(new GatewayFlowRule("provider_api2").setCount(1).setIntervalSec(1)); |
||||||
|
|
||||||
|
GatewayRuleManager.loadRules(rules); // 载入规则
|
||||||
|
} |
||||||
|
|
||||||
|
//自定义API分组
|
||||||
|
@PostConstruct |
||||||
|
private void initCustomizedApis() { |
||||||
|
Set<ApiDefinition> definitions = new HashSet<>(); |
||||||
|
ApiDefinition api1 = new ApiDefinition("provider_api1") |
||||||
|
.setPredicateItems(new HashSet<ApiPredicateItem>() {{ |
||||||
|
add(new ApiPathPredicateItem().setPattern("/provider/api1/**") // api1下所有接口均被限流
|
||||||
|
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)); |
||||||
|
}}); |
||||||
|
ApiDefinition api2 = new ApiDefinition("provider_api2") |
||||||
|
.setPredicateItems(new HashSet<ApiPredicateItem>() {{ |
||||||
|
add(new ApiPathPredicateItem().setPattern("/provider/api2/demo2")); // api2只对demo2接口限流
|
||||||
|
}}); |
||||||
|
definitions.add(api1); |
||||||
|
definitions.add(api2); |
||||||
|
GatewayApiDefinitionManager.loadApiDefinitions(definitions); |
||||||
|
} |
||||||
|
|
||||||
|
@PostConstruct |
||||||
|
public void initBlockHandlers() { // 自定义限流后的界面
|
||||||
|
BlockRequestHandler blockRequestHandler = new BlockRequestHandler() { |
||||||
|
@Override |
||||||
|
public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) { |
||||||
|
Map<String, String> result = new HashMap<>(); // 限流提示
|
||||||
|
result.put("code", "0"); |
||||||
|
result.put("message", "您已被限流"); |
||||||
|
return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON_UTF8). |
||||||
|
body(BodyInserters.fromObject(result)); |
||||||
|
} |
||||||
|
}; |
||||||
|
GatewayCallbackManager.setBlockHandler(blockRequestHandler); |
||||||
|
} |
||||||
|
|
||||||
|
@Bean |
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE) |
||||||
|
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() { // 配置限流异常处理器
|
||||||
|
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer); |
||||||
|
} |
||||||
|
|
||||||
|
@Bean |
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE) |
||||||
|
public GlobalFilter sentinelGatewayFilter() { //初始化一个限流的过滤器
|
||||||
|
return new SentinelGatewayFilter(); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue