Sentinel使用Nacos存储规则

准备服务启动 Nacos:http://10.17.12.160:8848 Sentinel Dashboard:http://10.17.12.158:8084

新建测试模块sentinel-nacos

pom.xml

<?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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>sentinel-nacos</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sentinel-nacos</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-datasource-nacos -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>1.6.2</version>
        </dependency>

    </dependencies>
    <!--
            引入依赖,版本好使用的是大于0.2.1.RELEASE,在dependencyManagement控制。
            因为spring-cloud-alibaba还没有加入spring-cloud的版本控制,所以需要自己引入的
     -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

spring.application.name=sentinel-datasource-nacos
server.port=8086
# sentinel dashboard的访问地址
spring.cloud.sentinel.transport.dashboard=http://10.17.12.158:8084
# nacos的访问地址
spring.cloud.sentinel.datasource.ds.nacos.server-addr=10.17.12.160:8848
#nacos中存储规则的dataId,对于dataId使用了${spring.application.name}变量,这样可以根据应用名来区分不同的规则配置
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel
#nacos中存储规则的groupId
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP
#定义存储的规则类型,该参数是spring cloud alibaba升级到0.2.2之后增加的配置
spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow

注意: spring.cloud.sentinel.datasource.ds.nacos.server-addr=10.17.12.160:8848 ,没有http://

控制器

package com.example.sentinelnacos.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello,World!";
    }
}

启动

控制台输出

2019-07-08 14:56:48.232  INFO 5108 --- [           main] o.s.c.a.s.c.SentinelDataSourceHandler    : [Sentinel Starter] DataSource ds-sentinel-nacos-datasource load 1 FlowRule
2019-07-08 14:56:48.289  INFO 5108 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8086 (http) with context path ''
2019-07-08 14:56:48.293  INFO 5108 --- [           main] c.e.s.SentinelNacosApplication           : Started SentinelNacosApplication in 3.856 seconds (JVM running for 5.023)

Sentinel控制台中修改规则:仅存在于服务的内存中,不会修改Nacos中的配置值,重启后恢复原来的值。 Nacos控制台中修改规则:服务的内存中规则会更新,Nacos中持久化规则也会更新,重启后依然保持。

存在问题:

如何使用Nacos集群持久化规则?

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券