一、涉及的技术:sentinel、nacos、微服务
二、原理:利用sentinel-datasource-nacos实时与nacos通信,将sentinel配置实时持久化到nacos配置中心,用户可以操作nacos配置或者sentinel平台直接对微服务进行管控。
三、实现:
1.升级聚合项目依赖(基础项目搭建参考:微服务架构-权限篇_Morik的博客-CSDN博客),尽量选择最新稳定版的,以下参考版本支持sentinel 1.8.1
?2、配置common模块依赖,目前这个模块只是用来测试sentinel的持久化,暂时没放任何公共业务
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>umf</artifactId>
<groupId>cn.morik</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>cn.morik.umf</groupId>
<artifactId>common</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
3、配置common模块yml
server:
port: 9999
spring:
application:
name: common-ser
cloud:
nacos:
discovery:
server-addr: 192.168.1.9:8848
sentinel:
transport:
dashboard: 192.168.0.9:8080
datasource:
db1:
nacos:
server-addr: 192.168.1.9:8848
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
data-type: json
rule-type: FLOW
management:
endpoints:
web:
exposure:
include: "*"
4、docker 重新编译下sentinel,原始包没有实现nacos持久化, 修改后的源码jar包下载参考地址:(链接:https://pan.baidu.com/s/19TOAV4iACo7JQOvDYvJvNA?提取码:2b5o)
?5、启动common微服务、docker-command启动重新编译后的sentinel
四、效果展示:
1、 添加流控规则
2、查看nacos持久化的配置文件、nacos和sentinel修改两边测试同步性
?
|