SpringCloudAlibaba——Sentinel(二)流控实例Demo

一、介绍

本实例分为消费者和生产者:生产者查询数据库,消费者调用生产者接口。
引入了Nacos注册中心、Ribbon负载均衡、Sentinel流量控制,服务调用使用RestTemplate、持久层使用JPA

SpringCloudAlibaba——Sentinel分布式系统的流量防卫兵
SpringCloudAlibaba——Nacos注册中心、配置中心的使用

消费者目录结构:
在这里插入图片描述
生产者目录结构:
在这里插入图片描述

二、pom依赖

2.1 消费者POM

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <!--引入 Sentinel 依赖-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.8.1</version>
        </dependency>
        <!--Sentinel注解支持 @SentinelResource 注解,注解方式埋点不支持 private 方法。-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
            <version>1.8.1</version>
        </dependency>
        <!--客户端需要引入 Transport 模块来与 Sentinel 控制台进行通信-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>1.8.1</version>
        </dependency>
        <!-- sentinel整合Spring -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <!--SpringBootweb-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--SpringBoot测试类-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>
        <!-- springcloud alibaba nacos discovery -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <!-- springcloud alibaba nacos config -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
        <!-- Ribbon -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
    </dependencies>

2.2 生产者POM

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <!--引入 Sentinel 依赖-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.8.1</version>
        </dependency>
        <!--Sentinel注解支持 @SentinelResource 注解,注解方式埋点不支持 private 方法。-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
            <version>1.8.1</version>
        </dependency>
        <!--客户端需要引入 Transport 模块来与 Sentinel 控制台进行通信-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>1.8.1</version>
        </dependency>
        <!--SpringDataJPA-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- Mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
        <!-- sentinel整合Spring -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
        <!--SpringBootweb-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--SpringBoot测试类-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>
        <!-- springcloud alibaba nacos discovery -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <!-- springcloud alibaba nacos config -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
    </dependencies>

三、源码分析

3.1 生产者

控制层直接查数据库(省略了业务层)
在这里插入图片描述
表结构:
在这里插入图片描述
在这里插入图片描述

表映射实体类:
在这里插入图片描述
数据访问层:
在这里插入图片描述

3.2 消费者(注解方式定义资源)

控制层:

Sentinel 支持通过 @SentinelResource 注解定义资源并配置 blockHandlerfallback 函数来进行限流之后的处理。
在这里插入图片描述

消费者启动类:
在这里插入图片描述
注入RestTemplate,使用Ribbon的@LoadBalanced注解开启负载均衡。
注意使用了@LoadBalanced注解,在RestTemplate调用的时候URL需要使用Nacos注册中心注册的服务名:
在这里插入图片描述
在这里插入图片描述

3.3 配置文件

sentinel配置文件:

project.name=SentinelDemo2021-06-1715:02:51
#单个监控日志文件的大小 long 52428800 (50MB)
csp.sentinel.metric.file.single.size=52428800
#监控日志文件的总数上限 默认6
csp.sentinel.metric.file.total.count=2
# 日志默认${user.home}/logs/csp/
csp.sentinel.log.dir=/Users/LiuShihao/IdeaProjects/sentinel-demo/logs
#日志文件名中是否加入进程号,用于单机部署多个应用的情况 默认 boolean false
csp.sentinel.log.use.pid=false
#Record 日志输出的类型,file 代表输出至文件,console 代表输出至终端
csp.sentinel.log.output.type=console
#控制台的地址,指定控制台后客户端会自动向该地址发送心跳包。地址格式为:hostIp:port
csp.sentinel.dashboard.server=localhost:8080
#心跳包发送周期,单位毫秒 long
csp.sentinel.heartbeat.interval.ms=30000
#本地启动 HTTP API Server 的端口号 int 默认为 8719 若端口冲突会自动向下探测可用的端口。
csp.sentinel.api.port=8719
#指定心跳包中本机的 IP
#csp.sentinel.heartbeat.client.ip

消费者application.yml

server:
  port: 8081
spring:
  application:
    name: sentinel-consumer
  cloud:
    nacos:
      config:
        # Nacos配置中心地址
        server-addr: 127.0.0.1:8848
        # 配置文件格式
        file-extension: yml
        # 共享配置
        shared-configs:
          - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

生产者application.yml

server:
  port: 8082
spring:
  application:
    name: sentinel-provider
  jpa:
    show-sql: true
    hibernate:
      naming:
        # 取消小驼峰到下划线映射(加上这个)
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  datasource:
    url: jdbc:mysql://localhost:3306/day27?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
  cloud:
    nacos:
      config:
        # Nacos配置中心地址
        server-addr: 127.0.0.1:8848
        # 配置文件格式
        file-extension: yml
        # 共享配置
        shared-configs:
          - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

四、启动测试

在这里插入图片描述

在这里插入图片描述

在浏览器地址访问:http://localhost:8081/consumer/getDept/1

在这里插入图片描述
可以在Sentinel仪表盘上看到服务的监控信息。
在这里插入图片描述
在这里插入图片描述
给消费者添加流控规则
在这里插入图片描述
QPS:每秒访问量。

当我们连续刷新的时候就会报错:
在这里插入图片描述
在这里插入图片描述

从而达到流量控制的目的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Liu_Shihao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值