pom依赖
<!--网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--nacos服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--nacos配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--sentinel相关依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-parameter-flow-control</artifactId>
</dependency>
创建配置类
package com.ntimes.config;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties;
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.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import java.util.Set;
/**
* @title: DataSourceInitFunc.java
* @Description: 限流规则持久化
* @author: zk
* @Date: 2020/7/15 13:46
* @Version: 1.0
*/
@Configuration
@Order(2)
public class DataSourceInitFunc {
@Autowired
private SentinelProperties sentinelProperties;
@Bean
public DataSourceInitFunc init() throws Exception {
loadGWFlowRule();
return new DataSourceInitFunc();
}
private void loadGWFlowRule(){
sentinelProperties.getDatasource().entrySet().stream().filter(map -> {
return map.getValue().getNacos() != null;
}).forEach(map -> {
NacosDataSourceProperties nacos = map.getValue().getNacos();
ReadableDataSource<String, Set<GatewayFlowRule>> gwFlowRuleDataSource = new NacosDataSource<>(
nacos.getServerAddr(), nacos.getGroupId(), nacos.getDataId(),
source -> JSON.parseObject(source, new TypeReference<Set<GatewayFlowRule>>() {
}));
GatewayRuleManager.register2Property(gwFlowRuleDataSource.getProperty());
});
}
}
网关application.properties配置相关的内容
gateway 流控规则持久化配置
spring.cloud.sentinel.datasource.gate-way-flow.nacos.server-addr=${spring.cloud.nacos.config.server-addr}
spring.cloud.sentinel.datasource.gate-way-flow.nacos.data-id=${spring.application.name}-gw-flow-rules
spring.cloud.sentinel.datasource.gate-way-flow.nacos.group-id=SENTINEL_GROUP
spring.cloud.sentinel.datasource.gate-way-flow.nacos.rule-type=gw_flow
# 限流返回的响应
spring.cloud.sentinel.scg.fallback.mode=response
spring.cloud.sentinel.scg.fallback.response-status=455
spring.cloud.sentinel.scg.fallback.response-body=error!
nacos中配置文件内容:基于路由ID来实现限流
像其它几种限流规则,都是一样的道理,给配置类多加几个其它种类的方法就ok了.
打完收工~~~