直接去拿jar用即可(实现了sentinel的修改,nacos同步进行改变)

地址

启动命令

java -Dserver.port=8888 -Dcsp.sentinel.dashboard.server=localhost:8888 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

那么跟着我了解如何更改的源码就接着看~

1.在该配置类中配置nacos信息和添加五种流控规则

/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.*;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Properties;

/**
 * @author Eric Zhao
 * @since 1.4.0
 */
@Configuration
public class NacosConfig {


        //流控
    @Bean
    public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    @Bean
    public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
        return s -> JSON.parseArray(s, FlowRuleEntity.class);
    }

    //授权
    @Bean
    public Converter<List<AuthorityRuleEntity>, String> authRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    @Bean
    public Converter<String, List<AuthorityRuleEntity>>  authRuleEntityDecoder() {
        return s -> JSON.parseArray(s, AuthorityRuleEntity.class);
    }
    //热点
    @Bean
    public Converter<List<ParamFlowRuleEntity>, String> paraRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    @Bean
    public Converter<String, List<ParamFlowRuleEntity>>  paraRuleEntityDecoder() {
        return s -> JSON.parseArray(s, ParamFlowRuleEntity.class);
    }
    //系统
    @Bean
    public Converter<List<SystemRuleEntity>, String> systemRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    @Bean
    public Converter<String, List<SystemRuleEntity>>  systemEntityDecoder() {
        return s -> JSON.parseArray(s, SystemRuleEntity.class);
    }

    //熔断
    @Bean
    public Converter<List<DegradeRuleEntity>, String> degradeRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    @Bean
    public Converter<String, List<DegradeRuleEntity>>  degradeEntityDecoder() {
        return s -> JSON.parseArray(s, DegradeRuleEntity.class);
    }

    @Bean
    public ConfigService nacosConfigService() throws Exception {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, "localhost:8848");
        properties.put(PropertyKeyConst.USERNAME, "nacos");
        properties.put(PropertyKeyConst.PASSWORD, "nacos");
        return ConfigFactory.createConfigService(properties);
    }
}

2.分析flow的provider和publish 

这两个主要是nacos和sentinel之间读取用的:

Provider: nacos中Json类型的配置文件传给sentinel,转为实体类型
Publish: sentinel中实体类型传给nacos,转为json类型

我们以其中一个来看(这就是为什么nacos中配置流控规则时为什么必须加后缀了,实际上按照应用名-规则后缀去读取/推送这个配置文件)

FlowRuleNacosProvider来讲解他的源码

3.对NacosConfigUtil补全五个流控规则

查看发现,该Util中只有两种规则,因此我们需要自己额外补全剩下三种流控规则

public static final String AUTHORITY_FLOW_DATA_ID_POSTFIX = "-authority-rules";
public static final String SYSTEM_FLOW_DATA_ID_POSTFIX = "-system-rules";
public static final String DEGRADE_FLOW_DATA_ID_POSTFIX = "-degrade-rules";

4.最后我们对FlowControllerV2这个类详解,其他规则的控制类一个道理(照葫芦画瓢📎FlowControllerV2.java

下面是五种规则中都有的一个参数,该参数底层是一堆map 如下图

提供好上面我们写的provider和publisher

如果忘了provider那再看一遍 如下

所以其他的四个规则换汤不换药

最后就完成了sentinel和nacos的双向整合啦!

如果有什么问题,虚心接受请教!

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