Sentinel1.8.6规则持久化到Nacos2.2.0集群记录
环境准备:
Nacos2.2.0集群搭建
版本说明:https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明
本次记录修改好的控制台代码,需要的话自取:Gitee、Github
1. 下载源代码
Sentinel1.8.6:https://github.com/alibaba/Sentinel/releases/tag/1.8.6
2. 解压后使用IDEA打开sentinel-dashboard
单独打开:
3. 修改pom.xml
大概104行,注释掉 test
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!--<scope>test</scope>-->
</dependency>
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-maven-plugin
4. 复制一份test中的nacos到rule中
目录介绍:
FlowRuleNacosProvider: 动态获取Nacos配置中心流控规则
FlowRuleNacosPublisher: publish上传流控规则到Nacos配置中心
NacosConfig: Nacos配置
NacosConfigUtils: 流控规则配置
5. 先新建 NacosConfigProperties.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 NacosConfigProperties {
// nacos地址
private String serverAddr;
// nacos登录名
private String username;
// nacos 登录密码
private String password;
// nacos命名空间
private String namespace;
public String getServerAddr() {
return serverAddr;
}
public void setServerAddr(String serverAddr) {
this.serverAddr = serverAddr;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
}
6. 再改下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 NacosConfigProperties nacosConfigProperties;
@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,nacosConfigProperties.getServerAddr());
properties.put(PropertyKeyConst.USERNAME,nacosConfigProperties.getUsername());
properties.put(PropertyKeyConst.PASSWORD,nacosConfigProperties.getPassword());
properties.put(PropertyKeyConst.NAMESPACE,nacosConfigProperties.getNamespace());
return ConfigFactory.createConfigService(properties);
}
}
NacosConfigUtil.java
文件有部分默认配置
比如:命名空间名称、默认配置文件后缀
7. controller层开启 Nacos 适配
com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2 中指定对应的 bean
@Autowired
// @Qualifier("flowRuleDefaultProvider")
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
// @Qualifier("flowRuleDefaultPublisher")
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
8. 修改sidebar.html
修改resources/app/scripts/directives/sidebar/sidebar.html
直接搜dashboard.flowV1
定位57行去掉V1
<!--<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>
9. 修改identity.js
修改resources/app/scripts/controllers/identity.js
第4行
// 'FlowServiceV1'
'FlowServiceV2',
直接搜/dashboard/flow/
定位第101行
// let url = '/dashboard/flow/' + $scope.app;
let url = '/dashboard/v2/flow/' + $scope.app;
10. 项目application.properties
新增配置
# 服务端口、控制台地址、名称
server.port=9100
csp.sentinel.dashboard.server=127.0.0.1:9100
project.name=sentinel-dashboard
# Nacos地址、账号、密码
sentinel.nacos.serverAddr=192.168.20.128:8999
sentinel.nacos.username=nacos
sentinel.nacos.password=nacos
# 默认命名空间就是 public,不用填写,填了public反而找不到,所以这个置空就行。
sentinel.nacos.namespace=
11. 尝试启动项目
访问地址:http://localhost:9100/#/login
默认账号:sentinel
默认密码:sentinel
12. 启动项目,注册到Sentinel并测试规则持久化到nacos
项目的yml配置:
spring:
# 配置中心地址
application:
# 服务名称
name: admin
# 环境配置 例如 admin-dev.yaml
#profiles:
# active: dev
cloud:
loadbalancer:
cache:
enabled: false
nacos:
discovery:
# 开启nacos作为服务注册中心,默认值:true
enabled: true
# nacos集群服务注册地址
server-addr: 192.168.20.128:8999
# nacos用户名
username: nacos
# nacos密码
password: nacos
# 命名空间,默认 public,可设置dev,pro等,相同特征的服务分类,先去nacos命名空间创建
# namespace: public
# 分组,默认 DEFAULT_GROUP 相同特征的服务划分的更细
group: DEFAULT_GROUP
# 临时实例,默认true,false永久实例,即使宕机也不会从nacos服务中删除,可应对雪崩保护,避免服务被删除
ephemeral: true
# 权重 1-100 数值越大权重越大分配的流量就越大,通常结合权重负载均衡策略
weight: 100
sentinel:
eager: true #是否开启网关限流,默认true
transport:
# 添加sentinel的控制台地址
dashboard: 127.0.0.1:9100
#指定应用与Sentinel控制台交互的端口,应用本地会起一个该端口占用的HttpServer
port: 8719
web-context-unify: false # 默认将调用链路收敛,导致链路流控效果无效
# 持久化配置
datasource:
admin-flow-rule: # 唯一名称可自定义
nacos:
# 设置Nacos的连接地址、命名空间和Group ID
server-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-rules
groupId: SENTINEL_GROUP
data-type: json
# 必填的重要字段,指定当前规则类型是"限流"
rule-type: flow
13. 新建测试接口;
@RestController
@RequestMapping("/api/admin")
public class OrderController {
@GetMapping("/sentinel")
public String sentinelCS() {
return "sentinel控制台测试";
}
}
14. 页面访问测试:
15. 接口流控:
16. 设置每秒阈值为1
17. 快速访问两下
18. 查看nacos配置中心是否记录了:
19. 注释回到单击页面按钮
最后这个回到单击页面 会恢复到 默认内存模式
选择把这个按钮注释掉
最后,希望Sentinel 官方优化下配置,将持久化集成进配置文件,这样只需要到配置文件选择任一 一种持久化存储就行,同时也祝国内开源越来越好!
搞定,感谢阅览~
END