Spring Cloud实战小贴士:turbine如何聚合设置了context-path的hystrix数据

简介: Spring Cloud实战小贴士:turbine如何聚合设置了context-path的hystrix数据

之前spring for all社区看到这样一个问题:当actuator端点设置了context-path之后,turbine如何聚合数据?首先,我们要知道actuator端点设置了context-path是什么意思?也就是说,此时spring boot actuator的端点都有了一个前缀,比如:

management.context-path=/xxx

如果设置了上面的参数,那个对于收集hystrix数据的端点将变为:/xxx/hystrix.stream,如果我们还是拿上一Spring Cloud构建微服务架构:Hystrix监控数据聚合【Dalston版】中构建你的turbine应用,那么将会看到如下错误:

INFO 7812 --- [        Timer-0] c.n.t.monitor.instance.InstanceMonitor   : Url for host: http://172.15.0.18:9020/hystrix.stream default
ERROR 7812 --- [InstanceMonitor] c.n.t.monitor.instance.InstanceMonitor   : Could not initiate connection to host, giving up: [{"timestamp":1499941336284,"status":404,"error":"Not Found","message":"No message available","path":"/hystrix.stream"}]
WARN 7812 --- [InstanceMonitor] c.n.t.monitor.instance.InstanceMonitor   : Stopping InstanceMonitor for: 172.15.0.18:9020 default
com.netflix.turbine.monitor.instance.InstanceMonitor$MisconfiguredHostException: [{"timestamp":1499941336284,"status":404,"error":"Not Found","message":"No message available","path":"/hystrix.stream"}]
  at com.netflix.turbine.monitor.instance.InstanceMonitor.init(InstanceMonitor.java:318) ~[turbine-core-1.0.0.jar:na]
  at com.netflix.turbine.monitor.instance.InstanceMonitor.access$100(InstanceMonitor.java:103) ~[turbine-core-1.0.0.jar:na]
  at com.netflix.turbine.monitor.instance.InstanceMonitor$2.call(InstanceMonitor.java:235) [turbine-core-1.0.0.jar:na]
  at com.netflix.turbine.monitor.instance.InstanceMonitor$2.call(InstanceMonitor.java:229) [turbine-core-1.0.0.jar:na]
  at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_91]
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_91]
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_91]
  at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]

从上述错误中我们可以知道,turbine在收集的时候由于访问的是/hystrix.stream,而此时收集端点却是/xxx/hystrix.stream,所以报了404错误。

那么我们要如何解决呢?通过之前的配置内容,我们可能找不到相关的配置信息,所以只能遍历一下源码,最后找到这个类:org.springframework.cloud.netflix.turbine.SpringClusterMonitor。它的具体内容如下:

public static InstanceUrlClosure ClusterConfigBasedUrlClosure = new InstanceUrlClosure() {
  private final DynamicStringProperty defaultUrlClosureConfig = DynamicPropertyFactory
          .getInstance().getStringProperty("turbine.instanceUrlSuffix",
              "hystrix.stream");
  private final DynamicBooleanProperty instanceInsertPort = DynamicPropertyFactory
        .getInstance().getBooleanProperty("turbine.instanceInsertPort", true);
  @Override
  public String getUrlPath(Instance host) {
    if (host.getCluster() == null) {
      throw new RuntimeException(
          "Host must have cluster name in order to use ClusterConfigBasedUrlClosure");
    }
    // find url
    String key = "turbine.instanceUrlSuffix." + host.getCluster();
    DynamicStringProperty urlClosureConfig = DynamicPropertyFactory.getInstance()
          .getStringProperty(key, null);
    String url = urlClosureConfig.get();
    if (url == null) {
      url = this.defaultUrlClosureConfig.get();
    }
    if (url == null) {
      throw new RuntimeException("Config property: "
          + urlClosureConfig.getName() + " or "
          + this.defaultUrlClosureConfig.getName() + " must be set");
    }
    // find port and scheme
    String port;
    String scheme;
    if (host.getAttributes().containsKey("securePort")) {
      port = host.getAttributes().get("securePort");
      scheme = "https";
    } else {
      port = host.getAttributes().get("port");
      scheme = "http";
    }
    if (host.getAttributes().containsKey("fusedHostPort")) {
      return String.format("%s://%s/%s", scheme, host.getAttributes().get("fusedHostPort"), url);
    }
    // determine if to insert port
    String insertPortKey = "turbine.instanceInsertPort." + host.getCluster();
    DynamicStringProperty insertPortProp = DynamicPropertyFactory.getInstance()
          .getStringProperty(insertPortKey, null);
    boolean insertPort;
    if (insertPortProp.get() == null) {
      insertPort = this.instanceInsertPort.get();
    }
    else {
      insertPort = Boolean.parseBoolean(insertPortProp.get());
    }
    // format url with port
    if (insertPort) {
      if (url.startsWith("/")) {
        url = url.substring(1);
      }
      if (port == null) {
        throw new RuntimeException(
            "Configured to use port, but port or securePort is not in host attributes");
      }
      return String.format("%s://%s:%s/%s", scheme, host.getHostname(), port, url);
    }
    //format url without port
    return scheme + "://" + host.getHostname() + url;
  }
};

