基于nacos实现sentinel网关限流规则持久化

pom依赖

        
        
            org.springframework.cloud
            spring-cloud-starter-gateway
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        
        
            com.alibaba.cloud
            spring-cloud-alibaba-sentinel-gateway
        
        
            com.alibaba.csp
            sentinel-transport-simple-http
        
        
            com.alibaba.csp
            sentinel-datasource-nacos
        
        
            com.alibaba.csp
            sentinel-parameter-flow-control
        

创建配置类

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> gwFlowRuleDataSource = new NacosDataSource<>(
                    nacos.getServerAddr(), nacos.getGroupId(), nacos.getDataId(),
                    source -> JSON.parseObject(source, new TypeReference>() {
                    }));
            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来实现限流


image.png

像其它几种限流规则,都是一样的道理,给配置类多加几个其它种类的方法就ok了.
打完收工~~~

你可能感兴趣的:(基于nacos实现sentinel网关限流规则持久化)