spring cloud 断路器监控-Hystrix Dashboard

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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值