从上述源码中,我们可以找到这个参数turbine.instanceUrlSuffix,由此该问题就迎刃而解了,我们只需要在turbine应用的配置文件中增加如下配置信息,就能正确的收集之前配置了management.context-path=/xxx的微服务的hystrix数据了。

turbine.instanceUrlSuffix=/xxx/hystrix.stream

相关阅读

目录
相关文章
|
7月前
|
Java UED 开发者
Spring Boot 降级功能的神秘面纱:Hystrix 与 Resilience4j 究竟藏着怎样的秘密?
【8月更文挑战第29天】在分布式系统中,服务稳定性至关重要。为应对故障,Spring Boot 提供了 Hystrix 和 Resilience4j 两种降级工具。Hystrix 作为 Netflix 的容错框架,通过隔离依赖、控制并发及降级机制增强系统稳定性;Resilience4j 则是一个轻量级库,提供丰富的降级策略。两者均可有效提升系统可靠性,具体选择取决于需求与场景。在面对服务故障时,合理运用这些工具能确保系统基本功能正常运作,优化用户体验。以上简介包括了两个工具的简单示例代码,帮助开发者更好地理解和应用。
143 0
|
3月前
|
消息中间件 监控 Java
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
52 6
|
3月前
|
Java 关系型数据库 MySQL
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
85 5
|
3月前
|
缓存 监控 Java
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
65 5
|
6月前
|
Java Spring
spring多线程实现+合理设置最大线程数和核心线程数
本文介绍了手动设置线程池时的最大线程数和核心线程数配置方法,建议根据CPU核数及程序类型(CPU密集型或IO密集型)来合理设定。对于IO密集型,核心线程数设为CPU核数的两倍;CPU密集型则设为CPU核数加一。此外,还讨论了`maxPoolSize`、`keepAliveTime`、`allowCoreThreadTimeout`和`queueCapacity`等参数的设置策略,以确保线程池高效稳定运行。
609 10
spring多线程实现+合理设置最大线程数和核心线程数
|
5月前
|
存储 Java API
简单两步,Spring Boot 写死的定时任务也能动态设置:技术干货分享
【10月更文挑战第4天】在Spring Boot开发中,定时任务通常通过@Scheduled注解来实现,这种方式简单直接,但存在一个显著的限制:任务的执行时间或频率在编译时就已经确定,无法在运行时动态调整。然而,在实际工作中,我们往往需要根据业务需求或外部条件的变化来动态调整定时任务的执行计划。本文将分享一个简单两步的解决方案,让你的Spring Boot应用中的定时任务也能动态设置,从而满足更灵活的业务需求。
327 4
|
6月前
|
XML 监控 Java
Spring Cloud全解析:熔断之Hystrix简介
Hystrix 是由 Netflix 开源的延迟和容错库,用于提高分布式系统的弹性。它通过断路器模式、资源隔离、服务降级及限流等机制防止服务雪崩。Hystrix 基于命令模式,通过 `HystrixCommand` 封装对外部依赖的调用逻辑。断路器能在依赖服务故障时快速返回备选响应,避免长时间等待。此外,Hystrix 还提供了监控功能,能够实时监控运行指标和配置变化。依赖管理方面,可通过 `@EnableHystrix` 启用 Hystrix 支持,并配置全局或局部的降级策略。结合 Feign 可实现客户端的服务降级。
322 23
|
6月前
|
Java 对象存储 开发者
故障隔离与容错处理:Hystrix在Spring Cloud和Netflix OSS中的应用
故障隔离与容错处理:Hystrix在Spring Cloud和Netflix OSS中的应用
86 3
|
8月前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
805 15
|
7月前
|
Java Spring 开发者
解锁 Spring Boot 自动化配置的黑科技:带你走进一键配置的高效开发新时代,再也不怕繁琐设置!
【8月更文挑战第31天】Spring Boot 的自动化配置机制极大简化了开发流程,使开发者能专注业务逻辑。通过 `@SpringBootApplication` 注解组合,特别是 `@EnableAutoConfiguration`,Spring Boot 可自动激活所需配置。例如,添加 JPA 依赖后,只需在 `application.properties` 配置数据库信息,即可自动完成 JPA 和数据源设置。这一机制基于多种条件注解(如 `@ConditionalOnClass`)实现智能配置。深入理解该机制有助于提升开发效率并更好地解决问题。
109 0