Spring Cloud 入门——2.2 Hystrix 熔断器添加数据监控

24 篇文章 0 订阅
22 篇文章 3 订阅

代码信息

本篇文章涉及代码版本

组件版本
Spring Boot2.0.8.RELEASE
Spring CloudFinchley.SR1

本篇文章涉及应用

应用说明
base-eureka服务发现
base-hystrix-dashboard添加了数据监控依赖的 熔断器

为Hystrix添加数据监控功能

为了更方便的监视Hystrix的运行情况,Hystrix提供了一个组件来监控请求数据,并且提供了数据的可视化组件。hystrix-dashboard

构建maven依赖

想要开启此功能需要引入下面的内容

    <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>

注意下面依赖在父项目中已经被引入,所以可以不需要引入

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

参数配置application.yml

spring:
  application:
    name: base-hystrix-dashboard
server:
  port: 8405

eureka:
  client:
    service-url: 
      defaultZone: http://localhost:8000/eureka/
logging:
  file: ${spring.application.name}.log
  # 配置监控端点
management:
  endpoints:
    web:
      exposure:
        # 默认配置中只有"health","info"
        include: ["health","info","hystrix.stream"]

上面management.endpoints.web.exposure.include不添加hystrix.stream查看数据的时候会出现下面情况:Unable to connect to Command Metric Stream.

在这里插入图片描述

代码编写

启动类

@EnableEurekaClient
@SpringBootApplication
@EnableCircuitBreaker
@EnableHystrixDashboard
public class HystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixApplication.class, args);
    }
}

启动类中除了之前的注解需要添加@EnableHystrixDashboard

模拟请求的代码

@Log
@Service
public class ClientService {

    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;

    /**
     * 配置降级方法
     * @return
     */
    @HystrixCommand(fallbackMethod = "getServiceBack")
    public String getService() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("base-producer");
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/getService";
        log.info(url);
        return restTemplate.postForObject(url,null, String.class);
    }


    public String getServiceBack() {
        return "use fallbackMethod";
    }
    
}

上面内容完成后启动服务中心以及服务提供项目和本项目

配置监控地址

进入地址http://localhost:8405/hystrix可以看到下面内容
在这里插入图片描述
此时有几个需要注意的;

  1. 我们现在因为没有引入turbine所以只能进行单一应用进行监控,无法对集群进行监控。
  2. 另外需要注意的在springboot1.X的时候,这里我们监控地址是http://:/hystrix.stream,spring boot2.0之后使用 http://:/actuator/hystrix.stream的地址

此时填写需要监控的地址如:http://localhost:8405/actuator/hystrix.stream会进入下面内容

在这里插入图片描述
然后我们尝试不停的请求http://localhost:8405/hystrixGetService地址查看数据变化。

在这里插入图片描述

关于这些内容的意思我就直接引用大神,程序猿DD的图了

在这里插入图片描述

本篇文章涉及的源码下载地址:https://gitee.com/daifylearn/cloud-learn

ps.上述的所有项目都是可以成功运行的。但是在后期为了实现每个应用端口尽量不冲突会有些许调整,而后续某次作死调整结构和名称可能会导致部分项目无法运行o(╯□╰)o,如果发现请留言我进行修改。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大·风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值