189 8069 5689

SpringCloud之熔断监控HystrixDashboard的实现

前言

专注于为中小企业提供做网站、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业兴宁免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

SpringCloud 是微服务中的翘楚,最佳的落地方案。

SpringCloud 中的 Hystrix 组件可以实现熔断,而在实际情况中,一般还需要直观地看到各个服务的调用情况,

这时,就用到了 SpringCloud 另一个组件:Hystrix Dashboard。

Hystrix Dashboard 是一款针对于 Hystrix 进行实时监控的工具,还提供了友好的图形化界面。

源码

GitHub地址:https://github.com/intomylife/SpringCloud

  • JDK 1.8.0 +
  • Maven 3.0 +
  • SpringBoot 2.0.3
  • SpringCloud Finchley.RELEASE

开发工具

IntelliJ IDEA

正文

commons 工程

commons 工程 - POM 文件

<?xml version="1.0" encoding="UTF-8"?>

  4.0.0
 
  
  com.zwc
  springcloud-dashboard-commons
  1.0
 
  
  springcloud-dashboard-commons
  公用工程
 
  
  jar
 
  
  
    
    UTF-8
    
    1.8
 
    
    Cairo-SR3
 
    
    Finchley.RELEASE
  
 
  
  
 
  
 
  
  
  
  
  
    
      
      
        io.spring.platform
        platform-bom
        ${platform-bom.version}
        pom
        import
      
      
      
        org.springframework.cloud
        spring-cloud-dependencies
        ${spring-cloud-dependencies.version}
        pom
        import
      
    
  
 
  
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  
 

配置一些共用依赖

commons 工程 - 项目结构

SpringCloud之熔断监控Hystrix Dashboard的实现

service 工程

  ① 此工程下有三个模块:一个注册中心以及服务 A、B

  ② A 提供服务并且调用服务 B 以及 B 提供服务

registry-service(注册中心)

registry-service - POM 文件

<?xml version="1.0" encoding="UTF-8"?>

  4.0.0
 
  
  
    com.zwc
    springcloud-dashboard-service
    1.0
  
 
  
  com.zwc
  springcloud-dashboard-registry-service
  1.0
 
  
  springcloud-dashboard-registry-service
  注册中心
 
  
  jar
 
  
  
 
  
 
  
  
    
    
      com.zwc
      springcloud-dashboard-commons
      1.0
    
 
    
    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-server
    
  
 
  
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  
 

主要是加入 spring-cloud-starter-netflix-eureka-server 依赖

registry-service - application.yml 配置文件

# 端口
server:
 port: 8761
 
# 应用名称
spring:
 application:
  name: eureka-server
 
eureka:
 instance:
  # 使用 ip 代替实例名
  prefer-ip-address: true
  # 实例的主机名
  hostname: ${spring.cloud.client.ip-address}
  # 实例的 ID 规则
  instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
 client:
  # 是否向注册中心注册自己
  registerWithEureka: false
  # 是否向注册中心获取注册信息
  fetchRegistry: false
  serviceUrl:
   # 注册中心地址
   defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

这里使用了默认的 8761 端口,当然也可以更改,不过在发现调用服务端的注册中心地址端口要与它一致

registry-service - 启动类

package com.zwc;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudDashboardRegistryServiceApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(SpringcloudDashboardRegistryServiceApplication.class, args);
  }
 
}

在启动类中添加 @EnableEurekaServer 注解表示此工程是注册中心registry-service - 启动项目

. 项目启动成功后访问 http://localhost:8761/ 即可看到 eureka-server 主页面

SpringCloud之熔断监控Hystrix Dashboard的实现

服务工程 A(提供者和消费者)

服务工程 A - POM 文件

<?xml version="1.0" encoding="UTF-8"?>

  4.0.0
 
  
  
    com.zwc
    springcloud-dashboard-a-service
    1.0
  
 
  
  com.zwc
  springcloud-dashboard-a-service-core
  1.0
 
  
  springcloud-dashboard-a-service-core
  服务工程 - A 核心
 
  
  jar
 
  
  
 
  
 
  
  
    
    
      com.zwc
      springcloud-dashboard-commons
      1.0
    
 
    
    
      com.zwc
      springcloud-dashboard-a-service-api
      1.0
    
 
    
    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-client
    
    
    
      org.springframework.cloud
      spring-cloud-starter-openfeign
    
    
    
      org.springframework.boot
      spring-boot-starter-actuator
    
    
    
      org.springframework.cloud
      spring-cloud-starter-netflix-hystrix
    
    
    
      org.springframework.cloud
      spring-cloud-starter-netflix-hystrix-dashboard
    
  
 
  
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  
 
  • 加入 spring-cloud-starter-netflix-eureka-client 依赖:提供和注册服务
  • 加入 Feign 的起步依赖 spring-cloud-starter-openfeign:消费服务
  • 加入依赖 spring-boot-starter-actuator:开启并配置端点
  • 加入依赖 spring-cloud-starter-netflix-hystrix:熔断器
  • 加入依赖 spring-cloud-starter-netflix-hystrix-dashboard:熔断监控

