各位看到这个博客的朋友,有需要的,参考使用,绝对良心创作,绝对可行的demo。
2、修改 sentinel-dashboard 这个工程目录下的 pom.xml 文件,把
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<scope>test</scope>
</dependency>里面的<scope>test</scope>去掉,变成
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>3、把 sentinel-dashboard\src\test\java\com\alibaba\csp\sentinel\dashboard\rule\nacos 目录及目录下的JAVA类 拷贝到 sentinel-dashboard\src\main\java\com\alibaba\csp\sentinel\dashboard\rule\ 目录下
4、修改 com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfig.java 这个类,修改后为如下:
/*
* 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 java.util.List;
import java.util.Properties;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
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.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Eric Zhao
* @since 1.4.0
*/
@Configuration
public class NacosConfig {
@Value("${spring.cloud.sentinel.datasource.ds.nacos.server-addr}")
private String nacosUrl;
@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 ConfigService nacosConfigService() throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR,nacosUrl);
return ConfigFactory.createConfigService(properties);
}
}
这个类主要是链接nacos的,通过配置文件链接nacos,初始化。
5、sentinel-dashboard\src\main\resources\application.properties 这个配置文件新增 配置内容:
spring.cloud.sentinel.datasource.ds.nacos.server-addr=192.168.99.100:8848
这个是nacos注册中心的地址
6、修改 这个文件 sentinel-dashboard\src\main\webapp\resources\app\scripts\directives\sidebar\sidebar.html 。修改内容是:
<!--<li ui-sref-active="active" ng-if="entry.appType==0">-->
<!--<a ui-sref="dashboard.flow({app: entry.app})">-->
<!--<i class="glyphicon glyphicon-filter"></i> 流控规则 V1</a>-->
<!--</li>-->把这段注释放开即可。修改为:
<li ui-sref-active="active" ng-if="entry.appType==0">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则 V1</a>
</li>7、修改这个类 sentinel-dashboard\src\main\java\com\alibaba\csp\sentinel\dashboard\controller\v2\FlowControllerV2.java
修改内容是把默认的提供者和推送者的代码注释掉,使用flowRuleNacosProvider、flowRuleNacosPublisher。 内容如下:
/*@Autowired
@Qualifier("flowRuleDefaultProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleDefaultPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
*/
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;8、这一步,已经是改造好了sentinel-dashboard。自行打包编译成jar,即可运行。运行脚本如下:java -jar -Xms4096m -Xmx4096m -Xmn2048m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC sentinel-dashboard.jar --server.port =8080 --spring.profiles.active=dev&
各位老板可以自己加多一个配置文件sentinel-dashboard\src\main\resources\application-dev.properties,把自己个性化配置放到这个文件。
二、验证这个sentinel-dashboard是否同步nacos,把自己的业务工程应用也改造一下,如改造gateway工程。
1、引入依赖jar
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- 将sentinel的数据同步到nacos中 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>2、gateway 网关工程的配置文件gateway-pre.properties 新增配置,内容如下:
feign.sentinel.enabled=true spring.cloud.sentinel.transport.port=8719 #sentinel-dashboard的访问地址及端口 spring.cloud.sentinel.transport.dashboard=http://127.0.0.1:8080 spring.cloud.sentinel.eager=true #nacos的地址及端口 spring.cloud.sentinel.datasource.ds.nacos.server-addr=127.0.0.1:8848 #nacos的配置文件的dataId名称 spring.cloud.sentinel.datasource.ds.nacos.dataId=gateway-flow-rules #nacos的配置文件的groupId名称 spring.cloud.sentinel.datasource.ds.nacos.groupId=SENTINEL_GROUP #nacos的配置文件格式是json spring.cloud.sentinel.datasource.ds.nacos.data-type=json spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow
3、gateway改造完成,先去nacos管理页面上新建配置文件。如图所示:

注意这个名字要和gateway配置文件的内容对得上。groupId是SENTINEL_GROUP,dataId是gateway-flow-rules,格式是json。
配置文件的内容如下:
[{"app":"gateway","clusterMode":false,"controlBehavior":0,"count":6.0,"gmtModified":1623314214316,"grade":1,"id":1,"limitApp":"default","resource":"/test","strategy":0}]
4、启动sentinel-dashboard、gateway后访问sentinel-dashboard,效果图如下:

账号密码都是sentinel 。

看到这个限流规则,修改它,nacos的配置文件也会跟着改变。各位自己调试验证。
注意 gateway和sentinel-dashboard 必须能相互访问,网络必须通的,不然会推送更新失败。