温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot中怎么利用Sentinel实现接口流量控制

发布时间:2021-07-08 17:07:24 来源:亿速云 阅读:183 作者:Leah 栏目:系统运维

这期内容当中小编将会给大家带来有关SpringBoot中怎么利用Sentinel实现接口流量控制,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

项目搭建

首先我们来创建一个测试项目,这里初始化项目的url建议大家填写阿里云的地址,会有惊喜?

http://start.aliyun.com

 SpringBoot中怎么利用Sentinel实现接口流量控制

接下来就是常规操作,一路next,在下图的位置稍微注意一下

SpringBoot中怎么利用Sentinel实现接口流量控制

说明:

同大家以前创建项目一样,只需要在这里勾选Sentinel就可以啦?

项目创建好以后,我们发现pom文件中引入了下面的依赖

SpringBoot中怎么利用Sentinel实现接口流量控制

有的小伙伴看网上博客,也会有下面的方式,指定版本号

<!-- sentinel -->  <dependency>   <groupId>com.alibaba.cloud</groupId>   <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>   <version>2.1.0.RELEASE</version>  </dependency>

如果你使用我推荐的阿里云的Url,会发现Sentinel的版本号都定义父工程,Cloud的各个组件的兼容性就不要大家操心了

<dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-dependencies</artifactId>                <version>${spring-boot.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>            <dependency>                <groupId>com.alibaba.cloud</groupId>                <artifactId>spring-cloud-alibaba-dependencies</artifactId>                <version>${spring-cloud-alibaba.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>

打开项目配置文件,会发现它已经为我们自动加好了配置,真的超级方便?

server.port=8083 # 应用名称 spring.application.name=springcloud-sentinel # Sentinel 控制台地址 spring.cloud.sentinel.transport.dashboard=localhost:8080 # 取消Sentinel控制台懒加载 # 默认情况下 Sentinel 会在客户端首次调用的时候进行初始化,开始向控制台发送心跳包 # 配置 sentinel.eager=true 时,取消Sentinel控制台懒加载功能 spring.cloud.sentinel.eager=true # 如果有多套网络,又无法正确获取本机IP,则需要使用下面的参数设置当前机器可被外部访问的IP地址,供admin控制台使用 # spring.cloud.sentinel.transport.client-ip=# sentinel 配置 spring.application.name=frms spring.cloud.sentinel.transport.dashboard=localhost:8080 spring.cloud.sentinel.transport.heartbeat-interval-ms=500

如何定义资源

编程式定义

官网提供的demo

package com.milo.sentinel;  import com.alibaba.csp.sentinel.Entry; import com.alibaba.csp.sentinel.SphU; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;  import java.util.ArrayList; import java.util.List;  /**  * 项目入口  * @author Milo Lee  * @date 2021-3-20 19:07  *  */ @SpringBootApplication public class SentinelApplication {      public static void main(String[] args) {         SpringApplication.run(SentinelApplication.class, args);          // 配置规则.         initFlowRules();         while (true) {             // 1.5.0 版本开始可以直接利用 try-with-resources 特性             try (Entry entry = SphU.entry("HelloWorld")) {                 // 被保护的逻辑                 Thread.sleep(300);                 System.out.println("hello world");             } catch (BlockException | InterruptedException ex) {                 // 处理被流控的逻辑                 System.out.println("blocked!");             }         }      }      private static void initFlowRules(){         List<FlowRule> rules = new ArrayList<>();         FlowRule rule = new FlowRule();         rule.setResource("HelloWorld");         rule.setGrade(RuleConstant.FLOW_GRADE_QPS);         // Set limit QPS to 20.         rule.setCount(20);         rules.add(rule);         FlowRuleManager.loadRules(rules);     }  }

注解式定义

@SpringBootApplication public class Application {      public static void main(String[] args) {         SpringApplication.run(ServiceApplication.class, args);     } }  @Service public class TestService {      @SentinelResource(value = "sayHello")     public String sayHello(String name) {         return "Hello, " + name;     } }  @RestController public class TestController {      @Autowired     private TestService service;      @GetMapping(value = "/hello/{name}")     public String apiHello(@PathVariable String name) {         return service.sayHello(name);     } }

@SentinelResource 注解用来标识资源是否被限流、降级。上述例子上该注解的属性 sayHello 表示资源名。

启动控制台

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.1.jar

 SpringBoot中怎么利用Sentinel实现接口流量控制

控制台配置规则

控制台的操作我们用编程式定义的例子来演示,大家启动我们的服务

SpringBoot中怎么利用Sentinel实现接口流量控制

我们会发现除了sentinel-dashboard之外,多了一个milolee-sentinel,这个就是我们的服务,它的名称其实对应我们配置文件定义的应用名称:

# 应用名称 spring.application.name=milolee-sentinel

点击机器列表,这这里如果能发现你的机器,那就是成功上线了

SpringBoot中怎么利用Sentinel实现接口流量控制

实时监控

SpringBoot中怎么利用Sentinel实现接口流量控制

簇点链路

SpringBoot中怎么利用Sentinel实现接口流量控制

流控规则配置

给我们的资源HelloWorld配置流控规则,它的QPS(每秒请求数)为1,如图:

SpringBoot中怎么利用Sentinel实现接口流量控制

通过查看实时监控,我们发现已经生效

SpringBoot中怎么利用Sentinel实现接口流量控制

降级规则配置

给我们的资源HelloWorld添加一个降级规则配置,如果QPS大于1,且平均响应时间大于20ms,则接口下来接口在2秒钟无法访问,之后自动恢复。

SpringBoot中怎么利用Sentinel实现接口流量控制

目前这些规则仅在内存态生效,应用重启之后,该规则会丢失。后续文章我们会继续学习动态规则

SpringBoot中怎么利用Sentinel实现接口流量控制

上述就是小编为大家分享的SpringBoot中怎么利用Sentinel实现接口流量控制了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI