Hystrix简单使用-demo

一 搭建hystrix dashboard

新建module(Hystrix dashboard )

POM文件引入:

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

主启动类开启Hystrix dashboard

application.yml配置Hystrix dashboard微服务监听端口

server:
  port: 9001

 

启动Hystrix dashboard微服务,访问地址localhost:9001/hystrix

 

 

二 服务提供方集成hystrix

 

引入POM依赖


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

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

主启动类开启Hystrix注解

@EnableHystrix
@EnableCircuitBreaker

controller开启Hystrix服务降级、熔断

@RestController
@RequestMapping("/ConsumerHystrix")
public class ConsumerHystrixController {

    /**
     * @HystrixCommand报异常后如何处理:
     * 一旦调用服务方法失败并抛出了错误信息后,
     * 会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
     *
     */
    @HystrixCommand(fallbackMethod = "fallback_zs1",commandProperties = {
            //设置这个线程的超时时间是3s,3s内是正常的业务逻辑,超过3s调用fallbackMethod指定的方法进行处理
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    @GetMapping("/zs1")
    public String zs1(String name){

        String  test=  "ConsumerHystrix-"+"zs1-";
        System.out.println(test);
        return test+ name;
    }


    public String fallback_zs1(String name ){
        String fallBack="ConsumerHystrix-"+"fallback_zs1-";
        return fallBack+name;
    }


    //服务熔断
    @HystrixCommand(fallbackMethod = "fallback_zs2",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),   //是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "100"),  //请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),    //时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),    //失败率达到多少后跳闸
    })
    @GetMapping("/zs2")
    public String zs2(String name){
        String  test=  "ConsumerHystrix-"+"zs2-";
        return test+ name;
    }

    public String fallback_zs2(String name ){
        String fallBack="ConsumerHystrix-"+"fallback_zs2-";
        return fallBack+name;
    }
}

 

三 Hystrix dashboard观察微服务接口响应情况

启动微服务,dashboard 监控应用微服务地址:localhost:8084/hystrix.stream

点击Monitior Stream 进入dashboard界面

 

请求应用微服务后台接口

Hystrix dashboard监控信息:

 

四 使用jmemter压测工具对接口进行压测

 

对接口1进行压测:

添加线程组: 1000个请求=100并发线程*10次循环

添加Http Request,测试接口zs1

运行jmemter,压测接口,观察Hystrix dashboard监控情况:

100并发,1000次请求情况下:

 

 

50并发,1000次请求:

 

 

对接口2进行压测:

 

100并发,1000次请求:

1000并发,10000次请求:

 

5000并发,20000次请求:此时熔断器仍没有打开,此时应用直接将请求拒绝

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值