上一篇整理过单向的持久化,sentinel笔记9- 限流规则持久化(上)-CSDN博客
本篇进行sentinel 改造,实现双向同步。
1 下载Sentinel源码
https://github.com/alibaba/Sentinel
2 dashboard 改造
2.1修改dashboard项目的pom.xml
<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!--<scope>test</scope>-->
</dependency>
2.2 复制nacos 测试类
将 test/com.alibaba.csp.sentinel.dashboard.rule.nacos下所有文件复制到 src/main/java/com.alibaba.csp.sentinel.dashboard.rule 目录
其中 FlowRuleNacosProvider 是读取流控规则,FlowRuleNacosPublisher是上传流控规则到nacos 配置中心。
2.3 修改NacosConfig文件
修改方法nacosConfigService,目的是引入nacos配置参数
/*
* 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;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 {
private final Logger LOGGER = LoggerFactory.getLogger(NacosConfig.class);
@Value("${nacos.address}")
private String address;
@Value("${nacos.namespace}")
private String namespace;
@Value("${nacos.username}")
private String username;
@Value("${nacos.password}")
private String password;
@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, address);
properties.put(PropertyKeyConst.NAMESPACE, namespace);
properties.put(PropertyKeyConst.USERNAME, username);
properties.put(PropertyKeyConst.PASSWORD, password);
LOGGER.info("nacosConfigService init,address:{}",address);
return ConfigFactory.createConfigService(properties);
// return ConfigFactory.createConfigService("localhost");
}
}
配置引入
2.4 修改配置文件
nacos.address=localhost:8848
nacos.username=nacos
nacos.password=nacos
nacos.namespace=
config代码只用到这些,至此,配置改完了。下面是修改下控制台的规则controller.
2.5.修改FlowControllerV2文件
使用新复制的flowRuleNacosProvider、flowRuleNacosPublisher 替代default.代码如下:
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
2.6 修改左侧导航菜单页面
修改webapp/resources/app/scripts/directives/sidebar/sidebar.html 文件
搜索“流控规则”,把其中的“dashboard.flowV1”改为“dashboard.flow”
把原来的注释掉。
2.7 修改identity.js
第4行将“FlowServiceV1”替换为“FlowServiceV2”
修改保存规则方法saveFlowRule(),98行,“/dashboard/flow/”替换为“/dashboard/v2/flow/”
就是为了调用新改的controller。把修改后规则后将信息同步给 Nacos。
原来默认的接口是在内存中。
至此,改造结束。打包测试。
3 打包
打包注意只是dashboard工程就行,不用外层整个原码工程打包。
4 启动sentinel
注意,拷贝命令时,之前默认是带版本的,我还沿用了,报错提示: 找不到或无法加载主类 .port=8888 以及Error: Unable to access jarfile .\xxxx.jar
对应下版本就好,有时候脑子不转了还很奇怪,为啥突然报错了。
至此,sentinel dashboard就准备好了。
5 验证项目准备
sentinel笔记9- 限流规则持久化(上)-CSDN博客
主要是验证项目引入依赖,修改配置文件。对照下看看,我在上一篇改过了,不用再改了,贴一下配置供参考:
<!--nacos-discovery 注册中心依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos-config 配置中心依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- sentinel 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--以nacos作为sentinel数据源的依赖-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
yml主要是看看sentinel那部分就行。
spring:
application:
name: tlmall-storage
cloud:
nacos:
# discovery:
# server-addr: tlmall-nacos-server:8848
config:
server-addr: tlmall-nacos-server:8848
file-extension: yml #指定配置文件扩展名为yml
sentinel:
transport:
port: 8719
# 添加sentinel的控制台地址
dashboard: tlmall-sentinel-dashboard:8888
datasource:
nacos:
nacos:
server-addr: localhost:8848
data-id: tlmall-storage-flow-rules
rule-type: flow
group-id: SENTINEL_GROUP
data-type: json
username: nacos
password: nacos
config:
import:
- optional:nacos:${spring.application.name}.yml
- optional:nacos:db-common.yml #数据库公共配置
- nacos:nacos-discovery.yml
- optional:nacos:seata-client.yml
6 验证
在dashbaord 新增流控规则,我刚才没截图,用编辑替代下。
然后再naocs看看
发现nacos多了条对应配置。如果我们在nacos 修改下阈值,会发现控制台也会同步修改,刷新下页面会展示。
测试下限流
这样sentinel重启后也不丢失规则,实现了sentinel 控制台与nacos的双向绑定。
其他的规则也是类似的。
好了,总体来说改动不大,有点麻烦。