sentinel控制台,使用方便,功能强大。使用官方的jar包,配置不会持久化,sentinel重启后会导致,之前的规则全部丢失,下面一起改造源码实现规则数据的持久化
(github访问太慢,直接上镜像版)
Sentinel: Sentinel 是什么 随着微服务的流行,服务和服务之间的稳定性变得越来越重要https://gitee.com/mirrors/Sentinel.git
因为项目使用的是Spring-cloud-alibaba,Sentinel支持和nacos整合,就持久化到nacos数据库中,同时sentinel还能读取nacos中做的流控规则。
小惊喜:sentinel中有和nacos中对接的源码,只不过没有使用。
1、改成 默认后台使用sentinel对接nacos,而不是存到内存,
2、前台页面接口调用nacos对应的接口
pom.xml中将sentinel-datasource-nacos
包的scope注释掉
不多说,先复制到main目录rule包下
nacos包中的4个类:
只实现了本地nacos并且需要默认配置,需要支持自定义配置
改造后
NacosConfig源码
/*
* 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.Value;
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 {
@Value("${sentinel.nacos.address}")
private String nacosAddr;
@Value("${sentinel.nacos.username}")
private String nacosUsername;
@Value("${sentinel.nacos.password}")
private String nacosPassword;
@Bean
public Converter, String> flowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter> flowRuleEntityDecoder() {
return s -> JSON.parseArray(s, FlowRuleEntity.class);
}
@Bean
public ConfigService nacosConfigService() throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, nacosAddr);
properties.put(PropertyKeyConst.USERNAME, nacosUsername);
properties.put(PropertyKeyConst.PASSWORD, nacosPassword);
return ConfigFactory.createConfigService(properties);
}
}
增加nacos配置信息
#Sentinel 连接nacos配置
sentinel.nacos.address= 192.168.1.109:8848
sentinel.nacos.username= nacos
sentinel.nacos.password= nacos
文件 src/main/webapp/resources/app/scripts/controllers/identity.js
搜FlowServiceV1 改为 FlowServiceV2
搜/dashboard/flow/ 改为 /dashboard/v2/flow/
文件 src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html
搜dashboard.flowV1
定位57行去掉V1
文件 src/main/webapp/resources/app/views/flow_v2.html
注释掉回到单机页面
按钮
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.csp
sentinel-datasource-nacos
# Spring
spring:
cloud:
sentinel:
eager: true
# sentinel 地址
transport:
dashboard: ${sentinel.host}:${sentinel.port}
filter:
enabled: false
datasource:
ds1:
nacos:
server-addr: ${nacos.host}:${nacos.port}
username: ${nacos.name}
password: ${nacos.pwd}
namespace: ${nacos.namespace}
group-id: ${nacos.group}
data-id: ${spring.application.name}-flow-rules
data-type: json
rule-type: flow
3 nacos增加sentinel持久化配置文件
以下文件后缀与组名需要对应
大功告成,去试试效果吧