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
- hystrix入门
- Hystrix入门指南
- springcloud入门之熔断器hystrix
- Hystrix
- Hystrix
- Hystrix使用入门手册(中文)
- Hystrix使用入门手册(中文)
- Hystrix基础入门和特性讲解
- Hystrix使用入门手册(中文)
- Spring Cloud入门1——Eureka&Ribbon&Hystrix
- Spring基础:快速入门spring cloud(5):断路器之Hystrix
- 《spring cloud微服务实战》读书笔记——Spring Cloud Hystrix(一)快速入门案例
- Hystrix使用
- Hystrix介绍
- Hystrix总结
- Hystrix 简介
- Hystrix 仪表盘
- Hystrix Javanica
- java 代码覆盖率
- html中js表单验证
- 如何修改“设置” -> “显示” ->“休眠”的默认休眠时间
- LeakCanary(二)内存泄露监测原理研究
- JavaScript异步
- hystrix入门
- java内存分配和String类型的深度解析
- 用wow.js实现滚动页面时触发CSS 动画效果的插件
- 堆、栈、程序区、静态数据区、字符常量区的介绍与区别
- MAVEN pom.xml文件添加依赖报错
- CI框架在Apache服务器下去掉URL中的index.php
- Inorder Successor in Binary Search Tree
- 使用Areas分离ASP.NET MVC项目
- 微信小程序,引爆新热点!JEECG社区小程序实战培训,业内首发,实战干货!