欢迎来到淼淼之森的博客小站。  交流请加我微信好友: studyjava。  也欢迎关注同名公众号:Java学习之道

SpringCloud(三):Hystrix-有界面和无界面的配置 置顶!

  |   0 评论   |   0 浏览

Hystrix

Propagating the Security Context or Using Spring Scopes

如果您希望某些线程本地上下文传播到@HystrixCommand,则默认声明不起作用,因为它在线程池中执行该命令(如果超时)。您可以通过配置或直接在注释中切换Hystrix,以使用与调用者相同的线程,方法是要求它使用不同的“隔离策略”。

  • 示例:
    • 演示如何在注释中设置线程:
    @RestController
    public class GoodsController {
     @Autowired
     private RestTemplate restTemplate;
    
     @GetMapping("/goods/{id}")
     //fallbackMethod定义的方法的参数名和返回值一定要和原参数一致
     @HystrixCommand(fallbackMethod = "findByIdFallback")
     public User findById(@PathVariable Long id) {
     return this.restTemplate.getForObject("http://microservice-provider-user/user/" + id, User.class);
     }
    
     public User findByIdFallback(Long id){
     User user = new User();
     user.setId(0L);
     user.setUsername("zhang三");
     return user;
     }
    
    }

一般首先不配置commandProperties ,如果遇到运行时异常,表示无法找到作用域上下文,则需要使用相同的线程,才需要配置。
因为请求是一个线程,@HystrixCommand是一个隔离的线程;
如果您使用@SessionScope或@RequestScope,也能达到同样的效果。

  • 使用commandProperties
@RestController
public class GoodsController {
@Autowired
private RestTemplate restTemplate;

@GetMapping("/goods/{id}")
//fallbackMethod定义的方法的参数名和返回值一定要和原参数一致
@HystrixCommand(fallbackMethod = "findByIdFallback", commandProperties = { @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")} )
//commandProperties = @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
//一般首先不做配置,如果遇到运行时异常,表示无法找到作用域上下文,则需要使用相同的线程,才需要配置。
//因为请求是一个线程,@HystrixCommand是一个隔离的线程,由于不在同一个线程,容易导致找不到上下文
//如果您使用@SessionScope或@RequestScope,也能达到同样的效果。
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject("http://microservice-provider-user/user/" + id, User.class);
}

public User findByIdFallback(Long id){
User user = new User();
user.setId(0L);
user.setUsername("zhang三");
return user;
}
  • 使用@SessionScope或@RequestScope
@RestController
@SessionScope
@Scope("session")
public class GoodsController {
@Autowired
private RestTemplate restTemplate;

@GetMapping("/goods/{id}")
@HystrixCommand(fallbackMethod = "findByIdFallback" )
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject("http://microservice-provider-user/user/" + id, User.class);
}

public User findByIdFallback(Long id){
User user = new User();
user.setId(0L);
user.setUsername("zhang三");
return user;
}

}

==注:== 了解scope的分类:Spring Scope

监控Hystrix界面:Hystrix dashboard 和 Turbine

链接:https://blog.csdn.net/hry2015/article/details/78617954

Hystrix Metrics Stream

要启用Hystrix度量标准流,请在==spring-boot-starter-actuator==上包含依赖项,并设置==management.endpoints.web.exposure.include:hystrix.stream==。 这样做会将 ==/actuator/hystrix.stream==公开为管理端点,如以下示例所示:

  • pom.xml
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  • application.yml
    # 配置Hystrix Metrics Stream
    management:
      endpoints:
        web:
          exposure:
            include: hystrix.stream

Hystrix Dashboard

  • pom.xml添加依赖
<!-- hystrix dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
  • 启动类添加Hystrix Dashboard注解
@EnableHystrixDashboard

Hystrix Turbine

  • 概念
    • 一个准实时的集群界面监控工具
    • 有一定的延迟
    • 因为取服务需要一定的时间
  • 如何使用
    • pom.xml添加依赖
     <!-- hystrix turbine -->
     <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
     </dependency>
    
    • 启动类添加Hystrix Turbine注解
        @EnableTurbine
    
    • application.xml
    # 配置turbine
    turbine:
      aggregator:
        clusterConfig: MICROSERVICE-CONSUMER-GOODS-RIBBON-WITH-HYSTRIX
      appConfig: microservice-consumer-goods-ribbon-with-hystrix
    

标题:SpringCloud(三):Hystrix-有界面和无界面的配置
作者:mmzsblog
地址:https://www.mmzsblog.cn/articles/2019/08/06/1565077872515.html

如未加特殊说明,文章均为原创,转载必须注明出处。均采用CC BY-SA 4.0 协议

本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。若本站转载文章遗漏了原文链接,请及时告知,我们将做删除处理!文章观点不代表本网站立场,如需处理请联系首页客服。
• 网站转载须在文章起始位置标注作者及原文连接,否则保留追究法律责任的权利。
• 公众号转载请联系网站首页的微信号申请白名单!

个人微信公众号 ↓↓↓                 

微信搜一搜 Java 学习之道