使用sentinel 做服务的流监控,需要把信息持久化到数据库,否则没有意义,今天我们分享信息持久化到nacos ,也就是持久化到nacos所配置的数据库里。
1、jar包引入到pom文件,此jar包是连接nacos的:
<!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-datasource-nacos -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.5.0</version>
</dependency>
配置文件配置:
server:
port: 9005
spring:
application:
name: register-server
cloud:
sentinel:
transport:
port: 8719 #如果加了客户端他就会开启一个 http server 为了dashboard 能够发过来
dashboard: localhost:8080
logging:
level:
root: info
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
file-extension: yml
server-addr: 127.0.0.1:8848
prefix: shadow
2、sentinel 的配置信息写到nacos中
import com.alibaba.csp.sentinel.datasource.WritableDataSource;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
public class WriteableNacos<T> implements WritableDataSource<T> {
//当sentinel 添加或更新信息的时候,把信息持久化到nacos中,自己实现的方法,官方只提供了接口
@Override
public void write(T value) throws Exception {
System.out.println("-------------write-----------");
String s = JSON.toJSONString(value);
System.out.println(s);
String serverAddr = "localhost:8488";
String dataId = "shadow";
String group = "DEFAULT_GROUP";
ConfigService configService = NacosFactory.createConfigService(serverAddr);
boolean isPublishOk = configService.publishConfig(dataId, group, s);
System.out.println(isPublishOk);
}
@Override
public void close() throws Exception {
}
}
接口源码:
3、sentinel 从nacos中读取持久化后的信息,nacos中提供了读信息的接口,没有提供写入nacos的接口,需要自己扩展:
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.WritableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;
@Component
public class SentinelUtil {
@PostConstruct
public void init() throws NacosException {
System.out.println("-------------init-----------");
String serverAddr = "localhost:8488";
String dataId = "shadow";
String group = "DEFAULT_GROUP";
//namespace
// final String rule = "[\n"
// + " {\n"
// + " \"resource\": \"login\",\n"
// + " \"controlBehavior\": 0,\n"
// + " \"count\": 5.0,\n"
// + " \"grade\": 1,\n"
// + " \"limitApp\": \"default\",\n"
// + " \"strategy\": 0\n"
// + " }\n"
// + "]";
//
// ConfigService configService = NacosFactory.createConfigService(serverAddr);
// boolean isPublishOk = configService.publishConfig(dataId, group, rule);
// System.out.println(isPublishOk);
//jar包中提供的读取配置的接口
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(serverAddr, group, dataId,
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
//System.out.println(flowRuleDataSource.getProperty());
// 将可写数据源注册至 transport 模块的 WritableDataSourceRegistry 中.
// 这样收到控制台推送的规则时,Sentinel 会先更新到内存,然后将规则写入到文件中.
WritableDataSource writableDataSource = new WriteableNacos<List<FlowRule>>();
WritableDataSourceRegistry.registerFlowDataSource(writableDataSource);
4、查看源码只有写的接口:
5、点击看类的继承: NacosDataSource<T> extends AbstractDataSource<String, T>,发现只实现了一个read相关的接口:
6、启动nacos 服务端,启动源码或程序都行:
7、访问nacos 客户端页面,配置客户端信息:
先创建相应服务信息:
8、启动sentinel服务,访问客户端:
9、初次访问后页面显示,证明已经关联了此客户端服务:
10、启动客户端信息进行初始化,首先到了nacos中持久化:
11、在sentinel客户端显示出来:
10、在sentinel 加一下配置,就能显示到nacos服务里
nacos客户端里持久化并显示:
11、 此时如果sentinel服务重启后,信息能显示到客户端,证明持久化成功。
springcloudAlibaba 组件 sentinel 的持久化已经分享了,下篇分析其他菜单功能的使用。