栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud断路器Hystrix原理及用法解析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud断路器Hystrix原理及用法解析

这篇文章主要介绍了SpringCloud断路器Hystrix原理及用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

在分布式环境中,许多服务依赖项中的一些必然会失败。Hystrix是一个库,通过添加延迟容忍和容错逻辑,帮助你控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点、停止级联失败和提供回退选项来实现这一点,所有这些都可以提高系统的整体弹性

两个比较重要的类

  • HystrixCommand
  • HystrixObservableCommand

注解@HystrixCommand(fallbackMethods="methods")methods中可以添加降级策略

除了提供服务降级

还提供了请求缓存

  • @CacheResult
  • @CacheRemve

不过添加CacheResult的时候,说

HystrixRequestContext未初始化。

2020-01-13 16:12:10.273 ERROR 15348 --- [nio-8083-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]  : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.reflect.UndeclaredThrowableException] with root cause

java.lang.IllegalStateException: Request caching is not available. Maybe you need to initialize the HystrixRequestContext?
  at com.netflix.hystrix.HystrixRequestCache.get(HystrixRequestCache.java:104) ~[hystrix-core-1.5.18.jar:1.5.18]
  at com.netflix.hystrix.AbstractCommand$7.call(AbstractCommand.java:478) ~[hystrix-core-1.5.18.jar:1.5.18]
  at com.netflix.hystrix.AbstractCommand$7.call(AbstractCommand.java:454) ~[hystrix-core-1.5.18.jar:1.5.18]
  at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) ~[rxjava-1.3.8.jar:1.3.8]
  at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) ~[rxjava-1.3.8.jar:1.3.8]

查看官方文档https://github.com/Netflix/Hystrix/wiki/How-To-Use

Typically this context will be initialized and shut down via a ServletFilter that wraps a user request or some other lifecycle hook.

在同一用户请求的上下文中,相同依赖服务的返回数据始终保持一致。在当次请求内对同一个依赖进行重复调用,只会真实调用一次。在当次请求内数据可以保证一致性。

初始化是在filter中进行(官方建议),但是每一次请求都会进行初始化 。所以说和一般的缓存还是有去别的,可以解决高并发,保证的资源的线程安全。在某些场景很有用。

请求合并


  @HystrixCollapser(batchMethod = "findALl",collapserProperties = @HystrixProperty(name = "timerDelayInMilliseconds",value = "100"))
  public String doBFindOne(String id){

    System.out.println("begin do provider service");
    return restTemplate.getForEntity("http://service-provider:8081/api/v1/provider/do",String.class).getBody();
  }

全部代码

package com.gitee.munan56.cloud.hystrixconsumer;

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 com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;


@Service
public class AService {

  @Autowired
  private RestTemplate restTemplate;

  public String doAService(){
    return "A Service is run";
  }

//  @Hystrix(fallbackHandler = )
  @HystrixCommand(fallbackMethod = "error")
  public String doBServiceOne(){
    System.out.println("begin do provider service");
    return restTemplate.getForEntity("http://service-provider:8081/api/v1/provider/do",String.class).getBody();
  }

  
  @CacheResult(cacheKeyMethod = "getKey")
  @HystrixCommand(fallbackMethod = "error")
  public String doBServiceTwo(String id){

    System.out.println("begin do provider service");
    return restTemplate.getForEntity("http://service-provider:8081/api/v1/provider/do",String.class).getBody();
  }

  
  @HystrixCollapser(batchMethod = "findALl",collapserProperties = @HystrixProperty(name = "timerDelayInMilliseconds",value = "100"))
  public String doBFindOne(String id){

    System.out.println("begin do provider service");
    return restTemplate.getForEntity("http://service-provider:8081/api/v1/provider/do",String.class).getBody();
  }

  @HystrixCommand()
  public String findALl(List ids){
    System.out.println("begin do provider service");
    return restTemplate.getForEntity("http://service-provider:8081/api/v1/provider/do",String.class).getBody();
  }

  
  public String error(Throwable e){
    return "do provider error this is default result" + "the error is " + e.getMessage();
  }
  
  public String error(String id ,Throwable e){
    return "do provider error this is default result" + "the error is " + e.getMessage();
  }


  public String getKey(String id){
    return id;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135447.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号