十六、springCloud Alibaba Sentinel阿里版的Hystrix

下载:

https://github.com/alibaba/Sentinel/         本次下载1.7.0版本

启动:

H:\softwar>java -jar sentinel-dashboard-1.7.0.jar
INFO: log output type is: file
INFO: log charset is: utf-8
INFO: log base dir is: C:\Users\smy\logs\csp\
INFO: log name use pid is: false

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.5.RELEASE)

访问:http://127.0.0.1:8080     sentinel/sentinel登录

监控本地服务:

新建服务pom:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

yml配置文件:

server:
  port: 8401
spring:
  application:
    name: cloud-alibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    sentinel:
      transport:
        dashboard: 127.0.0.1:8080
        port: 8719

management:
  endpoints:
    web:
      exposure:
        exclude: '*'

主类:

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentSentinelMain8401 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentSentinelMain8401.class,args);
    }

    @RestController
    public class FlowLimitController{
        @GetMapping("/testA")
        public String testA(){
            return "testA";
        }

    }
}

启动,然后打开http://127.0.0.1:8080/#/dashboard/home   发现没服务,因为采用的是懒加载,因此先请求一下8041服务,再打开sentinel

如下:

流控直接:狂点 http://127.0.0.1:8401/testA    抛出 

Blocked by Sentinel (flow limiting)
        @GetMapping("/testA")
        public String testA() throws Exception {
            return "testA";
        }

流控关联:testA关联testB快速访问testB然后迅速切回访问testA会导致testA失败

流控降级:

热点规则:配置完访问 http://127.0.0.1:8401/testC?p1=p1  当达到阈值抛出自定义的错误

        @GetMapping("/testC")
        @SentinelResource(value = "testC",blockHandler = "testC_fallbacl")
        public String testC(@RequestParam(value = "p1",required = false)String p1
                            ,@RequestParam(value = "p1",required = false)String p2) throws InterruptedException{
            System.out.println("-----");
            return "testC";
        }
        public String testC_fallbacl(String p1, String p2, BlockException exception){
            return "testC_fallbacl";
        }

热点规则参数例外项:

系统规则:全部拦截整个系统级别的

@SentinelResource注解

        @GetMapping("/testD")
        @SentinelResource(value = "testD",blockHandlerClass = MyBlockHandler.class
,blockHandler = "testD")
        public String testD(){
            return "testD";
        }

配置对应的blockHandlerClass 下面的形参BlockException 必须给,否则无法实现。

public class MyBlockHandler {
    public static String testD(BlockException b){
        return "MyBlockHandler----testD";
    }
}

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值