前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Sentinel使用Nacos存储规则

Sentinel使用Nacos存储规则

作者头像
程裕强
发布2019-07-11 13:37:28
9090
发布2019-07-11 13:37:28
举报

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

新建测试模块sentinel-nacos

pom.xml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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://

控制器

代码语言:javascript
复制
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!";
    }
}

启动

控制台输出

代码语言:javascript
复制
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集群持久化规则?

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年07月08日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • pom.xml
  • application.properties
  • 控制器
  • 启动
  • 存在问题:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档