生产环境规则管理及推送一般更常用的是 push 模式的数据源。对于 push 模式的数据源,如远程配置中心(ZooKeeper, Nacos, Apollo等等),推送的操作不应由 Sentinel 客户端进行,而应该经控制台统一进行管理,直接进行推送,数据源仅负责获取配置中心推送的配置并更新到本地。因此推送规则正确做法应该是 配置中心控制台/Sentinel 控制台 → 配置中心 → Sentinel 数据源 → Sentinel,而不是经 Sentinel 数据源推送至配置中心,本文采用Nacos作为数据源。
https://download.csdn.net/download/qq_17522211/87600233
https://github.com/alibaba/Sentinel/releases/tag/1.8.6
打开sentinel-dashboard模块下的pom文件,把nacos的test作用域注释掉
com.alibaba.csp sentinel-datasource-nacos
将test文件夹下 com.alibaba.csp.sentinel.dashboard.rule.nacos 包下的类移动到main文件com.alibaba.csp.sentinel.dashboard.rule下
FlowRuleNacosProvider.java:从Nacos配置中心动态获取流控规
FlowRuleNacosPublisher.java:上传动态获取流控规则到Nacos配置中
NacosConfig.java:nacos配置
NacosConfigUtils.java:流控规则相关配置,比如GROUP_ID 流控规则的后缀FLOW_DATA_ID_POSTFIX
在NacosConfig中使用的是本地的nacos,我们需要修改此配置
在com.alibaba.csp.sentinel.dashboard.rule.nacos路径下创建NacosProperties.java文件,内容如下
package com.alibaba.csp.sentinel.dashboard.rule.nacos;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "sentinel.nacos")
public class NacosProperties {/*** nacos地址*/private String serverAddr;/*** nacos命名空间*/private String namespace;public String getServerAddr() {return serverAddr;}public void setServerAddr(String serverAddr) {this.serverAddr = serverAddr;}public String getNamespace() {return namespace;}public void setNamespace(String namespace) {this.namespace = namespace;}
}
/*
* 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.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
*/
@Configuration
public class NacosConfig {//注入nacos配置文件@Autowiredprivate NacosProperties nacosProperties;@Beanpublic Converter, String> flowRuleEntityEncoder() {return JSON::toJSONString;}@Beanpublic Converter> 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, nacosProperties.getServerAddr());properties.put(PropertyKeyConst.NAMESPACE, nacosProperties.getNamespace());return ConfigFactory.createConfigService(properties);}
}
默认 Nacos 适配的 dataId 和 groupId 约定如下:
可以在 NacosConfigUtil 修改对应的 groupId 和 dataId postfix。然后在 NacosConfig 配置对应的 Converter,默认已提供 FlowRuleEntity 的 decoder 和 encoder。
在后端 com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2 中指定对应的 bean 即可开启 Nacos 适配
@Autowired
// @Qualifier("flowRuleDefaultProvider")@Qualifier("flowRuleNacosProvider")private DynamicRuleProvider> ruleProvider;@Autowired
// @Qualifier("flowRuleDefaultPublisher")@Qualifier("flowRuleNacosPublisher")private DynamicRulePublisher> rulePublisher;
前端页面需要手动切换,或者修改前端路由配置(sidebar.html 流控规则路由从 dashboard.flowV1 改成 dashboard.flow 即可,注意簇点链路页面对话框需要自行改造)
按照提示,找到resources/app/scripts/directives/sidebar/sidebar.html文件搜索dashboard.flowV1,进行修改
流控规则
找到resources/app/scripts/controllers/identity.js文件,把FlowServiceV1 改成FlowServiceV2
通页面找到saveFlowRule方法进行改动,把/dashboard/flow/换成/dashboard/v2/flow/
function saveFlowRule() {if (!FlowService.checkRuleValid(flowRuleDialogScope.currentRule)) {return;}FlowService.newRule(flowRuleDialogScope.currentRule).success(function (data) {if (data.code === 0) {flowRuleDialog.close();
// let url = '/dashboard/flow/' + $scope.app; //修改前let url = '/dashboard/v2/flow/' + $scope.app;//修改后$location.path(url);} else {alert('失败:' + data.msg);}}).error((data, header, config, status) => {alert('未知错误');});}
修改resource下面的application.properties文件,把一些常用的配置加上
#项目参数
server.port=8080# nacos地址
sentinel.nacos.serverAddr=127.0.0.1:8848
# Nacos,命名空间ID,若为空,则会使用public命名空间
sentinel.nacos.namespace=88c1cb3c-46fe-4770-ba4c-bec17fd44b5b
http://127.0.0.1:7080/#/login 登陆,用户名密码都是sentinel
com.alibaba.cloud spring-cloud-starter-alibaba-sentinel
com.alibaba.csp sentinel-datasource-nacos
spring:application:name: cloudalibaba-sentinel-servicecloud:nacos:discovery:server-addr: 127.0.0.1:8848sentinel:transport:port: 8719dashboard: 127.0.0.1:8080datasource:flow:nacos:server-addr: 127.0.0.1:8848dataId: ${spring.application.name}-flow-rulesgroupId: SENTINEL_GROUPdataType: jsonrule-type: flow
namespace与控制台项目中sentinel.nacos.namespace配置项保持一样
dataId按照约定规则{appName}-flow-rules,比如应用名为 sentinel-demo,则 dataId 为 sentinel-demo-flow-rules
groupId与控制台项目中NacosConfigUtil的GROUP_ID一致