Sentinel持久化到Nacos

Sentinel 的工作模式

1、原始模式(非生产环境推荐模式):不持久化,重启微服务,配置的限流规则就会丢失

2、Pull模式(非生产环境推荐模式):(拉模式),将规则缓存到本地文件

3、push模式(生产环境推荐模式):将规则在云端配置中心,然后将云端配置推送至sentinel dashboard,在dashboard配置的规则也可以知道同步到云端配置中心

下面重点介绍第三种模式(push模式)

1.pom.xml添加依赖

<parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.1.6.RELEASE</version>

        <relativePath/>

    </parent>

<!--引入父依赖 -->

<dependencyManagement>

        <dependencies>

     <!--spring-cloud-dependencies-->

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-dependencies</artifactId>

                <version>Hoxton.SR9</version>

                <type>pom</type>

            <scope>import</scope>

            </dependency>

            <!--alibaba nacos -->

            <dependency>

                <groupId>com.alibaba.cloud</groupId>

                <artifactId>spring-cloud-alibaba-dependencies</artifactId>

                <version>2.2.2.RELEASE</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

</dependencies>

    </dependencyManagement>

<dependencies>

<!--springcloud 子项目nacos配置中心依赖 -->

        <dependency>

            <groupId>com.alibaba.cloud</groupId>

            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>

        </dependency>

<!--sentinel依赖  -->

    <dependency>

        <groupId>com.alibaba.cloud</groupId>

        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>

    </dependency>

<!--sentinel 持久化nacos依赖 -->

     <dependency>

            <groupId>com.alibaba.csp</groupId>

            <artifactId>sentinel-datasource-nacos</artifactId>

        </dependency>

2.在application.yml文件中添加配置内容,本人使用的是yml文件格式,也可以使用properties格式

spring:

  cloud:

    sentinel:

      transport:

        dashboard: ip:端口 #链接sentinel dashboard

      datasource: #dashbord的数据由下来库中获取

        flow: #此名称可以随意取,该值得含义是key,不过最好见名知意,如限流用flow、降级用degrade等

          nacos:

            serverAddr: 127.0.0.1:80

            dataId: ${spring.application.name}.json

            groupId: DEFAULT_GROUP

            ruleType: flow

            dataType: json

3.在nacos中添加规则


4.创建测试类

@RestControllerpublicclass Test {

    @GetMapping("/test")

    public String test() {

        return"test sentinel";

    }

}


访问几次接口之后,就可以在Sentinel Dashboard 中看到在nacos中配置的规则信息了,并且项目服务重启依然存在。

以上的代码是经过本人运行过的,希望对看到的您有所帮助!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容