当前位置: 首页 > news >正文

电商网站开发需要掌握哪些知识技能昆山网站制作昆山网站建设

电商网站开发需要掌握哪些知识技能,昆山网站制作昆山网站建设,网页设计与制作配套素材,搭建网站 网页Hystrix线程隔离 在微服务框架中,可能一个服务需要调用多个微服务,在tomcat中运行时,tomcat只是分配了100个线程,由于多个服务之间调用的时间消耗过长,可能会导致线程耗尽,而在Hystrix中存在线程隔离&…

Hystrix线程隔离

在微服务框架中,可能一个服务需要调用多个微服务,在tomcat中运行时,tomcat只是分配了100个线程,由于多个服务之间调用的时间消耗过长,可能会导致线程耗尽,而在Hystrix中存在线程隔离,对于每个微服务分配一个线程池,访问某个微服务时就从对应的线程池中取线程,如果对应线程池中的线程都用光了,那么就认为该服务不可用了,如果在需要请求该微服务,则直接返回

那么这个线程池是存在于哪里呢?

  • [ ]

既然Hystrix的Command都是在线程池中执行的,就会遇到当前的RequestContextHolder获取不到RequestAttributes,没办法,跨线程了呀(RequestContextHolder中使用了ThreadLocal),这怎么解决呢?Hystrix中提供了一个HystrixConcurrencyStrategy类,HystrixConcurrencyStrategy提供了一套默认的并发策略实现。我们可以根据我们自己不同需求通过装饰去扩展它。如每次执行HystrixCommand的时候都会去调用wrapCallable(Callable) 方法,这里我们就可以通过装饰Callable使它提供一些额外的功能(如ThreadLocal上下文传递)

public class FeignHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy
{private static final Logger log;private HystrixConcurrencyStrategy delegate;public FeignHystrixConcurrencyStrategy() {try {this.delegate = HystrixPlugins.getInstance().getConcurrencyStrategy();if (this.delegate instanceof FeignHystrixConcurrencyStrategy) {return;}HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance().getCommandExecutionHook();HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance().getPropertiesStrategy();HystrixPlugins.reset();// 注册并发策略HystrixPlugins.getInstance().registerConcurrencyStrategy(this);HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);}catch (Exception e) {log.error("Failed to register Sleuth Hystrix Concurrency Strategy", e);}}// 这个时候还在主线程了,所以通过RequestContextHolder.getRequestAttributes()是能拿到上下文的拿到后hold住,等到run执行的时候再绑定即可public <T> Callable<T> wrapCallable( Callable<T> callable) {// 获取当前请求的requestAttributesRequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();return new WrappedCallable<T>(callable, requestAttributes);}public ThreadPoolExecutor getThreadPool( HystrixThreadPoolKey threadPoolKey,  HystrixProperty<Integer> corePoolSize,  HystrixProperty<Integer> maximumPoolSize,  HystrixProperty<Integer> keepAliveTime,  TimeUnit unit,  BlockingQueue<Runnable> workQueue) {return this.delegate.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);}public ThreadPoolExecutor getThreadPool( HystrixThreadPoolKey threadPoolKey,  HystrixThreadPoolProperties threadPoolProperties) {return this.delegate.getThreadPool(threadPoolKey, threadPoolProperties);}public BlockingQueue<Runnable> getBlockingQueue( int maxQueueSize) {return this.delegate.getBlockingQueue(maxQueueSize);}public <T> HystrixRequestVariable<T> getRequestVariable( HystrixRequestVariableLifecycle<T> rv) {return this.delegate.getRequestVariable(rv);}class WrappedCallable<T> implements Callable<T>{private final Callable<T> target;private final RequestAttributes requestAttributes;public WrappedCallable( Callable<T> target,  RequestAttributes requestAttributes) {this.target = target;this.requestAttributes = requestAttributes;}@Overridepublic T call() throws Exception {try {// 执行之前绑定上下文,执行完成后释放RequestContextHolder.setRequestAttributes(this.requestAttributes);return this.target.call();}finally {RequestContextHolder.resetRequestAttributes();}}}
}

这时候大家就奇怪了,为什么在wrapCallable方法中可以获取到当前请求呢,来看源码是怎么调用HystrixConcurrencyStrategy的

public class HystrixContextRunnable implements Runnable {private final Callable<Void> actual;// 父线程的上下文private final HystrixRequestContext parentThreadState;public HystrixContextRunnable(Runnable actual) {this(HystrixPlugins.getInstance().getConcurrencyStrategy(), actual);}public HystrixContextRunnable(HystrixConcurrencyStrategy concurrencyStrategy, final Runnable actual) {// 实例化HystrixContextRunnable的时候去调用的concurrencyStrategy.wrapCallable,此时还没有切换线程呢this.actual = concurrencyStrategy.wrapCallable(new Callable<Void>() {@Overridepublic Void call() throws Exception {actual.run();return null;}});this.parentThreadState = HystrixRequestContext.getContextForCurrentThread();}@Overridepublic void run() {HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread();try {// set the state of this thread to that of its parentHystrixRequestContext.setContextOnCurrentThread(parentThreadState);// execute actual Callable with the state of the parenttry {actual.call();} catch (Exception e) {throw new RuntimeException(e);}} finally {// restore this thread back to its original stateHystrixRequestContext.setContextOnCurrentThread(existingState);}}}

参考文献

  • Hystrix线程隔离导致的问题
http://www.yayakq.cn/news/752567/

相关文章:

  • 河南移动官网网站建设网站维护是谁做的
  • wordpress4.2 for saeseo网络优化培训
  • 高端网站建设必去磐石网络短链接生成器app
  • 爱做网站视频我想做互联网怎么做
  • 找谁做网站优化网站管理的主要内容
  • 大型多媒体网站建设工具安徽省建设安全监督站的网站
  • 湖北省住房和城乡建设厅网站的公示公告做网站很简单
  • 深圳市招投标交易中心网站南昌网站建设搜q.479185700
  • 自己的服务器建网站计算机专业论文 网站建设
  • 自己的网站怎么优化帝国cms添加网站地图
  • 网站空间域名续费哪个推广平台效果好
  • 网站后台可视化编辑高端 旅游 网站建设
  • 中国职业培训在线官方网站郑州鹏之信网络科技有限公司
  • 汉阳网站推广公司做电子简历的网站
  • 建设银行梅李分行网站广东网站建设教程
  • 怎么把网站推广出去一个公司网站的价格
  • 自适应网站建设都找全网天下上海招聘网站建设
  • 怎么制作属于自己的网站备案后怎么建设网站
  • 做旅游网站的工作流程图济南哪里有做网站的
  • 网站建设要准备的内容医疗器械产品设计公司
  • 宁波有哪家公司做网站的j建设局域网网站
  • 把网站放在虚拟主机上 怎么进入网站后台关键词怎么做快速的有排名
  • 建网站多少钱 万户在线解析网站
  • 南通网站建设top群晖 直接编辑wordpress
  • 做个网站应该怎么做大连网站代运营的公司有哪些
  • 网站后台都需要什么软件做用户登录页面html代码
  • 网站开发资源韩国小游戏网站
  • 济南网站的公司哪家好wordpress 阿里云
  • 网站建设相关工作学前端去哪个培训机构
  • 胶州市住房建设局网站企业管理咨询的工作主体