更新:
我注意到您在示例项目中使用的是 Spring Boot 1.4.3,并且您的项目设置不正确。 Spring Boot 2.0.x 的情况有所不同,见下文。
您的示例项目设置存在问题
您使用的是hystrix-servo-metrics-publisher 版本1.5.9。此版本与 Spring Cloud Brixton.SR5 使用的 Hystrix 版本 1.5.3 不兼容。您可以通过启动您的应用程序来观察不兼容性,调用http://localhost:8080/remotecall(创建初始Hystrix 指标),然后调用http://localhost:8080/metrics(访问执行器端点)。然后你会得到一个java.lang.NoSuchMethodError: com.netflix.hystrix.HystrixThreadPoolProperties.maximumSize()Lcom/netflix/hystrix/strategy/properties/HystrixProperty;。将版本设置为 1.5.3 可解决此问题。
Spring Boot 1.x 解决方案
JMX 指标在 Actuator 中不再可见的原因是由 ServoMetricsAutoConfiguration 引起的,如果使用 Spring Boot Actuator 则会激活。这里公开了一个监视器注册 bean,其配置依赖于 SpringMetricsConfigBean。新的默认注册表类是BasicMonitorRegistry。
要恢复原始默认 JMX 监视器注册表,请添加 resources/application.properties 和 netflix.metrics.servo.registryClass=com.netflix.servo.jmx.JmxMonitorRegistry 行。
通过这种方式,指标通过 JMX 和 Spring Actuator 指标端点公开。
Spring Boot 2.0.x 解决方案
对于 Spring Boot 2,问题有所不同。我将问题追踪到io.micrometer.core.instrument.binder.hystrix.HystrixMetricsBinder(micrometer 是spring-boot-actuator-autoconfigure 的依赖项)。在这里,当MetricsAutoConfiguration 类被触发时,任何现有的发布者都会被MicrometerMetricsPublisher 替换。因此,HystrixPlugins.getInstance().registerMetricsPublisher(HystrixServoMetricsPublisher.getInstance()); 语句没有达到预期的效果。发布者被简单地丢弃了......
Hystrix 插件的问题是每个插件类型一次只能注册一个插件。因此,一种解决方案是用“元”插件替换现有插件,该插件委托给多个插件实例。这种方法也用于HystrixSecurityAutoConfiguration。通过下面的配置类,我设法通过 JMX 和 Spring Boot Actuator(例如 /actuator/metrics/hystrix.execution)公开了 Hystrix 指标:
import com.netflix.hystrix.*;
import com.netflix.hystrix.contrib.servopublisher.HystrixServoMetricsPublisher;
import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier;
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher;
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisherCollapser;
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisherCommand;
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisherThreadPool;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
@ConditionalOnClass({Hystrix.class, HystrixServoMetricsPublisher.class})
@AutoConfigureAfter(MetricsAutoConfiguration.class)
public class HystrixServoAndMicrometerConfig {
@PostConstruct
public void init() {
// Keeps references of existing Hystrix plugins
HystrixMetricsPublisher existingMetricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();
HystrixConcurrencyStrategy concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance().getPropertiesStrategy();
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance().getCommandExecutionHook();
if (existingMetricsPublisher != null) {
HystrixPlugins.reset();
// Registers existing plugins except the new ServoAndExistingMetricsPublisher plugin
HystrixPlugins.getInstance().registerMetricsPublisher(new ServoAndExistingMetricsPublisher(
existingMetricsPublisher, HystrixServoMetricsPublisher.getInstance()));
HystrixPlugins.getInstance().registerConcurrencyStrategy(concurrencyStrategy);
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);
} else {
HystrixPlugins.getInstance().registerMetricsPublisher(HystrixServoMetricsPublisher.getInstance());
}
}
private static class ServoAndExistingMetricsPublisher extends HystrixMetricsPublisher {
private static class ServoAndOtherMetricsPublisherCommand implements HystrixMetricsPublisherCommand {
private final HystrixMetricsPublisherCommand servoMetricsPublisherCommand;
private final HystrixMetricsPublisherCommand existingMetricsPublisherCommand;
ServoAndOtherMetricsPublisherCommand(HystrixMetricsPublisherCommand servoMetricsPublisherCommand,
HystrixMetricsPublisherCommand existingMetricsPublisherCommand) {
this.servoMetricsPublisherCommand = servoMetricsPublisherCommand;
this.existingMetricsPublisherCommand = existingMetricsPublisherCommand;
}
@Override
public void initialize() {
servoMetricsPublisherCommand.initialize();
existingMetricsPublisherCommand.initialize();
}
}
private final HystrixMetricsPublisher existingMetricsPublisher;
private final HystrixMetricsPublisher servoMetricsPublisher;
ServoAndExistingMetricsPublisher(HystrixMetricsPublisher existingMetricsPublisher,
HystrixMetricsPublisher servoMetricsPublisher) {
this.existingMetricsPublisher = existingMetricsPublisher;
this.servoMetricsPublisher = servoMetricsPublisher;
}
@Override
public HystrixMetricsPublisherCommand getMetricsPublisherForCommand(HystrixCommandKey commandKey, HystrixCommandGroupKey commandGroupKey, HystrixCommandMetrics metrics, HystrixCircuitBreaker circuitBreaker, HystrixCommandProperties properties) {
HystrixMetricsPublisherCommand servoMetricsPublisherCommand = servoMetricsPublisher.getMetricsPublisherForCommand(commandKey, commandGroupKey, metrics, circuitBreaker, properties);
HystrixMetricsPublisherCommand existingMetricsPublisherCommand = existingMetricsPublisher.getMetricsPublisherForCommand(commandKey, commandGroupKey, metrics, circuitBreaker, properties);
return new ServoAndOtherMetricsPublisherCommand(servoMetricsPublisherCommand, existingMetricsPublisherCommand);
}
@Override
public HystrixMetricsPublisherThreadPool getMetricsPublisherForThreadPool(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolMetrics metrics, HystrixThreadPoolProperties properties) {
return servoMetricsPublisher.getMetricsPublisherForThreadPool(threadPoolKey, metrics, properties);
}
@Override
public HystrixMetricsPublisherCollapser getMetricsPublisherForCollapser(HystrixCollapserKey collapserKey, HystrixCollapserMetrics metrics, HystrixCollapserProperties properties) {
return servoMetricsPublisher.getMetricsPublisherForCollapser(collapserKey, metrics, properties);
}
}
}