1.下载源码
Release v1.8.6 · alibaba/Sentinel · GitHub
2. 解压后使用IDEA打开sentinel-dashboard,注释掉pom文件中的nacos下的test 在104行
3.将test下的rule下的nacos复制到Java的rule下
其中:
FlowRuleNacosProvider: 动态获取Nacos配置中心流控规则
FlowRuleNacosPublisher: publish上传流控规则到Nacos配置中心
NacosConfig: Nacos配置
NacosConfigUtils: 流控规则配置
4.新建NacosConfigProperties配置类
5.修改下NacosConfig.java
文件
package com.alibaba.csp.sentinel.dashboard.rule.nacos;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.Autowired;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 */@Configurationpublic class NacosConfig {// 注入刚新建的nacos配置文件@Autowiredprivate NacosConfigProperties nacosConfigProperties;@Beanpublic Converter<List, String> flowRuleEntityEncoder() {return JSON::toJSONString;}@Beanpublic Converter<String, List> flowRuleEntityDecoder() {return s -> JSON.parseArray(s, FlowRuleEntity.class);}@Beanpublic ConfigService nacosConfigService() throws Exception {// 注释// return ConfigFactory.createConfigService("localhost");// 新增Properties properties = new Properties();properties.put(PropertyKeyConst.SERVER_ADDR,nacosConfigProperties.getServerAddr());properties.put(PropertyKeyConst.USERNAME,nacosConfigProperties.getUsername());properties.put(PropertyKeyConst.PASSWORD,nacosConfigProperties.getPassword());properties.put(PropertyKeyConst.NAMESPACE,nacosConfigProperties.getNamespace());return ConfigFactory.createConfigService(properties);}}
6.controller层开启 Nacos 适配
com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2 中指定对应的 bean
7.修改sidebar.html
修改resources/app/scripts/directives/sidebar/sidebar.html
直接搜dashboard.flowV1
定位57行去掉V1
8.修改identity.js
修改resources/app/scripts/controllers/identity.js
第4行 FlowServiceV1改为FlowServiceV2
直接搜/dashboard/flow/,将
let url = ‘/dashboard/flow/’ + $scope.app; 改为let url = ‘/dashboard/v2/flow/’ + $scope.app;
9.项目application.properties
新增配置
# 服务端口、控制台地址、名称server.port=9100csp.sentinel.dashboard.server=127.0.0.1:9100project.name=sentinel-dashboard# Nacos地址、账号、密码sentinel.nacos.serverAddr=192.168.180.144:8848sentinel.nacos.username=nacossentinel.nacos.password=nacos# 默认命名空间就是 public,不用填写,填了public反而找不到,所以这个置空就行。sentinel.nacos.namespace=
10.注释掉flow_v2.html页面中的回到单机页面,因为回到单击页面 会恢复到 默认内存模式
11.启动项目,注册到Sentinel并测试规则持久化到nacos
# Springspring: application:# 应用名称name: simple-rbacprofiles:# 环境配置active: devcloud:nacos:discovery:# 服务注册地址server-addr: 192.168.180.144:8848# 服务启动后注册空间#namespace: ab8f515e-9c1f-4f1c-804f-642c816df64f#group: dev01config:# 配置中心地址server-addr: 192.168.180.144:8848# 配置文件格式file-extension: yml# 命名空间#namespace: ab8f515e-9c1f-4f1c-804f-642c816df64f# 分组#group: dev01# 共享配置#shared-configs:#simple-application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}sentinel:eager: true#是否开启网关限流,默认truetransport:# 添加sentinel的控制台地址dashboard: 127.0.0.1:9100#指定应用与Sentinel控制台交互的端口,应用本地会起一个该端口占用的HttpServerport: 8719web-context-unify: false # 默认将调用链路收敛,导致链路流控效果无效# 持久化配置datasource:admin-flow-rule: # 唯一名称可自定义nacos:# 设置Nacos的连接地址、命名空间和Group IDserver-addr: ${spring.cloud.nacos.discovery.server-addr}username: ${spring.cloud.nacos.discovery.username}password: ${spring.cloud.nacos.discovery.password}# 设置Nacos中配置文件的命名规则dataId: ${spring.application.name}-flow-rulesgroupId: SENTINEL_GROUPdata-type: json# 必填的重要字段,指定当前规则类型是"限流"rule-type: flow
项目中引入sentinel的持久化nacos依赖
com.alibaba.csp sentinel-datasource-nacos
12.演示效果
请求
设置限流
生成的nacos限流配置文件