2.springcloud项目搭建笔记2涉及理论(hystrix组件使用)

1. 引入依赖

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

2. 启动类增加注解:@EnableCircuitBreaker

    注解越来越多可以用注解@SpringCloudApplication代替一些注解

3. Controller层控制:

    a. 在需要熔断的方法上加@HystrixCommand(fallbackMethod = "saveOrderFail")注解

    b. 新建一个saveOrderFail方法,入参和原方法一模一样

4. 内层方法调用控制:

    a. 新建一个类实现Feign的接口,这个方法里返回熔断信息

    b. Feign接口增加这个类名(@FeignClient(name="product-service",fallback = ProductClientFallBack.class))

    c. 配置文件开启Feign

feign:
  hystrix:
    enabled: true

5. 增加报警机制,可以吧预警信息放redis,加过期时间,过期时间内就不再发送

6. hystrix超时时间默认:1s

    配置文件设置

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 1000

7. Dashboard监控仪表盘使用

  a. 引入依赖

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

b. 启动类加注解@EnableHystrixDashboard

c. 配置文件增加

managment:
  endpoints:
    web:
      exposure:
        include: "*"

d. 访问:http://localhost:8781/hystrix

e. 在页面上输入网址:http://localhost:8781/actuator/hystrix.stream

f. 报错:Failed opening connection to http://localhost:8781/actuator/hystrix.stream : 404

  原因:访问路径默认是不开启的,增加c的配置,暴露全部监控信息即可

g. 当请求速率达到2/s时,circuit开关会打开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值