服务工程 A - application.yml 配置文件

# 端口
server:
 port: 8090
 
# 应用名称
spring:
 application:
  name: dashboard-a
 
eureka:
 instance:
  # 使用 ip 代替实例名
  prefer-ip-address: true
  # 实例的主机名
  hostname: ${spring.cloud.client.ip-address}
  # 实例的 ID 规则
  instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
 client:
  serviceUrl:
   # 注册中心地址
   defaultZone: http://${eureka.instance.hostname}:8761/eureka/
 
management:
 endpoints:
  web:
   exposure:
    # 开启监控端点
    include: hystrix.stream
   base-path: /
  • 注意此处配置注册中心地址的端口为 8761 也就是上面注册中心工程配置的端口
  • spring.application.name:应用名称,被消费者调用时需要用到,它在消费的同时也可以被消费
  • 另一个服务工程 B 配置与此类似,只是端口不一样,不再赘述
  • 在 2.0 后需要主动开启端点 hystrix.stream,否则 404

服务工程 A -  application.properties(注意)

# 开启断路器
feign.hystrix.enabled=true
  • 断路器要主动开启,服务调用失败时才会熔断
  • 此处有一个坑,把此配置写到 yml 中熔断不会生效
  • application.properties 和 bootstrap.yml 二选一就行

服务工程 A -  bootstrap.yml(注意)

feign:
 hystrix:
  # 开启断路器
  enabled: true
  • 断路器要主动开启,服务调用失败时才会熔断
  • 此处有一个坑,把此配置写到 application.yml 中熔断不会生效
  • application.properties 和 bootstrap.yml 二选一就行

服务工程 A - controller 前端控制器(提供服务)

package com.zwc.a.controller;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/*
 * @ClassName ASayHelloController
 * @Desc TODO  Say Hello
 * @Date 2019/5/20 23:24
 * @Version 1.0
 */
@RestController
public class ASayHelloController {
 
  /*
   * @ClassName ASayHelloController
   * @Desc TODO  读取配置文件中的端口
   * @Date 2019/5/20 23:24
   * @Version 1.0
   */
  @Value("${server.port}")
  private String port;
 
  /*
   * @ClassName ASayHelloController
   * @Desc TODO  Say Hello
   * @Date 2019/5/20 23:24
   * @Version 1.0
   */
  @RequestMapping("/a")
  public String a(){
    return "Hello!I'm a. port:" + port;
  }
 
}

提供一个服务:输出 Hello 和端口

服务工程 A - 服务调用

package com.zwc.a.api.feign;
 
import com.zwc.a.api.impl.FeignApiFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
 
/*
 * @ClassName FeignApi
 * @Desc TODO  使用 Feign 调用 b - 接口
 * @Date 2019/5/20 23:21
 * @Version 1.0
 */
@FeignClient(value = "dashboard-b" , fallback = FeignApiFallBack.class)
public interface FeignApi {
 
  /*
   * @ClassName FeignApi
   * @Desc TODO  通过 dashboard-b 服务名调用 b() 方法
   * @Date 2019/5/20 23:21
   * @Version 1.0
   */
  @RequestMapping("/b")
  String b();
 
}
  • 通过 @FeignClient 注解中 value = "dashboard-b" 来指定调用哪个服务
  • dashboard-b 就是提供者的 spring.application.name:应用名称
  • 通过 @FeignClient 注解中 fallback = FeignApiFallBack.class 来指定熔断时调用的方法
  • FeignApiFallBack 就是此类(FeignApi)的实现类,对应的实现方法就是此类的熔断时调用的方法
  • b():此方法是 B 工程中提供的服务,在这里定义成接口
  • 注意要与提供者具有相同返回值,相同方法名以及相同参数

服务工程 A - Fallback(FeignApiFallBack)

package com.zwc.a.api.impl;
 
import com.zwc.a.api.feign.FeignApi;
import org.springframework.stereotype.Component;
 
