环境准备:
Nacos2.2.0集群搭建
版本说明:https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明
本次记录修改好的控制台代码,需要的话自取:Gitee、Github
Sentinel1.8.6:https://github.com/alibaba/Sentinel/releases/tag/1.8.6
大概104行,注释掉 test
com.alibaba.csp</groupId>
sentinel-datasource-nacos</artifactId>
<!--test</scope>-->
</dependency>
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-maven-plugin
目录介绍:
FlowRuleNacosProvider: 动态获取Nacos配置中心流控规则
FlowRuleNacosPublisher: publish上传流控规则到Nacos配置中心
NacosConfig: Nacos配置
NacosConfigUtils: 流控规则配置
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;
}
}
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, String> flowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter, List> 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);
}
}
com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2 中指定对应的 bean
@Autowired
// @Qualifier("flowRuleDefaultProvider")
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider> ruleProvider;
@Autowired
// @Qualifier("flowRuleDefaultPublisher")
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher> rulePublisher;
修改resources/app/scripts/directives/sidebar/sidebar.html
直接搜dashboard.flowV1
定位57行去掉V1
<!--"active" ng-if="!entry.isGateway">
"dashboard.flowV1({app: entry.app})">
class="glyphicon glyphicon-filter"></i> 流控规则</a>
</li>-->
"active" ng-if="!entry.isGateway">
"dashboard.flow({app: entry.app})">
class="glyphicon glyphicon-filter"></i> 流控规则</a>
</li>
修改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;
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=
访问地址:http://localhost:9100/#/login
默认账号:sentinel
默认密码:sentinel
项目的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
@RestController
@RequestMapping("/api/admin")
public class OrderController {
@GetMapping("/sentinel")
public String sentinelCS() {
return "sentinel控制台测试";
}
}
最后,希望Sentinel 官方优化下配置,将持久化集成进配置文件,这样只需要到配置文件选择任一 一种持久化存储就行,同时也祝国内开源越来越好!
搞定,感谢阅览~
END