美文网首页
springboot-alibabacloud-sentinel

springboot-alibabacloud-sentinel

作者: 前进的码农 | 来源:发表于2020-12-01 21:33 被阅读0次

    之前的教程

    springboot-alibabacloud-sentinel-安装运行(控制台运行)-01
    https://www.jianshu.com/p/dcf4f875def2
    springboot-alibabacloud-sentinel-使用-02
    https://www.jianshu.com/p/17557bec9c31
    springboot-alibabacloud-sentinel-自定义返回-03
    https://www.jianshu.com/p/82917e7e996e

    之前的配置每次服务重启配置都会消失,那么怎么让配置持久化呢?setinel提供了多种方案!这里我们介绍和nacos结合实现配置持久化。

    引入pom

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>com.ethan</groupId>
                <artifactId>aliclound-comm</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <!-- 关键-->
            <dependency>
                <groupId>com.alibaba.csp</groupId>
                <artifactId>sentinel-datasource-nacos</artifactId>
            </dependency>
            <!-- 关键-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            </dependency>
    

    bootstrap.properties配置

    spring.application.name=product-services
    server.port=8084
    spring.profiles.active=dev
    spring.cloud.nacos.config.server-addr=127.0.0.1:8848
    spring.cloud.nacos.config.prefix=product-services
    spring.cloud.nacos.config.namespace=dev
    spring.cloud.nacos.config.group=aliclound
    
    spring.cloud.sentinel.transport.dashboard=localhost:8080
    spring.cloud.sentinel.datasource.ds2.nacos.server-addr=localhost:8848
    #记住这里在配置nacos的时候dataid也写这个不要写后缀
    spring.cloud.sentinel.datasource.ds2.nacos.data-id=sentinel
    spring.cloud.sentinel.datasource.ds2.nacos.namespace=dev
    spring.cloud.sentinel.datasource.ds2.nacos.group-id=aliclound
    spring.cloud.sentinel.datasource.ds2.nacos.data-type=json
    spring.cloud.sentinel.datasource.ds2.nacos.rule-type=flow
    

    参数含义

    image.png

    nacos配置

    image.png

    看dataid的文件没有后缀

    controller

    随便写一个即可

    @RestController
    @RequestMapping("/product-services/product")
    @Slf4j
    @RefreshScope
    public class ProductController {
        @Value("${user.name:123}")
        private String name;
        @Value("${user.pwd:123}")
        private String pwd;
    
        @GetMapping("user_value")
        public String getUserValue() {
             int i = 10;
             int b =  i/0;
            log.info("name:"+name+" pwd:"+pwd);
            return "name:"+name+" pwd:"+pwd;
        }
    }
    

    效果

    通过测试效果可以得出,在nacos配置修改配置会同步到sentinel但是在sentinel中修改了配置不会同步到nacos配置只能在nacos里面修改,不然数据无法实现一致性。下一个教程我们解决这个问题。

    相关文章

      网友评论

          本文标题:springboot-alibabacloud-sentinel

          本文链接:https://www.haomeiwen.com/subject/rvbdwktx.html