sentinel组件

对于sentinel的前置知识这里就不多说了:

直接上代码:

Release v1.8.1 · alibaba/Sentinel · GitHub  下载地址

springcloud Alibaba环境下创建soringboot的项目:

POM:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>

application.yml
server:
port: 8089
spring:
application:
name: cloudorder
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
sentinel:
# eager: true
transport:
port: 8719
dashboard: localhost:8080
management:
endpoints:
web:
exposure:
include: "*"
feign:
sentinel:
enabled: true
主启动

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderApplication8088 {

public static void main(String[] args) {
SpringApplication.run(OrderApplication8088.class,args);
}

@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){

return new RestTemplate();
}
}
写一个controller接口就可以了:
@RequestMapping("/order")
public String test1(@RequestParam(name = "id")String id){

return UUID.randomUUID().toString()+ " id :"+ id;
}
先启动nacos注册中心,再启动sentinel的检测服务,本次用的是1.8.1的版本:
java -jar sentinel-dashboard-1.8.1.jar 就可以启动服务了

再启动我们的springboot服务
访问 localhost:8080就是sentinel的服务界面了 可以在上面配置服务的限流和降级的配置,以及热点key的配置

 @RequestMapping("/getFeign")
@SentinelResource(value = "getFeign",blockHandler = "demotionGetFeign",fallback = "deGetFeign")
public String getFeign(@RequestParam(name = "id",required = false,defaultValue = "5")String id) {

// int i = 10/0;
return "eeee eee "+proFeign.provide(id);
}

public String demotionGetFeign(String id, BlockException ex){

return id+" 服务降级了啊。。。o(╥﹏╥)o"+ex;
}

public String deGetFeign(String id){

return "限流了啊。。。。o(╥﹏╥)o";
}

@SentinelResource(value = "getFeign",blockHandler = "demotionGetFeign",fallback = "deGetFeign")
说一下这个注解就是配置接口出现异常 或者限流后应该怎么处理:
blockHandler 这个属性是在sentinel控制台配置的规则出现问题的时候会作出相应的处理方案
fallback 这个是兜底的方法了 代码出现异常 或者其他问题就会走这个方法了
需要注意的是
blockHandler 的降级方法的返回值和参数要和原来的方法一样,同时要加上
BlockException ex属性,代表出现异常后的信息

至于sentinel界面的配置就不多说了 可以看看官网写的

sentinel的持久化配置:

步骤:

  • 添加pom
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
  • 配置yml
spring:
application:
name: sentinel-service
cloud:
nacos:
discovery: #nacos服务注册中心地址
server-addr: www.cjlly.com:8848
sentinel:
transport:
dashboard: 127.0.0.1:8080
port: 8719
datasource:
ds1:
nacos:
server-addr: www.cjlly.com:8848
dataId: sentinel-service
groupId: DEFAULT_GROUP
data-type: json
rule_type: flow
  • 登陆nacos,新建配置规则sentinel-service
[
{
"resource": "/findById",
"limitApp":"default",
"grade":1,
"count":1,
"strategy":0,
"controlBehavior":0,
"clusterMode":false
}
]
naocs配置解读:

resource:资源名称

limitApp:来源应用

grade:阀值类型,0---线程数,1---QPS

count:单机阀值

strategy:流控模式,0---直接,1---关联,2---链路

controlBehavior:流控效果,0---快速失败,1---warmUp,2---排队等待

clusterMode:是否集群

需要注意地方:

  • 此时如果是Nacos集群,每个节点务必要配置到同一个数据库上。并且保证每个

    节点都可用。如果有的节点宕掉了可能会导致配置持久化失败。
  • 部署在nacos上的配置文件的名字并没有太多要求,只需要跟微服务项目中yml文件中配置的dataId一致即可。



