我使用Spring-boot 1.4.3.RELEASE和Netflix Hystrix,我将通过JMX提供Hystrix指标。 Hystrix通过此代码段包含在项目中
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
对于通过JMX的指标,我使用hystrix-servo-metrics-publisher 1.5.9
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-servo-metrics-publisher</artifactId>
<version>1.5.9</version>
</dependency>
hystrix-servo-metrics-publisher易于使用。它足以提供一个单行的静态块
HystrixPlugins.getInstance().registerMetricsPublisher(HystrixServoMetricsPublisher.getInstance());
如图所示
@EnableCircuitBreaker
@SpringBootApplication
public class ExampleApplication extends SpringBootServletInitializer {
static {
HystrixPlugins.getInstance().registerMetricsPublisher(HystrixServoMetricsPublisher.getInstance());
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ExampleApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}
效果很好。
但我们的项目还需要Spring Boot Actuator。添加依赖项
之后<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
hystrix-servo-metrics-publisher不再起作用了。包com.netlix.servo
在JMX MBeans / attributes中不可用。
这是一个小示例项目,其中禁用了spring-boot-starter-actuator依赖项: hystrix-servo-metrics-publisher-jmx-example
如果依赖项已启用,则hystrix-servo-metrics-publisher不再有效。
答案 0 :(得分:0)
更新: 我注意到您在示例项目中使用Spring Boot 1.4.3,并且您的项目设置不正确。对于Spring Boot 2.0.x,情况有所不同,请参见下文。
示例项目设置问题
您正在使用hystrix-servo-metrics-publisher
版本1.5.9
。该版本与Spring Cloud 1.5.3
使用的Hystrix版本Brixton.SR5
不兼容。您可以通过启动应用程序,调用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的解决方案
使用ServoMetricsAutoConfiguration
导致JMX度量标准在Actuator中不再可见的原因,如果使用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
的依赖项)。在此,当触发MicrometerMetricsPublisher
类时,任何现有的发布者都将被MetricsAutoConfiguration
替换。因此,语句HystrixPlugins.getInstance().registerMetricsPublisher(HystrixServoMetricsPublisher.getInstance());
并没有达到预期的效果。发布者只是被丢弃...
Hystrix插件的问题在于,每种插件类型一次只能注册一个插件。因此,一种解决方案是用委派给多个插件实例的“元”插件替换现有插件。 HystrixSecurityAutoConfiguration
中也使用了这种方法。通过下面的配置类,我设法通过JMX和Spring Boot Actuator公开了Hystrix指标(例如/actuator/metrics/hystrix.execution):
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);
}
}
}