/*
 * @ClassName FeignApi
 * @Desc TODO  fallback
 * @Date 2019/5/20 23:21
 * @Version 1.0
 */
@Component
public class FeignApiFallBack implements FeignApi {
 
  /*
   * @ClassName FeignApiFallBack
   * @Desc TODO  调用 dashboard-b 服务中的 b() 方法失败时执行
   * @Date 2019/5/20 23:31
   * @Version 1.0
   */
  @Override
  public String b() {
    return "Hello!aUseB fail";
  }
}

使用 @Component 注解把此类交给 Spring 管理

实现了 FeignApi 接口,提供熔断时对应的方法

服务工程 A - controller 前端控制器(消费服务)

package com.zwc.a.controller;
 
import com.zwc.a.api.feign.FeignApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/*
 * @ClassName AUseBFeignController
 * @Desc TODO  使用 Feign 调用 b - 前端控制器
 * @Date 2019/5/20 23:23
 * @Version 1.0
 */
@RestController
public class AUseBFeignController {
 
  @Autowired(required = false)
  private FeignApi feignApi;
 
  /*
   * @ClassName FeignController
   * @Desc TODO  通过 dashboard-b 服务名调用 b() 方法
   * @Date 2019/5/20 23:13
   * @Version 1.0
   */
  @RequestMapping("/aUseB")
  public String aUseB(){
    return feignApi.b();
  }
 
}

使用 @Autowired 注解装配 Bean,通过此 Bean 中的方法调用服务

此类对外暴露接口,调用的实则是提供者的服务

服务工程 A - 启动类

package com.zwc;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
 
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
public class SpringcloudDashboardAServiceCoreApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(SpringcloudDashboardAServiceCoreApplication.class, args);
  }
 
}
  • 添加 @EnableEurekaClient 注解表示此工程可以向注册中心提供服务
  • 添加 @EnableFeignClients 注解表示开启 Feign 功能进行远程调用
  • 添加 @EnableHystrix 注解表示开启熔断器
  • 添加 @EnableHystrixDashboard 注解表示开启熔断监控

服务工程 A - 启动项目

1. 项目启动成功后访问:http://localhost:8090/hystrix 可以看到

  SpringCloud之熔断监控Hystrix Dashboard的实现

2. 在中间的输入框中输入:http://127.0.0.1:8090/hystrix.stream ,点击 Monitor Stream 按钮可以看到

SpringCloud之熔断监控Hystrix Dashboard的实现

  3. 此时还未调用服务,所以一直显示 'Loading ...'

  4. 另起页面访问:http://localhost:8090/a (调用自己的服务)

  5. 输出内容:'Hello!I'm a. port:8090'

  6. 这是熔断监控页面还是不会有所改变,因为在调用别人的服务时才会有相关数据

  7. 访问地址:http://localhost:8090/aUseB (调用 B 工程的服务)

  8. 输出内容:'Hello!aUseB fail' (此时因为 B 工程还未启动,所以调用了 fallback 中的方法)

  9. 再观察步骤 2 中的熔断监控页面,可以看到

  SpringCloud之熔断监控Hystrix Dashboard的实现

 10. 启动服务工程 B,项目启动成功后再次访问:http://localhost:8090/aUseB (调用 B 工程的服务)

 11. 输出内容:'Hello!I'm b. port:8091' (如果还未调用成功,等待一会再刷新试试)

 12. 再次观察步骤 2 中的熔断监控页面,可以看到

SpringCloud之熔断监控Hystrix Dashboard的实现

 13. 红色的百分比表示失败率

 14. 左边的六个数字对应着页面右上角的六个状态(Success、Short-Circuited..) 

service 工程 - 项目结构

SpringCloud之熔断监控Hystrix Dashboard的实现

把多工程项目使用 IntelliJ IDEA  打开

  • 把项目从 GitHub 中下载到你的本地
  • 打开 IntelliJ IDEA 
  • 点击 File -> Open
  • 打开你下载到本地的项目目录
  • springcloud-dashboard -> springcloud-dashboard-service(选择打开此工程)
  • 打开 service 工程后
  • 再次点击 File -> Project Structrue
  • 选择 Modules,点击 '+' 符号
  • 点击 Import  Module
  • 还是打开你下载到本地的项目目录
  • springcloud-dashboard -> springcloud-dashboard-commons -> pom.xml
  • 点击 OK
  • 点击 Next,Finish
  • 点击 Apply,OK

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前题目:SpringCloud之熔断监控HystrixDashboard的实现
本文网址:http://cdxtjz.cn/article/pspojj.html

其他资讯