spring cloud Alibaba --sentinel组件的使用的更多相关文章

  1. Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战

    Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战 Springboot: 2.1.8.RELEASE SpringCloud: Greenwich.SR2 ...

  2. Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战

    Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战 在阅读本文前,建议先阅读<Spring Cloud Alibaba | Sentinel:分布式系 ...

  3. 0.9.0.RELEASE版本的spring cloud alibaba sentinel实例

    sentinel即哨兵,相比hystrix断路器而言,它的功能更丰富.hystrix仅支持熔断,当服务消费方调用提供方发现异常后,进入熔断:sentinel不仅支持异常熔断,也支持响应超时熔断,另外还 ...

  4. Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探

    目录 Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探 1. Sentinel 是什么? 2. Sentinel 的特征: 3. Sentinel 的开源生 ...

  5. Spring Cloud Alibaba | Sentinel: 服务限流基础篇

    目录 Spring Cloud Alibaba | Sentinel: 服务限流基础篇 1. 简介 2. 定义资源 2.1 主流框架的默认适配 2.2 抛出异常的方式定义资源 2.3 返回布尔值方式定 ...

  6. Spring Cloud Alibaba | Sentinel: 服务限流高级篇

    目录 Spring Cloud Alibaba | Sentinel: 服务限流高级篇 1. 熔断降级 1.1 降级策略 2. 热点参数限流 2.1 项目依赖 2.2 热点参数规则 3. 系统自适应限 ...

  7. Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则

    Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则 前面几篇文章较为详细的介绍了Sentinel的使用姿势,还没看过的小伙伴可以访问以下链接查看: &l ...

  8. Spring Cloud Alibaba Sentinel对Feign的支持

    Spring Cloud Alibaba Sentinel 除了对 RestTemplate 做了支持,同样对于 Feign 也做了支持,如果我们要从 Hystrix 切换到 Sentinel 是非常 ...

  9. Spring Cloud Alibaba Sentinel对RestTemplate的支持

    Spring Cloud Alibaba Sentinel 支持对 RestTemplate 的服务调用使用 Sentinel 进行保护,在构造 RestTemplate bean的时候需要加上 @S ...

随机推荐

  1. 小Z的袜子 & 莫队

    莫队学习 & 小Z的袜子 引入 莫队 由莫涛巨佬提出,是一种离线算法 运用广泛 可以解决广大的离线区间询问题 莫队的历史 早在mt巨佬提出莫队之前 类似莫队的算法和莫队的思想已在Codefor ...

  2. Centos 6.8 系统下安装RabbitMQ方法

    一,安装 RabbitMQ 首先要先安装 erlang 1,到erlang官网下载 OTP 19.0 Source File 2,解压 tar zvxf otp_src_19.0.tar.gz 3,c ...

  3. PTA 面向对象程序设计6-2 统计数字

    对于给定的一个字符串,统计其中数字字符出现的次数. 类和函数接口定义: 设计一个类Solution,其中包含一个成员函数count_digits,其功能是统计传入的string类型参数中数字字符的个数 ...

  4. [Python]爬虫获取知乎某个问题下所有图片并去除水印

    获取URL 进入某个知乎问题的主页下,按F12打开开发者工具后查看network面板. network面板可以查看页面向服务器请求的资源.资源的大小.加载资源花费的时间以及哪些资源加载失败等信息.还可 ...

  5. 做一个U盘的学习路线

    最近想研究一个U盘,然后顺便熟悉一下USB协议.因为USB协议比较复杂, 常用的复杂外设除了WiFi,Ethernet,SDIO和USB这些就是USB了,学习USB的时候肯定要拿一个东西下手,所以简单 ...

  6. 性能再提升70%?大咖前瞻带你揭开.NET6的神秘面纱!

    本月初微软官宣.NET 6 的RC1即将在11月正式发布,这意味着.NET6正式版跟我们见面的时间又近了一步.在之前的.NET6预览版本中,微软加入了大量新功能特性,而在最终版本中将不再额外加入新的内 ...

  7. JS HTML5仿微信朋友圈特效

    完美! 图片相册翻页可定位在第几张,右上角可关闭. 源代码下载地址: 链接: https://pan.baidu.com/s/1o7PA7wu 密码: asyt

  8. django class类即视图类添加装饰器的几种方法

    根据别人发布整理,个人爱好收集(原文:https://blog.csdn.net/mydistance/article/details/83958655 ) 第一种:定义函数装饰器,在函数,类中使用函 ...

  9. 鸿蒙内核源码分析(进程回收篇) | 老父亲如何向老祖宗临终托孤 ? | 百篇博客分析OpenHarmony源码 | v47.01

    百篇博客系列篇.本篇为: v47.xx 鸿蒙内核源码分析(进程回收篇) | 临终前如何向老祖宗托孤 | 51.c.h .o 进程管理相关篇为: v02.xx 鸿蒙内核源码分析(进程管理篇) | 谁在管 ...

  10. CF19E-Fairy【树形结构,差分】

    正题 题目链接:https://www.luogu.com.cn/problem/CF19E 题目大意 给出\(n\)个点\(m\)条边的一张无向图,求有多少条边去掉后可以使得图变成一张二分图. \( ...