目录
一、Seninel简介
 二、Sentinel和Hystrix的区别
三、sentinel可视化界面安装
四、在springcloudalibaba中整合sentinel
(1)添加依赖
(2)配置yml
(3)启动服务,再访问服务后,观察控制台:因为访问接口以后才会注册到sentinel当中。
五、流控规则
(1)实时监控,可用于查看接口访问情况
(2)簇点链路,可以对对应的资源流控降级
(3)QPS流控
(4)线程流控
(5)关联限流
(6)熔断降级
六、OpenFeign整个Sentinel
(1)导入依赖:
(2)调用者开发整合配置:
(3)添加openFeign调用接口
七、规则持久化
(1)引入依赖
(2)为nacos添加配置
一、Seninel简介

 二、Sentinel和Hystrix的区别

三、sentinel可视化界面安装

下载对应版本的sentinel的jar包,通过终端命令:
java -jar jar包名
启动

访问对应路径:控制台如下:

四、在springcloudalibaba中整合sentinel
(1)添加依赖
        <!--sentinel启动器-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
(2)配置yml
server:
  port: 8002
spring:
  application:
    name: WXL-DEV-SERVICE-2
  cloud:
    sentinel:
      transport:
        dashboard: 127.0.0.1:8080
(3)启动服务,再访问服务后,观察控制台:因为访问接口以后才会注册到sentinel当中。

五、流控规则
(1)实时监控,可用于查看接口访问情况

(2)簇点链路,可以对对应的资源流控降级

可以设置阀值来流控:
(3)QPS流控

可以看到当每秒超过2次时被流控:
 流控文字可自定义:
    @GetMapping("/world")
    @SentinelResource(value = "helloWorld",blockHandler = "helloBlock")
    public String helloWorld() {
        return "Hello world";
    }
    public String helloBlock(BlockException e){
        return "你已被流控";
    }
value将该方法定义为sentinel的资源,blockHandler是流控时调用的方法。

(4)线程流控

(5)关联限流

这里的意思是如果/hello/add接口一秒钟之内访问超过2次,则/hello/query会被限流。
(6)熔断降级

也要设置熔断时长,熔断时长过完之后会进入半开状态,即若下一次请求为慢请求则再次熔断,直到第一次请求不是慢请求才会恢复正常状态。
六、OpenFeign整个Sentinel
(1)导入依赖:
        <!--OpenFeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
(2)调用者开发整合配置:
feign:
  sentinel:
    enabled: true #开启openFeign对sentinel的整合
(3)添加openFeign调用接口
package com.wxl.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "WXL-DEV-SERVICE-2", path = "/hello",fallback = ServiceFailFeign.class)
public interface Service1HelloInterface {
    @GetMapping("/world")
    String helloWorld();
}
并且这里添加参数fallback值为失败时回调的实现类。
实现类如下:
package com.wxl.feign;
import org.springframework.stereotype.Component;
@Component
public class ServiceFailFeign implements Service1HelloInterface{
    public String helloWorld() {
        return "降级了!!!";
    }
}
当接口请求失败时便会调用失败类里的该方法。
这里我们为了使用效果,在服务生产者的接口里故意写入报错代码:
    @GetMapping("/world")
    public String helloWorld() {
        int i=1/0;
        return "Hello world";
    }
请求该消费者服务接口:

调用了失败回调方法!
七、规则持久化
(1)引入依赖
        <!--sentinel持久化存储-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
(2)为nacos添加配置

更多见:
sentinel配置 持久化到nacos_yueF_L的博客-CSDN博客_sentinel持久化到nacos
相关文章
暂无评论...

 二、Sentinel和Hystrix的区别