spring cloud 断路器监控-Hystrix Dashboard

阅读数:69 评论数:0

跳转到新版页面

分类

python/Java

正文

Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图表化界面。

修改service-hi

1、在pom工程文件引入相应的依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        
    </dependencies>

2、在程序的入口ServiceHiApplication类,加上@EnableHystrix注解开启断路器,这个必须的,并且需要在程序中声明断点HystrixCommand,加上@EnableHystrixDashboard注解,开启HystrixDashboard。

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceHiApplication {
 
    /**
     * 访问地址 http://localhost:8762/actuator/hystrix.stream
     * @param args
     */
 
    public static void main(String[] args) {
        SpringApplication.run( ServiceHiApplication.class, args );
    }
 
    @Value("${server.port}")
    String port;
 
    @RequestMapping("/hi")
    @HystrixCommand(fallbackMethod = "hiError")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }
 
    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }
 
}

4、application.yml

server:
  port: 8762
  application:
    name: service-hi
 
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

4、运行程序,依次开启eureka-server和service-hi

5、打开http://localhost:8762/actuator/hystrix.stream,可以看到一些具体的数据。

6、打开localhost:8762/hystrix,可以看见下界面。在界面依次输入http://localhost:8762/actuator/hystrix.stream、2000、miya点击确定。

7、在另一个窗口输入http://localhost:8762/hi?name=xxx,重新刷新hystrix.stream网页。

zuul+hystrix 

zuul本身就集成了Hystrix,实际上Zuul的路由转发也是用到了Ribbon+Hystrix,也就意味着我们可以通过Hystrix Dashboard监控zuul的工作情况。

方式一:开启Hystrix.stream入口

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class HystrixConfiguration {
 
    @Bean(name = "hystrixRegistrationBean")
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean registration = new ServletRegistrationBean(
                new HystrixMetricsStreamServlet(), "/hystrix.stream");
        registration.setName("hystrixServlet");
        registration.setLoadOnStartup(1);
        return registration;
    }
 
    @Bean(name = "hystrixForTurbineRegistrationBean")
    public ServletRegistrationBean servletTurbineRegistrationBean() {
        ServletRegistrationBean registration = new ServletRegistrationBean(
                new HystrixMetricsStreamServlet(), "/actuator/hystrix.stream");
        registration.setName("hystrixForTurbineServlet");
        registration.setLoadOnStartup(1);
        return registration;
    }
}

配置完成后重启zuul,访问http://localhost:port/hystrix.stream或者http://localhost:port/actuator/hystrix.stram就可以看到Hystrix流信息(之所以配置两个入口,是为了满足turbine需要)。

这时候我们可以通过已有看板以链接方式查看Hystrix Dashboard,或者可以用Turbine来聚合Hystrix数据了。

方式二:引入Hystrix Dashboard

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
 
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

加入@EnableHystrixDashboard注解 ,

management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream # 或者'*'

在Dashboard中通过/actuator/hystrix.stream接口监控。

重新启动服务实例后再次访问,页面变成了Loading,是因为需要调用任意hystrix接口,不然没有hystrix调用,访问hystrix.stream会一直ping, hystrix监控界面就会一直loading。




相关推荐

ZuulProxyAutoConfiguration 首先我们看一下zuul的配置类ZuulProxyAutoConfiguration, 这个类有一项工作是初始化Zuul默认

Tomcat Tomcat的最大并发数是可以配置的,实际运用中,最大并发数与硬件性能有很大关系的。Tomcat默认的HTTP实现是采用阻塞式的Socket通信,每个请求都需要创

Eureka Server在运行期间会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85

mvn依赖 &lt;dependency&gt; &lt;groupId

什么是jwt (json web token)jwt是一生中用来在网络上声明某种身份的令牌(TOKEN),它的特点是紧凑且自包含并且基于JSON,通过一些常用的算法对包含的主体

简介 Spring cloud Sleuth主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持zipkin,你只需要在pom文件中引入相应的依赖即可。 1、

简介 在spring cloud中,有分布式配置中心组件spring cloud config,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程git仓库中,在该组

为了保证高可用性,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若大量的请求涌入,Servlet容器的线

一、JAVA项目中网络接口调用工具 1、HttpClient 它是Apache Jakarta Common下的子项目,用来提供高效、最新的、功能丰富的支持Http协议的客户端编程工具包。 HttpC

在网络请求时,可能会出现异常请求,如果还想在异常情况下使系统可用,那么就需要容错处理。 Spring Cloud Feigh就是通过Fallback实现的,有两种方式: <