• 互联网架构服务降级熔断Hystrix学习


    Hystrix(豪猪,保护自己)

    熔断,熔断服务,为了防止整个系统故障,包含子和下游服务。

    降级,抛弃一些非核心的接口和数据。

    熔断和降级相互交集

    相同点:

    1)从可用性和可靠性出发,为了防止系统崩溃。

    2)最终让用户体会到的事某些功能暂时不能用

    不同点:

    1)服务熔断一般是下游服务故障导致的,而服务降级一般是从整体负荷考虑,由调用方控制

    Hystrix

    提供了熔断、隔离、fallback、cache、监控等功能。

    (一段时间内服务请求超时超过一定笔数,就会把该服务标记为不可用,其他请求进来则调用其他可用的服务。)

    实践实例

    1 加入依赖

     <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
     </dependency>

    Hystrix官网

    http://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#_circuit_breaker_hystrix_clients

    how to include Hystrix


    去阿里用仓库下载,修改maven仓库地址
    pom.xml中修改 

    <repositories>
    <repository>
    <id>nexus-aliyun</id>
    <name>Nexus aliyun</name>
    <layout>default</layout>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    <snapshots>
    <enabled>false</enabled>
    </snapshots>
    <releases>
    <enabled>true</enabled>
    </releases>
    </repository>
    </repositories>

    2 增加注解
    启动类里面增加注解
    @EnableCircuitBreaker

    注解越来越多-> 用SpringCloudApplication注解替代

    3、API接口编码实战
    熔断-》降级

    1)最外层api使用,好比异常处理(网络异常,参数或者内部调用问题)
    api方法上增加 @HystrixCommand(fallbackMethod = "saveOrderFail"),当api方法出错时,会调用fallbackMethod 方法

    编写fallback方法实现,方法签名一定要和api方法签名一致(注意点!!!)

     feign结合hystrix断路器开发

    feignClient 本身就集成了hystrix

    1)开启feign支持hystrix (注意,一定要开启,旧版本默认支持,新版本默认关闭)
    feign:
    hystrix:
    enabled: true

    2)FeignClient(name="xxx", fallback=xxx.class ), class需要继承当前FeignClient的类,productClientFallback继承ProductClient。

    下单接口

     

    获取商品异常

     

    熔断降级服务异常报警通知实战
    简介:完善服务熔断处理,报警机制完善

    1、加入redis依赖

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    2、配置redis链接信息

    redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    timeout: 2000

    3、使用

    //监控报警
    String saveOrderKye = "save-order";
    String sendValue = redisTemplate.opsForValue().get(saveOrderKye);
    final String ip = request.getRemoteAddr();
    new Thread( ()->{
    if (StringUtils.isBlank(sendValue)) {
    System.out.println("紧急短信,用户下单失败,请离开查找原因,ip地址是="+ip);
    //发送一个http请求,调用短信服务 TODO
    redisTemplate.opsForValue().set(saveOrderKye, "save-order-fail", 20, TimeUnit.SECONDS);
    }else{
    System.out.println("已经发送过短信,20秒内不重复发送");
    }
    }).start();

    断路器Dashboard监控仪表盘实战

    1、加入依赖

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    2、启动类增加注解
    @EnableHystrixDashboard


    3、配置文件增加endpoint

    management:
    endpoints:
    web:
    exposure:
    include: "*"

    4、访问入口
    http://localhost:8781/hystrix

    Hystrix Dashboard输入: http://localhost:8781/actuator/hystrix.stream


    参考资料
    默认开启监控配置
    https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-security-actuator

    配置文件类:
    spring-configuration-metadata.json


  • 相关阅读:
    sonic 移动端 集群测试【mac版】用于解决 Android、iOS、Windows、Mac、Web的云测方案
    Burp Suite 密码爆破
    mac mysql 忘记密码 重置
    Common Rust Lifetime Misconceptions 常见的对 Rust 生命周期的误解
    Rc<T>, Box<T>, RefCell<T>, Cell<T>
    Arenas in Rust
    Async Rust: What is a runtime? Here is how tokio works under the hood
    Rust Tokio Chat Server 用 Rust 写一个聊天服务器有多简单
    Rust 临时变量的生命周期
    js事件循环机制
  • 原文地址:https://www.cnblogs.com/dingpeng9055/p/12411179.html
Copyright © 2020-2023  润新知