hystrix入门

来源:互联网 发布:api接口文档 源码 编辑:程序博客网 时间:2024/06/18 10:33

1.Hystrix

hytrix通过命令模式,将服务的调用者和服务的提供者分开,为我们提供限流、降级、熔断等服务,在微服务系统依赖中是一个保持系统稳定的利器

2.实现方式

2.1 信号量

2.2 线程池

3.pom依赖

  <dependency>            <groupId>com.netflix.hystrix</groupId>            <artifactId>hystrix-core</artifactId>            <version>1.5.5</version>        </dependency>        <dependency>            <groupId>com.netflix.hystrix</groupId>            <artifactId>hystrix-metrics-event-stream</artifactId>            <version>1.5.5</version>        </dependency>


4.code

SayHelloCommand.java
/** * caicongyang.com Inc. * Copyright (c) 2004-2016 All Rights Reserved. */package com.caicongyang.hystrix;import com.netflix.hystrix.*;/** * @author caicongyang1 * @version id: SayHelloCommand, v 0.1 16/12/12 下午8:46 caicongyang1 Exp $$ */public class SayHelloCommand extends HystrixCommand<String> {    private Integer timeoutInMilliseconds;    private Integer coreSize;    private Integer maxConcurrentRequests;    protected SayHelloCommand(Setter setter) {        super(setter);    }    /**     * 采用线程池来控制服务依赖     *     * @param coreSize     * @param timeoutInMilliseconds     */    public SayHelloCommand(Integer coreSize, Integer timeoutInMilliseconds) {        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(SayHelloCommand.class.getName()))                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(SayHelloCommand.class.getName()))                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(coreSize))//服务线程池数量                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()                        .withExecutionTimeoutEnabled(true).withExecutionTimeoutInMilliseconds(timeoutInMilliseconds)//超时时间                        .withCircuitBreakerErrorThresholdPercentage(60)//熔断器关闭到打开阈值                        .withCircuitBreakerSleepWindowInMilliseconds(3000)));////熔断器打开到关闭的时间窗长度    }    /**     * 采用信号量来控制是否熔断     *     * @param Strategy     * @param maxConcurrentRequests     * @param timeoutInMilliseconds     */    public SayHelloCommand(HystrixCommandProperties.ExecutionIsolationStrategy Strategy, Integer maxConcurrentRequests, Integer timeoutInMilliseconds) {        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(SayHelloCommand.class.getName()))                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(SayHelloCommand.class.getName()))                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter())//服务线程池数量                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()                        .withExecutionIsolationStrategy(Strategy)//自定义策略                        .withExecutionIsolationSemaphoreMaxConcurrentRequests(maxConcurrentRequests) //信号量自定义最大并发数                        .withExecutionTimeoutEnabled(true)                        .withExecutionTimeoutInMilliseconds(timeoutInMilliseconds)//超时时间                        .withCircuitBreakerErrorThresholdPercentage(60)//熔断器关闭到打开阈值                        .withCircuitBreakerSleepWindowInMilliseconds(3000)));////熔断器打开到关闭的时间窗长度    }    public Integer getTimeoutInMilliseconds() {        return timeoutInMilliseconds;    }    public void setTimeoutInMilliseconds(Integer timeoutInMilliseconds) {        this.timeoutInMilliseconds = timeoutInMilliseconds;    }    public Integer getCoreSize() {        return coreSize;    }    public void setCoreSize(Integer coreSize) {        this.coreSize = coreSize;    }    public Integer getMaxConcurrentRequests() {        return maxConcurrentRequests;    }    public void setMaxConcurrentRequests(Integer maxConcurrentRequests) {        this.maxConcurrentRequests = maxConcurrentRequests;    }    @Override    protected String run() throws Exception {        Thread.sleep(1001L); //演示超时进入Fallback        return "hello";    }    @Override    protected String getFallback() {        return String.format("FallBack");    }    public static void main(String[] args) {//        SayHelloCommand command = new SayHelloCommand(20,30);//        String execute = command.execute();//        System.out.println(execute);        SayHelloCommand command = new SayHelloCommand(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE, 20, 30);        String execute = command.execute();        System.out.println(execute);    }}


5.学习链接:

https://segmentfault.com/a/1190000005988895


1 0