我使用spring 1.4.3与Netflix一起使用metrics,并通过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伺服度量-publisher 1.5.9
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-servo-metrics-publisher</artifactId>
<version>1.5.9</version>
</dependency>hystrix伺服度量-发行者是容易使用的。只要提供一个带有一行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执行器。在添加依赖项之后
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>hystrix伺服度量-发行者不再工作了。com.netlix.servo包在JMX /attributes中不可用。
下面是一个小示例项目,它具有禁用的spring-boot-starter驱动器依赖关系:hystrix.伺服测量.publisher-jmx-示例
如果启用了依赖项,hystrix伺服度量-publisher就不再工作了。
发布于 2018-07-10 07:38:27
更新:我注意到您在示例项目中使用SpringBoot1.4.3,并且您的项目设置是不正确的。SpringBoot2.0.x的情况不同,见下文。
示例项目设置的问题
您正在使用hystrix-servo-metrics-publisher版本的1.5.9。此版本与Spring 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解决了此问题。
SpringBoot1.x的解决方案
JMX指标在Actuator中不再可见的原因是由ServoMetricsAutoConfiguration引起的,如果使用Spring,就会激活它。在这里,将公开一个监视器注册表bean,其配置依赖于一个SpringMetricsConfigBean。新的默认注册表类是BasicMonitorRegistry。
要获得原始的默认JMX监视器注册表,请添加一个带有行resources/application.properties的netflix.metrics.servo.registryClass=com.netflix.servo.jmx.JmxMonitorRegistry。这种方式通过JMX和度量端点公开度量。
SpringBoot2.0.x的解决方案
对于Spring 2,问题是不同的。我将问题追溯到io.micrometer.core.instrument.binder.hystrix.HystrixMetricsBinder (micrometer是spring-boot-actuator-autoconfigure的依赖项)。在这里,当触发MicrometerMetricsPublisher类时,任何现有的发布服务器都会被MetricsAutoConfiguration替换。因此,语句HystrixPlugins.getInstance().registerMetricsPublisher(HystrixServoMetricsPublisher.getInstance());没有达到预期的效果。出版商被抛弃了..。
Hystrix插件的问题是,每个插件类型一次只能注册一个插件。因此,解决方案是用委托给多个插件实例的"meta"-plugin替换现有的插件。这种方法也在HystrixSecurityAutoConfiguration中使用。使用下面的config类,我设法通过JMX和Spring (例如/驱动器/度量/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);
}
}
}https://stackoverflow.com/questions/42509711
复制相似问题