Spring Cloud Alibaba整合Sentinel
Spring Cloud Alibaba 整合 Sentinel
一、需求
实现一个简单的 整合 sentinel,不涉及sentinel的用法
二、实现步骤
1、下载 sentinel dashboard
https://github.com/alibaba/Sentinel/releases
注意:
默认会启动
8080端口,如果端口冲突,可以在启动命令上加入-Dserver.port=新端口
默认用户名和密码[sentinel/sentinel]
启动控制台可用的配置项
2、服务提供者和消费者引入sentinel依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
注意: 在这个里面查找 sentinel-core的版本号,可以确定我们需要下载那个版本的 sentinel dashboard
3、配置控制台信息
spring:
sentinel:
transport:
# 控制台的地址
dashboard: localhost:8080
# 与控制台通讯的端口,默认是8719,不可用会一直+1,知道找到一个可用的
port: 8719
# 和控制台保持心跳的ip地址
client-ip: 127.0.0.1
# 发送心跳的周期,默认是10s
heartbeat-interval-ms: 10000
4、一个简答的整合就整合完了,访问资源,查看控制台。
三、整合Feigen
1、配置文件中加入 feign.sentinel.enabled=true 即可。
2、加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3、对 Feign接口的降级或限流等操作时,资源名称的格式为:httpmethod:protocol://requesturl
四、整合 RestTemplate
1、在RestTemplate上加入 @SentinelRestTemplate注解。
2、配置文件中加入 resttemplate.sentinel.enabled: true,默认就是 true
3、降级或限流时的处理
抄sentinel的官网描述,文本给出链接
@SentinelRestTemplate 注解的属性支持限流(blockHandler, blockHandlerClass)和降级(fallback, fallbackClass)的处理。
其中 blockHandler 或 fallback 属性对应的方法必须是对应 blockHandlerClass 或 fallbackClass 属性中的静态方法。
该方法的参数跟返回值跟 org.springframework.http.client.ClientHttpRequestInterceptor#interceptor 方法一致,其中参数多出了一个 BlockException 参数用于获取 Sentinel 捕获的异常。
比如上述 @SentinelRestTemplate 注解中 ExceptionUtil 的 handleException 属性对应的方法声明如下:
public class ExceptionUtil {
public static ClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) {
...
}
}
五、@SentinelResource的用法
1、@SentinelResource 注解用来标识资源是否被限流、降级。
2、一般推荐将 @SentinelResource 注解加到服务实现上
3、可以指定 blockHandler 或 fallback ,在发生异常时的处理。
六、代码路径
https://gitee.com/huan1993/spring-cloud-alibaba-parent/tree/master/sentinel
七、参考文档
Spring Cloud Alibaba整合Sentinel的更多相关文章
- Spring Cloud Alibaba整合Sentinel流控
前面我们都是直接通过集成sentinel的依赖,通过编码的方式配置规则等.对于集成到Spring Cloud中阿里已经有了一套开源框架spring-cloud-alibaba,就是用于将一系列的框架成 ...
- Spring Cloud Alibaba(10)---Sentinel控制台搭建+整合SpringCloudAlibaba
上一篇博客讲了Sentinel一些概念性的东西 Spring Cloud Alibaba(9)---Sentinel概述 这篇博客主要讲 Sentinel控制台搭建,和 整合SpringCloudAl ...
- Spring Cloud Alibaba(11)---Sentinel+Nacos持久化
Sentinel+Nacos持久化 有关Sentinel之前有写过两篇 Spring Cloud Alibaba(9)---Sentinel概述 Spring Cloud Alibaba(10)--- ...
- Spring Cloud alibaba网关 sentinel zuul 四 限流熔断
spring cloud alibaba 集成了 他内部开源的 Sentinel 熔断限流框架 Sentinel 介绍 官方网址 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentine ...
- Spring Cloud Alibaba 使用Sentinel实现接口限流
Sentinel是什么 Sentinel的官方标题是:分布式系统的流量防卫兵.从名字上来看,很容易就能猜到它是用来作服务稳定性保障的.对于服务稳定性保障组件,如果熟悉Spring Cloud的用户,第 ...
- Spring Cloud Alibaba 之 Sentinel 限流规则和控制台实例
这一节我们通过一个简单的实例,学习Sentinel的基本应用. 一.Sentinel 限流核心概念 在学习Sentinel的具体应用之前,我们先来了解一下Sentinel中两个核心的概念,资源和规则. ...
- Spring Cloud Alibaba 整合 Nacos 实现服务配置中心
在之前的文章 <Nacos 本地单机版部署步骤和使用> 中,大家应该了解了 Nacos 是什么?其中 Nacos 提供了动态配置服务功能 一.Nacos 动态配置服务是什么? 官方是这么说 ...
- Spring Cloud Alibaba:Sentinel实现熔断与限流
一.什么是Sentinel Sentinel,中文翻译为哨兵,是为微服务提供流量控制.熔断降级的功能,它和Hystrix提供的功能一样,可以有效的解决微服务调用产生的“雪崩效应”,为微服务系统提供了稳 ...
- spring cloud gateway整合sentinel作网关限流
说明: sentinel可以作为各微服务的限流,也可以作为gateway网关的限流组件. spring cloud gateway有限流功能,但此处用sentinel来作为替待. 说明:sentine ...
随机推荐
- epoll经典代码示例
1. epoll原理 原理性的知识不再另做说明,我在这里附上收藏整理的两篇经典文章: select与epoll的本质关系. select.poll.epoll之间的区别. 2. epoll服务器端经典 ...
- openswan发送状态机分析
openswan发送状态机分析 1. 函数调用关系 2. 函数说明 如果按用户空间.内核空间划分的话,此部分代码更多是运行在内核空间的. 2.1 ipsec_tunnel_init_devices() ...
- 这些解决 Bug 的套路,你都会了不?
最近整理了我原创的 140 篇编程经验和技术文章,欢迎大家阅读,一起成长!指路:https://t.1yb.co/ARnD 大家好,我是鱼皮. 学编程的过程中,我们会遇到各式各样的 Bug,也常常因为 ...
- (2)java Spring Cloud+Spring boot+mybatis企业快速开发架构之SpringCloud-Spring Cloud是什么?Spring Cloud版本介绍
Spring Cloud 是一系列框架的有序集合.它利用 Spring Boot 的开发便利性,巧妙地简化了分布式系统基础设施的开发,如服务注册.服务发现.配置中心.消息总线.负载均衡.断路器.数 ...
- spark相关介绍-提取hive表(一)
本文环境说明 centos服务器 jupyter的scala核spylon-kernel spark-2.4.0 scala-2.11.12 hadoop-2.6.0 本文主要内容 spark读取hi ...
- 学生信息管理系统.cpp(大二上)
#include<iostream> #include<fstream> #include<string> #include<iomanip> #i ...
- 安卓使用讯飞sdk报错
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.iflytek.cloud.SpeechSy ...
- 解决下载的css样式文件在同一排的问题
一.将样式文件里的所有内容复制到word里 Ctrl+F查找替换,将所有分号;替换成;^p 小提示:在word里^p表示回车 二.将央视文件里的所有反括号}进行替换替换成}^p然后将代码整个粘贴回样式 ...
- springboot多数据源配合docker部署mysql主从实现读写分离
本篇主要有两部分: 1.使用docker部署mysql主从 实现主从复制 2.springboot项目多数据源配置,实现读写分离 一.使用docker部署mysql主从 实现主从复制 此次使用的是wi ...
- cmake入门:01 构建一个简单的可执行程序
一.目录结构 CMakeLists.txt:cmake 工程入口文件,包含当前目录下的工程组织信息.cmake 指令根据此文件生成相应的 MakeFile 文件. Hello.c: 源代码文件 bui ...