Hystrix服务限流,降级,熔断实战

LD is tigger forever,CG are not brothers forever, throw the pot and shine.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code,Keep progress,make a better result.

目录

概述

高并发场景下,防止一个服务出错造成链式的雪崩/服务有限订的返回时间,hystrix常用在客户端,服务端也可以使用。

架构特性

设计思路

实现思路分析

1.加入POM文件

org.springframework.cloud spring-cloud-starter-netflix-hystrix

2.yml:

spring:
  application:
    name: cloud-payment-service #服务名
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource  #当前数据源操作类型
    driver-class-name: com.mysql.cj.jdbc.Driver #数据库驱动包
    url: jdbc:mysql://localhost:3306/cloud?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    username: root
    password: "000000"
mybatis:
  type-aliases-package: com.cloud.util.entity
  mapper-locations: classpath:mapper/*.xml
  configuration:
    cache-enabled: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

eureka:
  client:
    #    表示将自己注册到eureka服务中心去
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: payment8001
    prefer-ip-address: true

3.main:

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

4.service服务,配置一个兜底的方法,当服务超时或者报错的话,都会进入到timeoutHandler方法中去
@HystrixCommand(fallbackMethod = “timeoutHandler”,commandProperties = {
@HystrixProperty(name=“execution.isolation.thread.timeoutInMilliseconds”,value=“3000”)
})
public String timeOut() throws InterruptedException {
Thread.sleep(5000);
return “test”;
}
public String timeoutHandler() throws InterruptedException {
return “fallback success”;
}

拓展实现

客户端:

yml:

server:
  port: 80 #服务端口

eureka:
  client:
    #    表示将自己注册到eureka服务中心去
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

//ribbon:

  //ReadTimeout: 6000

 // ConnectTimeout: 6000
logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.cloud.openFeign.service.paymentFeign: debug
feign:
  hystrix:
    enabled: true
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableHystrix
@EnableDiscoveryClient
public class mainhystrix80 {
    public static void main(String[] args) {
        SpringApplication.run(mainhystrix80.class,args);
    }
}

7.客户端的在这里的controller层中做规范,1.5s没有收到数据就调用降级方法,注意服务降级的方法和调用的方法返回类型要一致。

@RequestMapping("/consumer/hystrixTimeOutTest")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "timeoutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
    })
    public ResultObject timeOut() throws InterruptedException {
        return hystrixService.timeOut();

    }
    public ResultObject timeoutHandler() throws InterruptedException {
        String s = "你访问的页面超时请稍后再试";
        return ReturnResult.success(s);
    }
}

8.service接口用feign

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface hystrixService {

    @PostMapping("/timeOut")
    @ResponseBody
    public ResultObject timeOut() ;
}

相关工具如下:

15.hystrix图形化界面监控9001
16.pom

<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>

17.@EnableHystrixDashboard
18.访问http://localhost:9001/hystrix
19.输入监控的端口,比如要监控的端口是80端口,就输入:http://localhost:80/hystrix.stream,delay:2000,title:T3

注意,在监控的服务的主启动类上加上一下代码,不然会报连接失败:
然后监控的服务要实现负载均衡,在这里我用的是openFeign,而且调用的接口要实现服务降级,也就是有@HystrixCommand注解,不然会一直loading…

实验效果:(解决思路)

分析:

小结:

主要讲述了一些Hystrix服务限流,降级,熔断实战,里面有许多不足,请大家指正~

参考资料和推荐阅读

1.链接: 参考资料.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执于代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值