修改后代码位置: https://gitee.com/maju_blogs/sentinel-dashboard-nacos/tree/master/sentinel-dashboard
1.从github上下载源码
git clone https://github.com/alibaba/Sentinel.git
2.复制test下的nacos文件到rule下
3.修改NacosConfig,配置序列化方式以及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.nacos;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.*;
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("${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);
return ConfigFactory.createConfigService(properties);
}
}
4.在application下增加配置
nacos.address=ydfm.cc:8848
nacos.namespace=sentinel-rule
nacos.username=nacos
nacos.password=nacos
server.port=8889
5.修改controller下FlowControllerV2使用的bean为nacos
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
6.修改前端页面,将V1改成V2
修改页面
7.nacos新增命名空间sentinel-rule,第四步未指定可以不用配置
8.启动服务完成Sentinel-Dashboard配置
9.springboot引入依赖
FAQ:如果新增规则不显示,可能是自己引入fastjson版本不对,移除自己引入fastjson
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.5</version>
</dependency>
10.服务配置yml
spring:
cloud:
sentinel:
transport:
dashboard: 192.168.123.65:8889
clientIp: 192.168.123.65
eager: true
datasource:
flow:
nacos:
server-addr: ydfm.cc:8848
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
namespace: sentinel-rule
rule-type: flow
11.启动服务触发请求
控制台监控到该请求
增加限流配置
查看限流配置