Hystrix仪表盘监控Hystrix Dashboard
目录
Hystrix仪表盘监控介绍
Hystrix仪表盘( Hystrix Dashboard),就像汽车的仪表盘实时显示汽车的各项数据一样, Hystrix仪表盘主要用来监控 Hystrix的实时运行状态,通过它我们可以看到HystriX的各项指标信息,从而快速发现系统中存在的问题进而解决要使用Hystriⅸ仪表盘功能,我们首先需要有一个Hystrix Dashboard,这个功能我们可以在原来的消费者应用上添加,让原来的消费者应用具备Hystrix仪表 盘功能,但一般地微服务架构思想是推崇服务的拆分, Hystrix Dashboard也是一个服务,所以通常会单独创建一个新的工程专门用做Hystrix Dashboard服务。
HystrixDashboard
创建工程HystrixDashboard,引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
在启动类添加@EnableHystrixDashboard注解,并且手动添加启动参数关闭版本检查等。
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
public static void main(String[] args) {
System.setProperty("spring.cloud.compatibility-verifier.enabled", "false");
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
由于springboot2.4.x以后,没有ConfigurationBeanFactoryMetadata类,容易启动项目报错,这里我们创建org.springframework.boot.context.properties包,手动添加这个类:
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.context.properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* Utility class to memorize {@code @Bean} definition metadata during initialization of
* the bean factory.
*
* @author Dave Syer
* @since 1.1.0
* @deprecated since 2.2.0 for removal in 2.4.0 in favor of
* {@link ConfigurationPropertiesBean}
*/
@Deprecated
public class ConfigurationBeanFactoryMetadata implements ApplicationContextAware {
/**
* The bean name that this class is registered with.
*/
public static final String BEAN_NAME = ConfigurationBeanFactoryMetadata.class.getName();
private ConfigurableApplicationContext applicationContext;
public <A extends Annotation> Map<String, Object> getBeansWithFactoryAnnotation(Class<A> type) {
Map<String, Object> result = new HashMap<>();
for (String name : this.applicationContext.getBeanFactory().getBeanDefinitionNames()) {
if (findFactoryAnnotation(name, type) != null) {
result.put(name, this.applicationContext.getBean(name));
}
}
return result;
}
public <A extends Annotation> A findFactoryAnnotation(String beanName, Class<A> type) {
Method method = findFactoryMethod(beanName);
return (method != null) ? AnnotationUtils.findAnnotation(method, type) : null;
}
public Method findFactoryMethod(String beanName) {
ConfigurableListableBeanFactory beanFactory = this.applicationContext.getBeanFactory();
if (beanFactory.containsBeanDefinition(beanName)) {
BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName);
if (beanDefinition instanceof RootBeanDefinition) {
return ((RootBeanDefinition) beanDefinition).getResolvedFactoryMethod();
}
}
return null;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = (ConfigurableApplicationContext) applicationContext;
}
static void register(BeanDefinitionRegistry registry) {
if (!registry.containsBeanDefinition(BEAN_NAME)) {
GenericBeanDefinition definition = new GenericBeanDefinition();
definition.setBeanClass(ConfigurationBeanFactoryMetadata.class);
definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(ConfigurationBeanFactoryMetadata.BEAN_NAME, definition);
}
}
}
修改配置文件:
server:
port: 7006
hystrix:
dashboard:
proxyStreamAllowList: "127.0.0.1"
启动项目,访问http://127.0.0.1:7006/hystrix,就成功了,红圈部分是我们要监控的url地址:
然后我们需要监控EurekaTest,首先添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
然后启动类添加:
@EnableEurekaClient
@EnableCircuitBreaker
@SpringBootApplication
public class EurekaTestApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaTestApplication.class, args);
}
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
修改配置文件:
server:
port: 7003
spring:
application:
name: EurekaTest
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
instance:
lease-renewal-interval-in-seconds: 1 #间隔1s,向服务端发送一次心跳,证明自己依然“存活”
lease-expiration-duration-in-seconds: 2 # 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,请将我踢掉
#用来暴露endpoints的,由于endpoints中会包含很多敏感信息,
#除了health和info两个支持直接访问外,其他的默认不能直接访问,
#所以我们让他们都能访问(*),或者指定springboot的监控端点访问权限,
#*表示所有的端点都允许访问,如果只写hystrix.stream,他会把默认的info,health端点关闭
management:
endpoints:
web:
exposure:
include: '*'
最后启动所有项目,先访问我们熔断限制的接口:127.0.0.1:7003/test/hello,然后再访问127.0.0.1:7003/actuator/hystrix.stream,会显示如下内容:
如果不先访问熔断限流的接口,则会一直ping,ping···,然后再仪表盘把127.0.0.1:7003/actuator/hystrix.stream接口放进去,会显示监控效果。
原文地址:https://blog.csdn.net/weixin_55987175/article/details/145098410
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!