环境
- 1、sentinel版本:1.8.6,下载地址:https://github.com/alibaba/Sentinel/releases/tag/1.8.6
- 2、nacos版本:2.1.2,下载地址:https://github.com/alibaba/nacos/releases
问题:
生产环境规则管理及推送一般更常用的是 push 模式的数据源。对于 push 模式的数据源,如远程配置中心(ZooKeeper, Nacos, Apollo等等),推送的操作不应由 Sentinel 客户端进行,而应该经控制台统一进行管理,直接进行推送,数据源仅负责获取配置中心推送的配置并更新到本地。因此推送规则正确做法应该是 配置中心控制台/Sentinel 控制台 → 配置中心 → Sentinel 数据源 → Sentinel,而不是经 Sentinel 数据源推送至配置中心,本文采用Nacos作为数据源。
支持nacos作为数据源的改后源码:
https://download.csdn.net/download/qq_17522211/87600233
操作步骤如下:
1、下载sentinel源码:
https://github.com/alibaba/Sentinel/releases/tag/1.8.6
2、修改pom文件
打开sentinel-dashboard模块下的pom文件,把nacos的test作用域注释掉
<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!-- <scope>test</scope>-->
</dependency>
3、移动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
4、修改nacos配置文件
在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;
}
}
修改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 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配置文件
@Autowired
private NacosProperties nacosProperties;
@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 {
// 修改前
// 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 约定如下:
- groupId: SENTINEL_GROUP 流控规则 dataId: {appName}-flow-rules,比如应用名为
- appA,则 dataId 为 appA-flow-rules
可以在 NacosConfigUtil 修改对应的 groupId 和 dataId postfix。然后在 NacosConfig 配置对应的 Converter,默认已提供 FlowRuleEntity 的 decoder 和 encoder。
修改 FlowControllerV2.java
在后端 com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2 中指定对应的 bean 即可开启 Nacos 适配
@Autowired
// @Qualifier("flowRuleDefaultProvider")
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
// @Qualifier("flowRuleDefaultPublisher")
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
5、前端文件路由修改
前端页面需要手动切换,或者修改前端路由配置(sidebar.html 流控规则路由从 dashboard.flowV1 改成 dashboard.flow 即可,注意簇点链路页面对话框需要自行改造)
5.1、修改流控规则
按照提示,找到resources/app/scripts/directives/sidebar/sidebar.html文件搜索dashboard.flowV1,进行修改
<!-- 修改前 -->
<!-- <li ui-sref-active="active" ng-if="!entry.isGateway">-->
<!-- <a ui-sref="dashboard.flowV1({app: entry.app})">-->
<!-- <i class="glyphicon glyphicon-filter"></i> 流控规则</a>-->
<!-- </li>-->
<!-- 修改后 -->
<li ui-sref-active="active" ng-if="!entry.isGateway">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则</a>
</li>
5.2、修改簇点链路
找到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('未知错误');
});
}
6、修改项目的配置文件
修改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
7、启动项目测试
http://127.0.0.1:7080/#/login 登陆,用户名密码都是sentinel
8、创建项目演示
8.1、引入相关jar文件
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- 引入nacos数据源 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
8.2、配置文件
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
sentinel:
transport:
port: 8719
dashboard: 127.0.0.1:8080
datasource:
flow:
nacos:
server-addr: 127.0.0.1:8848
dataId: ${
spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
dataType: json
rule-type: flow
namespace与控制台项目中sentinel.nacos.namespace配置项保持一样
dataId按照约定规则{appName}-flow-rules,比如应用名为 sentinel-demo,则 dataId 为 sentinel-demo-flow-rules
groupId与控制台项目中NacosConfigUtil的GROUP_ID一致
8.3、启动演示项目,随便调用一个接口
8.4、新增一个流控规则
8.5、验证Nacos