微服务熔断限流Hystrix之流聚合

简介

上一篇介绍了 Hystrix Dashboard 监控单体应用的例子,在生产环境中,监控的应用往往是一个集群,我们需要将每个实例的监控信息聚合起来分析,这就用到了 Turbine 工具。Turbine有一个重要的功能就是汇聚监控信息,并将汇聚到的监控信息提供给Hystrix Dashboard来集中展示和监控。

流程

实验

工程说明

工程名端口作用
eureka-server8761注册中心
service-hi8762服务提供者
service-consumer8763服务消费者
service-turbine8765Turbine服务

核心代码

eureka-server 、service-hi、service-consumer 工程代码与上一节 微服务熔断限流Hystrix之Dashboard 相同,下面是 service-turbine 工程的核心代码。

pom.xml
<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-turbine</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>
复制代码
application.yml
server:
  port: 8765

spring:
  application:
    name: service-turbine
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

turbine:
  app-config: service-consumer
  cluster-name-expression: new String("default")
  combine-host-port: true
复制代码

参数说明:

  • turbine.app-config:指定要监控的应用名
  • turbine.cluster-name-expression:指定集群的名字
  • turbine.combine-host-port:表示同一主机上的服务通过host和port的组合来进行区分,默认情况下是使用host来区分,这样会使本地调试有问题
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableTurbine
public class ServiceTurbineApplication {

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

}
复制代码

模拟多实例

启动多个 service-consumer 工程,来模拟多实例,可以通过命令java -jar service-consumer.jar --server.port=XXXX 来实现。

为了方便,在编辑器中实现启动工程。但 idea 不支持单个应用的多次启动, 需要开启并行启动:

选择 “Edit Configurations...”

勾选 “Allow running in parallel”

测试

启动工程,访问 http//localhost:8763/hi , http//localhost:8764/hi , http//localhost:8763/oh , http//localhost:8764/oh,来产生测试数据。

访问 http://localhost:8765/hystrix

输入监控流地址 http://localhost:8765/turbine.stream ,点击 Monitor Stream 进入监控页面

可以看到聚合了两个实例的 Hystrix dashbord 数据。

源码

github.com/gf-huanchup…

欢迎扫码或微信搜索公众号《程序员果果》关注我,关注有惊喜~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值