备案网站名称修改,网站备案成功怎么查备案号,新加坡 网站建设,如何给局域网 做网站Hystrix 的核心是提供服务容错保护#xff0c;防止任何单一依赖耗尽整个容器的全部用户线程。使用舱壁隔离模式#xff0c;对资源或失败单元进行隔离#xff0c;避免一个服务的失效导致整个系统垮掉#xff08;雪崩效应#xff09;。
1 Hystrix监控
Hystrix 提供了对服务…Hystrix 的核心是提供服务容错保护防止任何单一依赖耗尽整个容器的全部用户线程。使用舱壁隔离模式对资源或失败单元进行隔离避免一个服务的失效导致整个系统垮掉雪崩效应。
1 Hystrix监控
Hystrix 提供了对服务请求的仪表盘监控。在客户端加入以下依赖
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-hystrix-dashboard/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId
/dependency
并在客户端的Application类加上EnableHystrixDashboard注解。
访问地址http://localhost:8080/hystrix
在输入框中输入监控地址http://localhost:8080/hystrix.stream然后点击“Monitor Stream”按钮。 图 Hystrix 监控初始界面 图 Hystrix 仪表盘主要参数及含义
2 服务隔离 图 Hystrix 实现服务隔离的思路
2.1 隔离策略
Hystrix 提供了线程池隔离和信号量隔离两种隔离策略。
execution.isolation.strategy 属性配置隔离策略默认为THREAD(线程池隔离)SEMAPHORE为信号量隔离。execution.isolation.thread.timeoutInMilliseconds属性配置请求超时时间默认为1000ms。
2.1.1 线程池隔离
不同服务的执行使用不同的线程池同时将用户请求的线程如Tomcat与具体业务执行的线程分开业务执行的线程池可以控制在指定的大小范围内从而使业务之间不受影响达到隔离的效果。 优点 隔离优势如果一个服务发生问题只会影响该服务而不会影响其他服务。方便性能变化调整可以更改对应command的参数超时时间、重试次数、线程池大小等。 缺点 线程及线程池的创建及管理增加了计算开销。
表 线程池隔离的优缺点
关于线程池隔离的相关配置有如下参数
coreSize:线程池核心线程数。即线程池中保持存活的最小线程数。默认为10。
maximumSize:线程池允许的最大线程数。当核心线程数已满且任务队列已满时线程池会尝试创建新的线程直到达到最大线程数。默认为10。
maxQueueSize:线程池任务队列的大小。默认为-1表示任务将被直接交给工作线程处理而不是放入队列中等待。
queueSizeRejectionThreshold:即便没达到maxQueueSize阈值但达到该阈值时请求也会被拒绝默认值为5。 图 新的请求在线程池的处理过程
是否创建新线程是指当前活跃线程已达到coreSize,但线程数小于maximumSizeHystrix 会创建新的线程来处理该任务。这一步还受到queueSizeRejectionThreshold及maxQueueSize参数的影响。
2.1.2 信号量隔离
用户请求线程和业务执行线程是同一线程通过设置信号量的大小限制用户请求对业务的并发访问量从而达到限流的保护效果。 优点 1 开销小避免了线程创建、销毁及上下文切换等开销。 2 配置简单只需要设置信号量大小即可。 3 适合轻量级操作如内存或缓存服务访问这些操作不太可能导致长时间延迟因此信号隔离可以保持系统的高效性。 缺点 限流能力有限存在阻塞风险如果依赖服务阻塞因为其没有使用线程池来隔离请求那么可能会影响整个请求链路导致系统性能下降。
表 Hystrix 信号隔离的优缺点
execution.isolation.semaphore.maxConcurrentRequests 最大并发请求数信号量大小。默认为10。
2.2 服务隔离的颗粒度
HystrixCommand 注解还有三个属性commandKey标识该命令全局唯一的名称默认情况下同一个服务名称共享一个线程池、groupKey组名Hystrix 会让相同组名的命令使用同一个线程池、threadPoolKey线程池名称多个服务可以设置同一个threadPoolKey来共享同一个线程池在信号量隔离策略中不起作用。
3 请求缓存
在用户的同一个请求中消费者可能会多次重复调用一个服务。Hystrix 提供的请求缓存可以在CommandKey/CommandGroup相同的情况下直接共享第一次命令执行的结果降低依赖调用次数。
Service
public class UserService {private final RestTemplate restTemplate;public UserService(RestTemplate restTemplate) {this.restTemplate restTemplate;}CacheResult(cacheKeyMethod generateCacheKey)HystrixCommand(commandKey info3,groupKey info3Group,commandProperties {HystrixProperty(name requestCache.enabled, value true)})public RequestResultString info3(String name) {System.out.println(发送请求 name);return RequestResult.success(restTemplate.getForObject(http://provider/user/info?name name, String.class));}public String generateCacheKey(String name) {return name;}
}
RequestMapping(/home)
RestController
public class HomeController {private final RestTemplate restTemplate;private final UserService userService;public HomeController(RestTemplate restTemplate, UserService userService) {this.restTemplate restTemplate;this.userService userService;}GetMapping(/info3)public RequestResultString info3(String name) {HystrixRequestContext context HystrixRequestContext.initializeContext();RequestResultString result userService.info3(name);userService.info3(name);context.close();return result;}
} 4 请求合并
Hystrix针对高并发场景支持将多个请求自动合并为一个请求通过合并可以减少对依赖的请求极大节省开销提高系统效率。
请求合并主要是通过两部分实现1HystrixCollapser 指定高并发请求的对应请求其返回值为Futuer。2HystrixCommand 指定合并后的单个请求。其参数为第1部分请求参数的集合。
Service
public class UserService {private final RestTemplate restTemplate;public UserService(RestTemplate restTemplate) {this.restTemplate restTemplate;}HystrixCollapser(collapserKey info,scope com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,batchMethod batchInfo,collapserProperties {HystrixProperty(name timerDelayInMilliseconds, value 1000),HystrixProperty(name maxRequestsInBatch, value 3)})public FutureRequestResultString batchInfo(String name) {// 不会被执行System.out.println(HystrixCollapser info);return null;}HystrixCommandpublic ListRequestResultString batchInfo(ListString names) {System.out.println(批量发送 names);// 依赖也需要有一个支持批量的接口String res restTemplate.getForObject(http://provider/user/info?name names, String.class);ListRequestResultString list new ArrayList();int count 0;for (String str : names) {list.add(RequestResult.success(res kk str count));}return list;}
}
RequestMapping(/home)
RestController
public class HomeController {private final RestTemplate restTemplate;private final UserService userService;public HomeController(RestTemplate restTemplate, UserService userService) {this.restTemplate restTemplate;this.userService userService;}GetMapping(/info2)public RequestResultString info2(String name) throws ExecutionException, InterruptedException {HystrixRequestContext context HystrixRequestContext.initializeContext();FutureRequestResultString future userService.batchInfo(name);RequestResultString result future.get();context.close();return result;}
}