`
hot66hot
  • 浏览: 442400 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hystrix 使用与分析

阅读更多

 转载请注明出处哈:http://hot66hot.iteye.com/blog/2155036


一:为什么需要Hystrix?

在大中型分布式系统中,通常系统很多依赖(HTTP,hession,Netty,Dubbo等),如下图:

 

在高并发访问下,这些依赖的稳定性与否对系统的影响非常大,但是依赖有很多不可控问题:如网络连接缓慢,资源繁忙,暂时不可用,服务脱机等.

如下图:QPS为50的依赖 I 出现不可用,但是其他依赖仍然可用.

 

当依赖I 阻塞时,大多数服务器的线程池就出现阻塞(BLOCK),影响整个线上服务的稳定性.如下图:

 

在复杂的分布式架构的应用程序有很多的依赖,都会不可避免地在某些时候失败。高并发的依赖失败时如果没有隔离措施,当前应用服务就有被拖垮的风险。

 

例如:一个依赖30个SOA服务的系统,每个服务99.99%可用。
99.99%的30次方 ≈ 99.7%
0.3% 意味着一亿次请求 会有 3,000,00次失败
换算成时间大约每月有2个小时服务不稳定.
随着服务依赖数量的变多,服务不稳定的概率会成指数性提高.

 解决问题方案:对依赖做隔离,Hystrix就是处理依赖隔离的框架,同时也是可以帮我们做依赖服务的治理和监控.

 

Netflix 公司开发并成功使用Hystrix,使用规模如下:

 

The Netflix API processes 10+ billion HystrixCommand executions per day using thread isolation. 
Each API instance has 40+ thread-pools with 5-20 threads in each (most are set to 10).

二:Hystrix如何解决依赖隔离

1:Hystrix使用命令模式HystrixCommand(Command)包装依赖调用逻辑,每个命令在单独线程中/信号授权下执行。

2:可配置依赖调用超时时间,超时时间一般设为比99.5%平均时间略高即可.当调用超时时,直接返回或执行fallback逻辑。

3:为每个依赖提供一个小的线程池(或信号),如果线程池已满调用将被立即拒绝,默认不采用排队.加速失败判定时间。

4:依赖调用结果分:成功,失败(抛出异常),超时,线程拒绝,短路。 请求失败(异常,拒绝,超时,短路)时执行fallback(降级)逻辑。

5:提供熔断器组件,可以自动运行或手动调用,停止当前依赖一段时间(10秒),熔断器默认错误率阈值为50%,超过将自动运行。

6:提供近实时依赖的统计和监控

Hystrix依赖的隔离架构,如下图:

三:如何使用Hystrix

1:使用maven引入Hystrix依赖

 

<!-- 依赖版本 -->
<hystrix.version>1.3.16</hystrix.version>
<hystrix-metrics-event-stream.version>1.1.2</hystrix-metrics-event-stream.version> 
 
<dependency>
     <groupId>com.netflix.hystrix</groupId>
     <artifactId>hystrix-core</artifactId>
     <version>${hystrix.version}</version>
 </dependency>
     <dependency>
     <groupId>com.netflix.hystrix</groupId>
     <artifactId>hystrix-metrics-event-stream</artifactId>
     <version>${hystrix-metrics-event-stream.version}</version>
 </dependency>
<!-- 仓库地址 -->
<repository>
     <id>nexus</id>
     <name>local private nexus</name>
     <url>http://maven.oschina.net/content/groups/public/</url>
     <releases>
          <enabled>true</enabled>
     </releases>
     <snapshots>
          <enabled>false</enabled>
     </snapshots>
</repository>

2:使用命令模式封装依赖逻辑

 

public class HelloWorldCommand extends HystrixCommand<String> {
    private final String name;
    public HelloWorldCommand(String name) {
        //最少配置:指定命令组名(CommandGroup)
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.name = name;
    }
    @Override
    protected String run() {
        // 依赖逻辑封装在run()方法中
        return "Hello " + name +" thread:" + Thread.currentThread().getName();
    }
    //调用实例
    public static void main(String[] args) throws Exception{
        //每个Command对象只能调用一次,不可以重复调用,
        //重复调用对应异常信息:This instance can only be executed once. Please instantiate a new instance.
        HelloWorldCommand helloWorldCommand = new HelloWorldCommand("Synchronous-hystrix");
        //使用execute()同步调用代码,效果等同于:helloWorldCommand.queue().get(); 
        String result = helloWorldCommand.execute();
        System.out.println("result=" + result);
 
        helloWorldCommand = new HelloWorldCommand("Asynchronous-hystrix");
        //异步调用,可自由控制获取结果时机,
        Future<String> future = helloWorldCommand.queue();
        //get操作不能超过command定义的超时时间,默认:1秒
        result = future.get(100, TimeUnit.MILLISECONDS);
        System.out.println("result=" + result);
        System.out.println("mainThread=" + Thread.currentThread().getName());
    }
     
}
    //运行结果: run()方法在不同的线程下执行
    // result=Hello Synchronous-hystrix thread:hystrix-HelloWorldGroup-1
    // result=Hello Asynchronous-hystrix thread:hystrix-HelloWorldGroup-2
    // mainThread=main

 note:异步调用使用 command.queue()get(timeout, TimeUnit.MILLISECONDS);同步调用使用command.execute() 等同于 command.queue().get();

3:注册异步事件回调执行

 

//注册观察者事件拦截
Observable<String> fs = new HelloWorldCommand("World").observe();
//注册结果回调事件
fs.subscribe(new Action1<String>() {
    @Override
    public void call(String result) {
         //执行结果处理,result 为HelloWorldCommand返回的结果
        //用户对结果做二次处理.
    }
});
//注册完整执行生命周期事件
fs.subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {
                // onNext/onError完成之后最后回调
                System.out.println("execute onCompleted");
            }
            @Override
            public void onError(Throwable e) {
                // 当产生异常时回调
                System.out.println("onError " + e.getMessage());
                e.printStackTrace();
            }
            @Override
            public void onNext(String v) {
                // 获取结果后回调
                System.out.println("onNext: " + v);
            }
        });
/* 运行结果
call execute result=Hello observe-hystrix thread:hystrix-HelloWorldGroup-3
onNext: Hello observe-hystrix thread:hystrix-HelloWorldGroup-3
execute onCompleted
*/

4:使用Fallback() 提供降级策略

 

//重载HystrixCommand 的getFallback方法实现逻辑
public class HelloWorldCommand extends HystrixCommand<String> {
    private final String name;
    public HelloWorldCommand(String name) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HelloWorldGroup"))
                /* 配置依赖超时时间,500毫秒*/
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(500)));
        this.name = name;
    }
    @Override
    protected String getFallback() {
        return "exeucute Falled";
    }
    @Override
    protected String run() throws Exception {
        //sleep 1 秒,调用会超时
        TimeUnit.MILLISECONDS.sleep(1000);
        return "Hello " + name +" thread:" + Thread.currentThread().getName();
    }
    public static void main(String[] args) throws Exception{
        HelloWorldCommand command = new HelloWorldCommand("test-Fallback");
        String result = command.execute();
    }
}
/* 运行结果:getFallback() 调用运行
getFallback executed
*/

 

NOTE: 除了HystrixBadRequestException异常之外,所有从run()方法抛出的异常都算作失败,并触发降级getFallback()和断路器逻辑。

          HystrixBadRequestException用在非法参数或非系统故障异常等不应触发回退逻辑的场景。

5:依赖命名:CommandKey

 

public HelloWorldCommand(String name) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                /* HystrixCommandKey工厂定义依赖名称 */
                .andCommandKey(HystrixCommandKey.Factory.asKey("HelloWorld")));
        this.name = name;
    }

 NOTE: 每个CommandKey代表一个依赖抽象,相同的依赖要使用相同的CommandKey名称。依赖隔离的根本就是对相同CommandKey的依赖做隔离.

6:依赖分组:CommandGroup

命令分组用于对依赖操作分组,便于统计,汇总等.

//使用HystrixCommandGroupKey工厂定义
public HelloWorldCommand(String name) {
    Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HelloWorldGroup"))
}

 NOTE: CommandGroup是每个命令最少配置的必选参数,在不指定ThreadPoolKey的情况下,字面值用于对不同依赖的线程池/信号区分.

7:线程池/信号:ThreadPoolKey

public HelloWorldCommand(String name) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("HelloWorld"))
                /* 使用HystrixThreadPoolKey工厂定义线程池名称*/
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("HelloWorldPool")));
        this.name = name;
    }

 NOTE: 当对同一业务依赖做隔离时使用CommandGroup做区分,但是对同一依赖的不同远程调用如(一个是redis 一个是http),可以使用HystrixThreadPoolKey做隔离区分.

           最然在业务上都是相同的组,但是需要在资源上做隔离时,可以使用HystrixThreadPoolKey区分.

8:请求缓存 Request-Cache

public class RequestCacheCommand extends HystrixCommand<String> {
    private final int id;
    public RequestCacheCommand( int id) {
        super(HystrixCommandGroupKey.Factory.asKey("RequestCacheCommand"));
        this.id = id;
    }
    @Override
    protected String run() throws Exception {
        System.out.println(Thread.currentThread().getName() + " execute id=" + id);
        return "executed=" + id;
    }
    //重写getCacheKey方法,实现区分不同请求的逻辑
    @Override
    protected String getCacheKey() {
        return String.valueOf(id);
    }
 
    public static void main(String[] args){
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            RequestCacheCommand command2a = new RequestCacheCommand(2);
            RequestCacheCommand command2b = new RequestCacheCommand(2);
            Assert.assertTrue(command2a.execute());
            //isResponseFromCache判定是否是在缓存中获取结果
            Assert.assertFalse(command2a.isResponseFromCache());
            Assert.assertTrue(command2b.execute());
            Assert.assertTrue(command2b.isResponseFromCache());
        } finally {
            context.shutdown();
        }
        context = HystrixRequestContext.initializeContext();
        try {
            RequestCacheCommand command3b = new RequestCacheCommand(2);
            Assert.assertTrue(command3b.execute());
            Assert.assertFalse(command3b.isResponseFromCache());
        } finally {
            context.shutdown();
        }
    }
}

 NOTE:请求缓存可以让(CommandKey/CommandGroup)相同的情况下,直接共享结果,降低依赖调用次数,在高并发和CacheKey碰撞率高场景下可以提升性能.

Servlet容器中,可以直接实用Filter机制Hystrix请求上下文

public class HystrixRequestContextServletFilter implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
     throws IOException, ServletException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            chain.doFilter(request, response);
        } finally {
            context.shutdown();
        }
    }
}
<filter>
      <display-name>HystrixRequestContextServletFilter</display-name>
      <filter-name>HystrixRequestContextServletFilter</filter-name>
      <filter-class>com.netflix.hystrix.contrib.requestservlet.HystrixRequestContextServletFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>HystrixRequestContextServletFilter</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

9:信号量隔离:SEMAPHORE

  隔离本地代码或可快速返回远程调用(如memcached,redis)可以直接使用信号量隔离,降低线程隔离开销.

public class HelloWorldCommand extends HystrixCommand<String> {
    private final String name;
    public HelloWorldCommand(String name) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HelloWorldGroup"))
                /* 配置信号量隔离方式,默认采用线程池隔离 */
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)));
        this.name = name;
    }
    @Override
    protected String run() throws Exception {
        return "HystrixThread:" + Thread.currentThread().getName();
    }
    public static void main(String[] args) throws Exception{
        HelloWorldCommand command = new HelloWorldCommand("semaphore");
        String result = command.execute();
        System.out.println(result);
        System.out.println("MainThread:" + Thread.currentThread().getName());
    }
}
/** 运行结果
 HystrixThread:main
 MainThread:main
*/

10:fallback降级逻辑命令嵌套

 

  适用场景:用于fallback逻辑涉及网络访问的情况,如缓存访问。

 

public class CommandWithFallbackViaNetwork extends HystrixCommand<String> {
    private final int id;
 
    protected CommandWithFallbackViaNetwork(int id) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceX"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("GetValueCommand")));
        this.id = id;
    }
 
    @Override
    protected String run() {
        // RemoteService.getValue(id);
        throw new RuntimeException("force failure for example");
    }
 
    @Override
    protected String getFallback() {
        return new FallbackViaNetwork(id).execute();
    }
 
    private static class FallbackViaNetwork extends HystrixCommand<String> {
        private final int id;
        public FallbackViaNetwork(int id) {
            super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceX"))
                    .andCommandKey(HystrixCommandKey.Factory.asKey("GetValueFallbackCommand"))
                    // 使用不同的线程池做隔离,防止上层线程池跑满,影响降级逻辑.
                    .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("RemoteServiceXFallback")));
            this.id = id;
        }
        @Override
        protected String run() {
            MemCacheClient.getValue(id);
        }
 
        @Override
        protected String getFallback() {
            return null;
        }
    }
}

 NOTE:依赖调用和降级调用使用不同的线程池做隔离,防止上层线程池跑满,影响二级降级逻辑调用.

 11:显示调用fallback逻辑,用于特殊业务处理

 

public class CommandFacadeWithPrimarySecondary extends HystrixCommand<String> {
    private final static DynamicBooleanProperty usePrimary = DynamicPropertyFactory.getInstance().getBooleanProperty("primarySecondary.usePrimary", true);
    private final int id;
    public CommandFacadeWithPrimarySecondary(int id) {
        super(Setter
                .withGroupKey(HystrixCommandGroupKey.Factory.asKey("SystemX"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("PrimarySecondaryCommand"))
                .andCommandPropertiesDefaults(
                        HystrixCommandProperties.Setter()
                                .withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE)));
        this.id = id;
    }
    @Override
    protected String run() {
        if (usePrimary.get()) {
            return new PrimaryCommand(id).execute();
        } else {
            return new SecondaryCommand(id).execute();
        }
    }
    @Override
    protected String getFallback() {
        return "static-fallback-" + id;
    }
    @Override
    protected String getCacheKey() {
        return String.valueOf(id);
    }
    private static class PrimaryCommand extends HystrixCommand<String> {
        private final int id;
        private PrimaryCommand(int id) {
            super(Setter
                    .withGroupKey(HystrixCommandGroupKey.Factory.asKey("SystemX"))
                    .andCommandKey(HystrixCommandKey.Factory.asKey("PrimaryCommand"))
                    .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("PrimaryCommand"))
                    .andCommandPropertiesDefaults(
                            // we default to a 600ms timeout for primary
                            HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(600)));
            this.id = id;
        }
        @Override
        protected String run() {
            // perform expensive 'primary' service call
            return "responseFromPrimary-" + id;
        }
    }
    private static class SecondaryCommand extends HystrixCommand<String> {
        private final int id;
        private SecondaryCommand(int id) {
            super(Setter
                    .withGroupKey(HystrixCommandGroupKey.Factory.asKey("SystemX"))
                    .andCommandKey(HystrixCommandKey.Factory.asKey("SecondaryCommand"))
                    .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("SecondaryCommand"))
                    .andCommandPropertiesDefaults(
                            // we default to a 100ms timeout for secondary
                            HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(100)));
            this.id = id;
        }
        @Override
        protected String run() {
            // perform fast 'secondary' service call
            return "responseFromSecondary-" + id;
        }
    }
    public static class UnitTest {
        @Test
        public void testPrimary() {
            HystrixRequestContext context = HystrixRequestContext.initializeContext();
            try {
                ConfigurationManager.getConfigInstance().setProperty("primarySecondary.usePrimary", true);
                assertEquals("responseFromPrimary-20", new CommandFacadeWithPrimarySecondary(20).execute());
            } finally {
                context.shutdown();
                ConfigurationManager.getConfigInstance().clear();
            }
        }
        @Test
        public void testSecondary() {
            HystrixRequestContext context = HystrixRequestContext.initializeContext();
            try {
                ConfigurationManager.getConfigInstance().setProperty("primarySecondary.usePrimary", false);
                assertEquals("responseFromSecondary-20", new CommandFacadeWithPrimarySecondary(20).execute());
            } finally {
                context.shutdown();
                ConfigurationManager.getConfigInstance().clear();
            }
        }
    }
}

 NOTE:显示调用降级适用于特殊需求的场景,fallback用于业务处理,fallback不再承担降级职责,建议慎重使用,会造成监控统计换乱等问题.

12:命令调用合并:HystrixCollapser

命令调用合并允许多个请求合并到一个线程/信号下批量执行。

执行流程图如下:

public class CommandCollapserGetValueForKey extends HystrixCollapser<List<String>, String, Integer> {
    private final Integer key;
    public CommandCollapserGetValueForKey(Integer key) {
        this.key = key;
    }
    @Override
    public Integer getRequestArgument() {
        return key;
    }
    @Override
    protected HystrixCommand<List<String>> createCommand(final Collection<CollapsedRequest<String, Integer>> requests) {
        //创建返回command对象
        return new BatchCommand(requests);
    }
    @Override
    protected void mapResponseToRequests(List<String> batchResponse, Collection<CollapsedRequest<String, Integer>> requests) {
        int count = 0;
        for (CollapsedRequest<String, Integer> request : requests) {
            //手动匹配请求和响应
            request.setResponse(batchResponse.get(count++));
        }
    }
    private static final class BatchCommand extends HystrixCommand<List<String>> {
        private final Collection<CollapsedRequest<String, Integer>> requests;
        private BatchCommand(Collection<CollapsedRequest<String, Integer>> requests) {
                super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                    .andCommandKey(HystrixCommandKey.Factory.asKey("GetValueForKey")));
            this.requests = requests;
        }
        @Override
        protected List<String> run() {
            ArrayList<String> response = new ArrayList<String>();
            for (CollapsedRequest<String, Integer> request : requests) {
                response.add("ValueForKey: " + request.getArgument());
            }
            return response;
        }
    }
    public static class UnitTest {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            Future<String> f1 = new CommandCollapserGetValueForKey(1).queue();
            Future<String> f2 = new CommandCollapserGetValueForKey(2).queue();
            Future<String> f3 = new CommandCollapserGetValueForKey(3).queue();
            Future<String> f4 = new CommandCollapserGetValueForKey(4).queue();
            assertEquals("ValueForKey: 1", f1.get());
            assertEquals("ValueForKey: 2", f2.get());
            assertEquals("ValueForKey: 3", f3.get());
            assertEquals("ValueForKey: 4", f4.get());
            assertEquals(1, HystrixRequestLog.getCurrentRequest().getExecutedCommands().size());
            HystrixCommand<?> command = HystrixRequestLog.getCurrentRequest().getExecutedCommands().toArray(new HystrixCommand<?>[1])[0];
            assertEquals("GetValueForKey", command.getCommandKey().name());
            assertTrue(command.getExecutionEvents().contains(HystrixEventType.COLLAPSED));
            assertTrue(command.getExecutionEvents().contains(HystrixEventType.SUCCESS));
        } finally {
         context.shutdown();
        }   
    }
}

 NOTE:使用场景:HystrixCollapser用于对多个相同业务的请求合并到一个线程甚至可以合并到一个连接中执行,降低线程交互次和IO数,但必须保证他们属于同一依赖.

四:监控平台搭建Hystrix-dashboard

1:监控dashboard介绍

dashboard面板可以对依赖关键指标提供实时监控,如下图:

 

2:实例暴露command统计数据

Hystrix使用Servlet对当前JVM下所有command调用情况作数据流输出

配置如下:

 

<servlet>
    <display-name>HystrixMetricsStreamServlet</display-name>
    <servlet-name>HystrixMetricsStreamServlet</servlet-name>
    <servlet-class>com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HystrixMetricsStreamServlet</servlet-name>
    <url-pattern>/hystrix.stream</url-pattern>
</servlet-mapping>
<!-- 
    对应URL格式 : http://hostname:port/application/hystrix.stream
-->

 

3:集群模式监控统计搭建

1)使用Turbine组件做集群数据汇总

结构图如下;

2)内嵌jetty提供Servlet容器,暴露HystrixMetrics

public class JettyServer {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private int port;
    private ExecutorService executorService = Executors.newFixedThreadPool(1);
    private Server server = null;
    public void init() {
        try {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        //绑定8080端口,加载HystrixMetricsStreamServlet并映射url
                        server = new Server(8080);
                        WebAppContext context = new WebAppContext();
                        context.setContextPath("/");
                        context.addServlet(HystrixMetricsStreamServlet.class, "/hystrix.stream");
                        context.setResourceBase(".");
                        server.setHandler(context);
                        server.start();
                        server.join();
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }
                }
            });
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    public void destory() {
        if (server != null) {
            try {
                server.stop();
                server.destroy();
                logger.warn("jettyServer stop and destroy!");
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}

 

3)Turbine搭建和配置

  a:配置Turbine Servlet收集器

<servlet>
   <description></description>
   <display-name>TurbineStreamServlet</display-name>
   <servlet-name>TurbineStreamServlet</servlet-name>
   <servlet-class>com.netflix.turbine.streaming.servlet.TurbineStreamServlet</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>TurbineStreamServlet</servlet-name>
   <url-pattern>/turbine.stream</url-pattern>
 </servlet-mapping>

   b:编写config.properties配置集群实例

#配置两个集群:mobil-online,ugc-online
turbine.aggregator.clusterConfig=mobil-online,ugc-online
#配置mobil-online集群实例
turbine.ConfigPropertyBasedDiscovery.mobil-online.instances=10.10.*.*,10.10.*.*,10.10.*.*,10.10.*.*,10.10.*.*,10.10.*.*,10.16.*.*,10.16.*.*,10.16.*.*,10.16.*.*
#配置mobil-online数据流servlet
turbine.instanceUrlSuffix.mobil-online=:8080/hystrix.stream
#配置ugc-online集群实例
turbine.ConfigPropertyBasedDiscovery.ugc-online.instances=10.10.*.*,10.10.*.*,10.10.*.*,10.10.*.*#配置ugc-online数据流servlet
turbine.instanceUrlSuffix.ugc-online=:8080/hystrix.stream

 

  c:使用Dashboard配置连接Turbine

  如下图 :

 

五:Hystrix配置与分析

1:Hystrix 配置

1):Command 配置

Command配置源码在HystrixCommandProperties,构造Command时通过Setter进行配置

具体配置解释和默认值如下

//使用命令调用隔离方式,默认:采用线程隔离,ExecutionIsolationStrategy.THREAD
private final HystrixProperty<ExecutionIsolationStrategy> executionIsolationStrategy; 
//使用线程隔离时,调用超时时间,默认:1秒
private final HystrixProperty<Integer> executionIsolationThreadTimeoutInMilliseconds; 
//线程池的key,用于决定命令在哪个线程池执行
private final HystrixProperty<String> executionIsolationThreadPoolKeyOverride; 
//使用信号量隔离时,命令调用最大的并发数,默认:10
private final HystrixProperty<Integer> executionIsolationSemaphoreMaxConcurrentRequests;
//使用信号量隔离时,命令fallback(降级)调用最大的并发数,默认:10
private final HystrixProperty<Integer> fallbackIsolationSemaphoreMaxConcurrentRequests; 
//是否开启fallback降级策略 默认:true 
private final HystrixProperty<Boolean> fallbackEnabled; 
// 使用线程隔离时,是否对命令执行超时的线程调用中断(Thread.interrupt())操作.默认:true
private final HystrixProperty<Boolean> executionIsolationThreadInterruptOnTimeout; 
// 统计滚动的时间窗口,默认:5000毫秒circuitBreakerSleepWindowInMilliseconds
private final HystrixProperty<Integer> metricsRollingStatisticalWindowInMilliseconds;
// 统计窗口的Buckets的数量,默认:10个,每秒一个Buckets统计
private final HystrixProperty<Integer> metricsRollingStatisticalWindowBuckets; // number of buckets in the statisticalWindow
//是否开启监控统计功能,默认:true
private final HystrixProperty<Boolean> metricsRollingPercentileEnabled; 
// 是否开启请求日志,默认:true
private final HystrixProperty<Boolean> requestLogEnabled; 
//是否开启请求缓存,默认:true
private final HystrixProperty<Boolean> requestCacheEnabled; // Whether request caching is enabled.

 

2):熔断器(Circuit Breaker)配置

Circuit Breaker配置源码在HystrixCommandProperties,构造Command时通过Setter进行配置,每种依赖使用一个Circuit Breaker

// 熔断器在整个统计时间内是否开启的阀值,默认20秒。也就是10秒钟内至少请求20次,熔断器才发挥起作用
private final HystrixProperty<Integer> circuitBreakerRequestVolumeThreshold; 
//熔断器默认工作时间,默认:5秒.熔断器中断请求5秒后会进入半打开状态,放部分流量过去重试
private final HystrixProperty<Integer> circuitBreakerSleepWindowInMilliseconds; 
//是否启用熔断器,默认true. 启动
private final HystrixProperty<Boolean> circuitBreakerEnabled; 
//默认:50%。当出错率超过50%后熔断器启动.
private final HystrixProperty<Integer> circuitBreakerErrorThresholdPercentage;
//是否强制开启熔断器阻断所有请求,默认:false,不开启
private final HystrixProperty<Boolean> circuitBreakerForceOpen; 
//是否允许熔断器忽略错误,默认false, 不开启
private final HystrixProperty<Boolean> circuitBreakerForceClosed;

 

3):命令合并(Collapser)配置

Command配置源码在HystrixCollapserProperties,构造Collapser时通过Setter进行配置

//请求合并是允许的最大请求数,默认: Integer.MAX_VALUE
private final HystrixProperty<Integer> maxRequestsInBatch;
//批处理过程中每个命令延迟的时间,默认:10毫秒
private final HystrixProperty<Integer> timerDelayInMilliseconds;
//批处理过程中是否开启请求缓存,默认:开启
private final HystrixProperty<Boolean> requestCacheEnabled;

 

4):线程池(ThreadPool)配置

/**
配置线程池大小,默认值10个.
建议值:请求高峰时99.5%的平均响应时间 + 向上预留一些即可
*/
HystrixThreadPoolProperties.Setter().withCoreSize(int value)
/**
配置线程值等待队列长度,默认值:-1
建议值:-1表示不等待直接拒绝,测试表明线程池使用直接决绝策略+ 合适大小的非回缩线程池效率最高.所以不建议修改此值。
当使用非回缩线程池时,queueSizeRejectionThreshold,keepAliveTimeMinutes 参数无效
*/
HystrixThreadPoolProperties.Setter().withMaxQueueSize(int value)

2:Hystrix关键组件分析

 1):Hystrix流程结构解析

流程说明:

1:每次调用创建一个新的HystrixCommand,把依赖调用封装在run()方法中.

2:执行execute()/queue做同步或异步调用.

3:判断熔断器(circuit-breaker)是否打开,如果打开跳到步骤8,进行降级策略,如果关闭进入步骤.

4:判断线程池/队列/信号量是否跑满,如果跑满进入降级步骤8,否则继续后续步骤.

5:调用HystrixCommand的run方法.运行依赖逻辑

5a:依赖逻辑调用超时,进入步骤8.

6:判断逻辑是否调用成功

6a:返回成功调用结果

6b:调用出错,进入步骤8.

7:计算熔断器状态,所有的运行状态(成功, 失败, 拒绝,超时)上报给熔断器,用于统计从而判断熔断器状态.

8:getFallback()降级逻辑.

  以下四种情况将触发getFallback调用:

 (1):run()方法抛出非HystrixBadRequestException异常。

 (2):run()方法调用超时

 (3):熔断器开启拦截调用

 (4):线程池/队列/信号量是否跑满

8a:没有实现getFallback的Command将直接抛出异常

8b:fallback降级逻辑调用成功直接返回

8c:降级逻辑调用失败抛出异常

9:返回执行成功结果

2):熔断器:Circuit Breaker 

Circuit Breaker 流程架构和统计

每个熔断器默认维护10个bucket,每秒一个bucket,每个blucket记录成功,失败,超时,拒绝的状态,

默认错误超过50%且10秒内超过20个请求进行中断拦截. 

3)隔离(Isolation)分析

Hystrix隔离方式采用线程/信号的方式,通过隔离限制依赖的并发量和阻塞扩散.

(1):线程隔离

         把执行依赖代码的线程与请求线程(如:jetty线程)分离,请求线程可以自由控制离开的时间(异步过程)。

   通过线程池大小可以控制并发量,当线程池饱和时可以提前拒绝服务,防止依赖问题扩散。

   线上建议线程池不要设置过大,否则大量堵塞线程有可能会拖慢服务器。

(2):线程隔离的优缺点

线程隔离的优点:

[1]:使用线程可以完全隔离第三方代码,请求线程可以快速放回。

[2]:当一个失败的依赖再次变成可用时,线程池将清理,并立即恢复可用,而不是一个长时间的恢复。

[3]:可以完全模拟异步调用,方便异步编程。

线程隔离的缺点:

[1]:线程池的主要缺点是它增加了cpu,因为每个命令的执行涉及到排队(默认使用SynchronousQueue避免排队),调度和上下文切换。

[2]:对使用ThreadLocal等依赖线程状态的代码增加复杂性,需要手动传递和清理线程状态。

NOTE: Netflix公司内部认为线程隔离开销足够小,不会造成重大的成本或性能的影响。

Netflix 内部API 每天100亿的HystrixCommand依赖请求使用线程隔,每个应用大约40多个线程池,每个线程池大约5-20个线程。

(3):信号隔离

      信号隔离也可以用于限制并发访问,防止阻塞扩散, 与线程隔离最大不同在于执行依赖代码的线程依然是请求线程(该线程需要通过信号申请),

   如果客户端是可信的且可以快速返回,可以使用信号隔离替换线程隔离,降低开销.

   

线程隔离与信号隔离区别如下图:

 解析图片出自官网wiki , 更多内容请见官网: https://github.com/Netflix/Hystrix

  • 大小: 66.2 KB
  • 大小: 65.3 KB
  • 大小: 98 KB
  • 大小: 52.5 KB
  • 大小: 161.8 KB
  • 大小: 20 KB
  • 大小: 30.5 KB
  • 大小: 57.3 KB
  • 大小: 73.1 KB
  • 大小: 42 KB
  • 大小: 224.9 KB
  • 大小: 51.1 KB
  • 大小: 103.9 KB
  • 大小: 110.4 KB
  • 大小: 131.9 KB
  • 大小: 178.7 KB
22
2
分享到:
评论
22 楼 FTest 2017-11-30  
hystrix中request请求清不掉是什么问题啊

HystrixRequestCache.getInstance(GETTER_KEY, HystrixConcurrencyStrategyDefault.getInstance()).clear(key);
这个清不掉
21 楼 l0m0a0 2017-05-16  
note:异步调用使用 command.queue().get(timeout, TimeUnit.MILLISECONDS);同步调用使用command.execute() 等同于 command.queue().get();

请问这两个不都是异步调用吗?只不过get()没有超时控制,而get(timeout, TimeUnit.MILLISECONDS)多了超时控制而已。
20 楼 welcomezhang 2017-04-21  
好东西,大家讨论的很多哈
19 楼 xiaolei1004 2017-01-12  
虽然看不太懂,收藏慢慢研究
18 楼 ssbpls 2016-11-23  
你好,请问你用什么软件能画出这么漂亮的图的
17 楼 mafly008 2016-08-18  
---------------------------------------------------------------
#配置两个集群:mobil-online,ugc-online 
turbine.aggregator.clusterConfig=mobil-online,ugc-online 
#配置mobil-online集群实例 
turbine.ConfigPropertyBasedDiscovery.mobil-online.instances=10.10.*.*,10.10.*.*,10.10.*.*,10.10.*.*,10.10.*.*,10.10.*.*,10.16.*.*,10.16.*.*,10.16.*.*,10.16.*.* 
#配置mobil-online数据流servlet 
turbine.instanceUrlSuffix.mobil-online=:8080/hystrix.stream 
#配置ugc-online集群实例 
turbine.ConfigPropertyBasedDiscovery.ugc-online.instances=10.10.*.*,10.10.*.*,10.10.*.*,10.10.*.*#配置ugc-online数据流servlet 
turbine.instanceUrlSuffix.ugc-online=:8080/hystrix.stream
---------------------------------------------------------------
turbine监控配置这块有两个问题请教下:
1.turbine可以同时监控两个应用的集群吗?我按照上面配置好像只能监控到第一个应用的集群,第二个不起作用。官方的wiki上好像不是这种写法
2.turbine.instanceUrlSuffix.ugc-online=:8080/hystrix.stream 这样好像不行吧,8080和hystrix.stream还需要加上应用的路径吧。
16 楼 狂盗一枝梅 2016-08-02  
这么多,感觉很大的工作量呢,而且几乎都看不懂
15 楼 hot66hot 2016-03-01  
sunquest 写道
hi.你好。
我准备整合trubine,但是出了一点问题。

我监控的是
  ——172.28.186.251:9090/hystrix.stream
  ——172.28.186.251:9091/hystrix.stream
  ——172.28.186.251:9092/hystrix.stream
三台机器。

在trubine中也配了。
配置如下
turbine.aggregator.clusterConfig=hyDemo

turbine.ConfigPropertyBasedDiscovery.hyDemo.instances=172.28.186.251:9090,172.28.186.251:9091,172.28.186.251:9092 

turbine.instanceUrlSuffix.hyDemo=/hystrix.stream  


使用的时候,监控页面一直是:ping。好像没有通的样子。
但是我用172.28.186.251:9090/hystrix.stream的时候,出现一个ping之后出现 data:XXXXXX的数据出来。

配置文件也修改成最简单的
turbine.aggregator.clusterConfig=hyDemo

turbine.ConfigPropertyBasedDiscovery.hyDemo.instances=172.28.186.251

turbine.instanceUrlSuffix.hyDemo=:9090/hystrix.stream  

也同样的效果。

是不是那里需要注意的?还望指导一下,谢谢。


为什么用外网IP呢? 首先确认你每个节点的hystrix.stream 是否能输出数据,如果可以,看些turbine的webServer的日志,有没有报错。
14 楼 sunquest 2016-02-24  
hi.你好。
我准备整合trubine,但是出了一点问题。

我监控的是
  ——172.28.186.251:9090/hystrix.stream
  ——172.28.186.251:9091/hystrix.stream
  ——172.28.186.251:9092/hystrix.stream
三台机器。

在trubine中也配了。
配置如下
turbine.aggregator.clusterConfig=hyDemo

turbine.ConfigPropertyBasedDiscovery.hyDemo.instances=172.28.186.251:9090,172.28.186.251:9091,172.28.186.251:9092 

turbine.instanceUrlSuffix.hyDemo=/hystrix.stream  


使用的时候,监控页面一直是:ping。好像没有通的样子。
但是我用172.28.186.251:9090/hystrix.stream的时候,出现一个ping之后出现 data:XXXXXX的数据出来。

配置文件也修改成最简单的
turbine.aggregator.clusterConfig=hyDemo

turbine.ConfigPropertyBasedDiscovery.hyDemo.instances=172.28.186.251

turbine.instanceUrlSuffix.hyDemo=:9090/hystrix.stream  

也同样的效果。

是不是那里需要注意的?还望指导一下,谢谢。
13 楼 hot66hot 2016-01-30  
kazaff 写道
rpc框架自身就会提供底层通信的模型,也有比较完善的容错机制。为何还要使用Hystrix呢?

容错是个容易被忽略的点,特别是在依赖不稳定的情况下,超时等普通机制无法迅速做到隔离,我还没遇到RPC 框架提供像Hystrix这样完善的容错机制。如果有,请告诉我一下哈。
12 楼 kazaff 2016-01-17  
rpc框架自身就会提供底层通信的模型,也有比较完善的容错机制。为何还要使用Hystrix呢?
11 楼 hot66hot 2015-12-30  
bigtian 写道
发现2个问题:
1. 线程池的大小也是可以修改的
2. request cache貌似也解决不了高并发的问题,好像request context是thread local的。

感谢提醒哈。
10 楼 bigtian 2015-12-28  
发现2个问题:
1. 线程池的大小也是可以修改的
2. request cache貌似也解决不了高并发的问题,好像request context是thread local的。
9 楼 hot66hot 2015-11-06  
krystal_zyy 写道
dashboard展示的数据是从Hystrix.stream文件中实时读出来的,那么我想问的问题是hystrix.stream里面的数据怎么写进去的?

看源码吧,内部维护了一个基于内存的统计。
8 楼 krystal_zyy 2015-11-06  
dashboard展示的数据是从Hystrix.stream文件中实时读出来的,那么我想问的问题是hystrix.stream里面的数据怎么写进去的?
7 楼 ravollen 2015-04-16  
hot66hot 写道
ravollen 写道
hot66hot 写道
ravollen 写道
hot66hot 写道
ravollen 写道
hi,你好,问一下,你这图上的这个turbine可针对单台机器的监控是hystrix dashbroad自带的功能还是自己开发的呢?

是Netflix公司开发的Turbine,见:https://github.com/Netflix/Turbine/wiki

嗯,我现在也是用的trubine,但是没有你这图上的上面那一排hosts,不知道什么原因。

这个是我改的它的代码,方便查询每个节点信息。

我觉得这个功能挺有用,能否发我参考借鉴一下啊?

直接改hystrix-dashboard/monitor/monitor.html 像这样:
<div class="row">
                        <div class="menubar" style="border-bottom: 1px solid black;">
                                <div class="title">
                                Hosts
                                </div>
                                <div class="menu_actions">
                                        <a href="http://10.10.52.136:8080/hystrix-dashboard/monitor/monitor.html?stream=http%3A%2F%2F10.10.34.11%3A8080%2Fhystrix.stream&delay=4000&title=${ip}" target="_blank"">${ip}</a> 
....
                                </div>
                        </div>
                </div>

好的,非常感谢!
6 楼 hot66hot 2015-04-16  
ravollen 写道
hot66hot 写道
ravollen 写道
hot66hot 写道
ravollen 写道
hi,你好,问一下,你这图上的这个turbine可针对单台机器的监控是hystrix dashbroad自带的功能还是自己开发的呢?

是Netflix公司开发的Turbine,见:https://github.com/Netflix/Turbine/wiki

嗯,我现在也是用的trubine,但是没有你这图上的上面那一排hosts,不知道什么原因。

这个是我改的它的代码,方便查询每个节点信息。

我觉得这个功能挺有用,能否发我参考借鉴一下啊?

直接改hystrix-dashboard/monitor/monitor.html 像这样:
<div class="row">
                        <div class="menubar" style="border-bottom: 1px solid black;">
                                <div class="title">
                                Hosts
                                </div>
                                <div class="menu_actions">
                                        <a href="http://10.10.52.136:8080/hystrix-dashboard/monitor/monitor.html?stream=http%3A%2F%2F10.10.34.11%3A8080%2Fhystrix.stream&delay=4000&title=${ip}" target="_blank"">${ip}</a> 
....
                                </div>
                        </div>
                </div>
5 楼 ravollen 2015-04-15  
hot66hot 写道
ravollen 写道
hot66hot 写道
ravollen 写道
hi,你好,问一下,你这图上的这个turbine可针对单台机器的监控是hystrix dashbroad自带的功能还是自己开发的呢?

是Netflix公司开发的Turbine,见:https://github.com/Netflix/Turbine/wiki

嗯,我现在也是用的trubine,但是没有你这图上的上面那一排hosts,不知道什么原因。

这个是我改的它的代码,方便查询每个节点信息。

我觉得这个功能挺有用,能否发我参考借鉴一下啊?
4 楼 hot66hot 2015-04-14  
ravollen 写道
hot66hot 写道
ravollen 写道
hi,你好,问一下,你这图上的这个turbine可针对单台机器的监控是hystrix dashbroad自带的功能还是自己开发的呢?

是Netflix公司开发的Turbine,见:https://github.com/Netflix/Turbine/wiki

嗯,我现在也是用的trubine,但是没有你这图上的上面那一排hosts,不知道什么原因。

这个是我改的它的代码,方便查询每个节点信息。
3 楼 ravollen 2015-04-13  
hot66hot 写道
ravollen 写道
hi,你好,问一下,你这图上的这个turbine可针对单台机器的监控是hystrix dashbroad自带的功能还是自己开发的呢?

是Netflix公司开发的Turbine,见:https://github.com/Netflix/Turbine/wiki

嗯,我现在也是用的trubine,但是没有你这图上的上面那一排hosts,不知道什么原因。

相关推荐

    08-Hystrix源码分析1

    08-Hystrix源码分析1

    14.Netflix之Hystrix详细分析

    详细讲解了Hystrix的执行流程,以及对应流程中的一些组件的概念

    Spring Cloud Hystrix入门和Hystrix命令原理分析

    主要介绍了Spring Cloud Hystrix入门和Hystrix命令原理分析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    微服务springcloud之ribbon和hystrix使用demo.7z

    ribbon 就是客户端负载均衡,根据配置的负载均衡策略进行请求,如果同一请求的数量较大时,需要将请求合并,而且每一个请求需要进行线程隔离,也就是我们hystrix 出场了,这是我们按照需求分析的三个组件的作用,...

    Hystrix源码简析

    Hystrix源码简析:包括线程隔离和信号量隔离实现分析、熔断器实现分析等

    spring cloud hystrix &&dashboard源码解读

    spring cloud hystrix &&dashboard源码解读

    springcloud微服务框架+服务模版

    spring-cloud-hystrix:Hystrix熔断的使用示例 hystrix-dashboard-turbine:熔断监控Hystrix Dashboard和Turbine的示例 spring-cloud-config-git:配置中心git版本示例 spring-cloud-config-svn-refresh:配置中心...

    spring-cloud-examples

    spring-cloud-hystrix:Hystrix熔断的使用示例 hystrix-dashboard-turbine:熔断监控Hystrix Dashboard和Turbine的示例 spring-cloud-config-git:配置中心git版本示例 spring-cloud-config-svn-refresh:配置中心...

    spring-cloud组件原理分析.ppt

    spring cloud组件源码分析,包括Eureka,feign,gateway,ribbon,Hystrix组件

    新版本SpringCloud2学习手册

    负载均衡器源码分析 33 负载均衡器重试机制 33 服务保护机制SpringCloud Hystrix 33 微服务高可用技术 33 服务降级、熔断、限流概念 34 服务学崩效应 34 服务降级 34 服务熔断 35 服务隔离 35 服务限流 36 ...

    spring-cloud使用的各种示例

    - [spring-cloud-hystrix](https://github.com/ityouknow/spring-cloud-examples/tree/master/spring-cloud-hystrix):Hystrix熔断的使用示例 - [hystrix-dashboard-turbine]...

    甜瓜属人工异源四倍体早期世代遗传变异的SSAP 分析

    甜瓜属人工异源四倍体早期世代遗传变异的SSAP 分析,陈劲枫,江彪,为了研究甜瓜属人工异源四倍体早期世代遗传变异,以酸黄瓜Cucumis hystrix(2n=24)与栽培黄瓜C. sativus L. cv. Beijingjietou(2n=14)的正反交...

    开涛高可用高并发-亿级流量核心技术

    3.10 使用Hystrix实现隔离 51 3.10.1 Hystrix简介 51 3.10.2 隔离示例 52 3.11 基于Servlet 3实现请求隔离 56 3.11.1 请求解析和业务处理线程池分离 57 3.11.2 业务线程池隔离 58 3.11.3 业务线程池监控/运维/降级 ...

    lamp-cloud微服务脚手架

    基于Nacos来实现的服务注册与发现,使用使用Feign来实现服务互调, 可以做到使用HTTP请求远程调用时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求。 2、服务鉴权: 通过...

    spring-boot-2.1.0.RELEASE:SpringBoot源码分析-项目里有超详细的中文代码注释,中文注释会转换每篇SpringBoot的源码分析文章的推出而更新,中文注释持续更新中..-项目

    【原始笔记】专注于Java... SpringCloud(Eureka,Ribbon,Hystrix,Zuul,Config,Feign ...) 四郎 Tomcat 西塔 运河 联合会 卡夫卡 纳科斯 动物园管理员 我的猫 ...... 微信搜: 公众号:原始笔记 联系我:艰苦奋斗

    springcloud03.doc

    Hystrix 隔离策略 .线程池隔离-- 控制线程的并发 THREAD 线程池隔离策略 独立线程接收请求 默认的 默认采用的就是线程池隔离

    zxs:洲鑫系统

    算法原始分析:code-analysis设计模式手写mybatis手写spring,springMVC并发:并发螺纹池分布式:Distributed 生物,蔚来净值rmijdk:jdk分析反射安全杰姆克斯函数接口片断探究React性-x:React性-x API使用微服务:...

    leetcode中文版-Interview:JavaInterview知识点总结与面试

    项目用于保存平时我对各种知识点的总结和一些源码分析,同时此文档也是平时收集的面试复习资料。 文档内有大量的不同网站的引用,同时我也参考了github上大量的面试指南。在每一部分的下方都留有引用原地址的链接,...

Global site tag (gtag.js) - Google Analytics