Hystrix 请求合并、请求隔离、优化
创始人
2024-02-09 22:01:13
0

文章目录

  • 请求合并
    • 引入依赖
    • 启动类 加注解@EnableHystrix
    • service服务
    • 测试
  • 请求隔离
    • 线程池隔离(大部分情况下)
    • 信号量隔离
  • 线程池隔离演示
    • 引入依赖
    • 启动类 加注解@EnableHystrix
    • service服务
    • 测试
  • 信号量隔离演示
  • Hystrix的其他用法

请求合并

引入依赖

org.springframework.cloudspring-cloud-starter-netflix-hystrix2.2.7.RELEASE

org.projectlomboklombok

启动类 加注解@EnableHystrix

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;@SpringBootApplication
@EnableHystrix
public class DemoApplication {public static void main(String[] args) {SpringApplication.run (DemoApplication.class, args);}}

service服务

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.concurrent.Future;
import java.util.stream.Collectors;@Component
@Slf4j
public class TestServcie {@HystrixCollapser(batchMethod = "getUserByIdBatch", //合并 请求方法scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,//请求 方式collapserProperties = {// 间隔多久的请求会进行合并,默认是 10ms (时间限制)@HystrixProperty(name = "timerDelayInMilliseconds", value = "15"),// 批处理之前,批处理中允许的最 大请求数 (数量限制)@HystrixProperty(name = "maxRequestsInBatch", value = "200"),@HystrixProperty(name = "requestCache.enabled", value = "true")})// 处理请求合并的方法一定 要支持异步!!,返回值必须是 Future// 指出 方法的返回值,参数public Future getUserById(Long id) {log.info ("------selectProductByIdReturnFuture------");return null;}/*** 因为是 合并请求,所以 返回值是 之前返回值的集合,参数 也是之前参数的的集合*/// 声明 请求合并的方法@HystrixCommandpublic List getUserByIdBatch(List ids) {log.info ("=====batch start ====");// TODO 对这些数据 批量处理!!List users = ids.stream ().map (x -> {User user = new User ();user.setId (x);user.setName ("name :" + x);return user;}).collect (Collectors.toList ());log.info ("=====batch end ,handle {} 条 request====", ids.size ());return users;}
}

测试

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;@RestController
@Slf4j
public class RetryController {@Resourceprivate TestServcie testServcie;@GetMapping("/test")public String getUser(long id) throws ExecutionException, InterruptedException {Future f = testServcie.getUserById (id);System.out.println (f.get ());return "success";}}@Data
class User {private long id;private String name;}

结果:

在这里插入图片描述

请求隔离

线程池隔离(大部分情况下)

之前请求缓存的例子中,并发的请求一个接口时,造成另一个接口耗时也增多,当时我们使用的缓存解决,还可以使用线程池隔离方式解决
在这里插入图片描述
而增加了隔离以后,两个接口隔离,即每个隔离粒度都是个线程池,互不干扰,异步方式提高了并发的性能
在这里插入图片描述
这种方式线程每次创建销毁切换等等都需要消耗CPU的性能,如果要隔离的实例太多了,成百上千个,也不推荐使用这种情况

信号量隔离

同步方式,阻塞我们 请求方的线程有一个信号量的概念,其实就是个计数器,当请求来的时候先拿到信号量,再执行,执行完释放

当信号量消耗完时其他的线程就阻塞等待或者快速释放
在这里插入图片描述
同步方式,不支持超时,无法用于网络。比如 系统内部的逻辑处理时可以使用

线程池隔离演示

引入依赖


org.springframework.cloudspring-cloud-starter-netflix-hystrix2.2.7.RELEASE

org.projectlomboklombok

启动类 加注解@EnableHystrix

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;@SpringBootApplication
@EnableHystrix
public class DemoApplication {public static void main(String[] args) {SpringApplication.run (DemoApplication.class, args);}}

service服务

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;@Component
@Slf4j
public class TestServcie {// 声明需要服务容错的方法// 线程池隔离@HystrixCommand(commandKey = "getProductList",//接口名称,默认方法名threadPoolKey = "order-testServcie-Pool",//线程池名称,相同名称使用同一个线程池commandProperties = {// 隔离策略@HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),//超时时间,默认1000ms, 超时 就走fallbackMethod 降级@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "200")},threadPoolProperties = {// 线程池核心线程大小@HystrixProperty(name = "coreSize", value = "3"),// 队列 等待阈值(最大队列长度,默认-1)@HystrixProperty(name = "maxQueueSize", value = "10"),// 线程存活时间,默认 1min@HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),// 超出队列 等待阈值 执行拒绝策略!!@HystrixProperty(name = "queueSizeRejectionThreshold", value = "10"),}, fallbackMethod = "fallbackMtd")public String getProductList() {log.info (Thread.currentThread ().getName () + "---getProductList----");return "success";}/*** 超出队列等待阈值执行方法**/private String fallbackMtd() {log.info (Thread.currentThread ().getName () + "---降级sorry, the request is timeout----");return "降级sorry, the request is timeout";}}

测试

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@Slf4j
public class RetryController {@Resourceprivate TestServcie testServcie;@GetMapping("/test")public String getUser() {return testServcie.getProductList ();}}

结果:

在这里插入图片描述

信号量隔离演示

其他不变 修改TestServcie

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;@Component
@Slf4j
public class TestServcie {//信号量隔离@HystrixCommand(commandProperties = {//超时时间,默认1000ms@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "200"),//信号量隔离@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY, value = "SEMAPHORE"),//信号量最大并发,调小一点方便模拟高并发@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, value = "6")}, fallbackMethod = "fallbackMtd")public String getProductList() {log.info (Thread.currentThread ().getName () + "---getProductList----");return "success";}/*** 超出队列等待阈值执行方法**/private String fallbackMtd() {log.info (Thread.currentThread ().getName () + "---降级sorry, the request is timeout----");return "降级sorry, the request is timeout";}}

结果:

在这里插入图片描述

Hystrix的其他用法

SpringCloud优化记录

相关内容

热门资讯

押韵的经典语录社会 导语:时光听着一段旋律,熟悉在心里的记忆,阳光烘干一片阴雨,温暖在晴朗的笑容里,目光琉璃一行小诗,晶...
大一新生寄语 大一新生寄语5篇  在平日的学习、工作和生活里,大家都不可避免地会接触到寄语吧,寄语是指寄托希望和美...
毕业感言:值得我们珍惜 毕业感言:值得我们珍惜  每年凤凰花开时,就是一个小学生飞向另一段旅程,开始青少年生活的时候,毕业感...
与老外的聊天技巧 与老外的聊天技巧  西方人为了彼此融洽相处,维护国家良好形象,特别重视生活教育和人际关系。尤其平时说...
代嫁弃妃 安知晓 小说语录   方流苏:有的人,适合相伴一生,有的人,适合怀念一生,这都是幸福  ——安知晓《代嫁弃妃》  风南...
安意如《陌上花开》唯美语录   爱情用来遗忘,感情用来摧毁,忠诚用来背叛,在时之洪流中起落,人心常常经不住世事熬煮。一切都存在变...
幼儿园暑假工作安排公文 幼儿园暑假工作安排公文  尊敬的家长朋友们:  转眼间,本学期即将结束,衷心的'感谢您一直以来对幼儿...
感谢家长的话语 感谢家长的话语感谢家长的话语1.坐在电脑前敲打着键盘,出一张“两位数加减一位数、整十数”的口算练习。...
第一学期中班幼儿评语 第一学期中班幼儿评语(精选15篇)  在平平淡淡的日常中,大家都写过评语吧,评语的内容包括被考评者的...
王小波的经典语录100句   王小波的小说既继承了年代对爱情与革命权力关系的思考,具有强烈启蒙意味,也顺应了年代世俗化潮流。接...
经典祝愿高考成功寄语 经典祝愿高考成功寄语(通用90句)  六月参加高考,准备还得趁早,心态一定摆正,付出必有回报,毕竟熬...
电影的观后感 电影的观后感  在观看完一部作品以后,能够给我们不少启示,这时候最关键的观后感不能忘了。但是观后感有...
利益关系经典语录 利益关系经典语录  在日常的学习、工作、生活中,大家都知道一些经典的语录吧,语录是指一个人的说话记录...
哆啦a梦语录 哆啦a梦语录  01、骗人有风险,说谎要谨慎。  02、日子就像偷跑的小孩,无声地溜走。  03、知...
感人的情书经典语录   在很多超感人的情书中,是有一些情书经典语录起到关键作用。小编今天收集了超感人情书经典语录,希望得...
高中生毕业寄语 高中生毕业寄语4篇  在日常的学习、工作、生活中,大家总免不了要接触或使用寄语吧,寄语是所传的、寄托...
优秀班干部获奖感言 优秀班干部获奖感言优秀班干部获奖感言尊敬的老师,亲爱的同学: 大家晚上好!我是S,今天很高兴作为获奖...
最新感人经典语录 最新感人经典语录  1其实,我不是一定要等你,只是等上了,就等不了别人了。《朝露若颜》  2如果世界...
小说我的美女大小姐经典语录   揭穿谎言背后的谎言多累啊!还不如站的离你远点儿,看着你怎么在谎言中尽情的表演  ——李兴禹《我的...
个性早安寄语 常用个性早安寄语汇总75句  我只是想写一封信给你,寄信人在河这头,收信人在河那头。隔开我们的这条河...