本机IP为  192.168.1.102

一、搭建Hystrix Dashboard

1.   新建 Maven 项目  hystrix-dashboard

2. pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.  
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.java</groupId>
  8. <artifactId>hystrix-dashboard</artifactId>
  9. <version>1.0.0-SNAPSHOT</version>
  10. <name>${project.artifactId}</name>
  11.  
  12. <!-- 配置版本常量 -->
  13. <properties>
  14. <jdk.version>1.8</jdk.version>
  15. <spring.cloud.version>2.0.0.RELEASE</spring.cloud.version>
  16. </properties>
  17.  
  18. <parent>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-parent</artifactId>
  21. <version>2.0.5.RELEASE</version>
  22. </parent>
  23.  
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  32. <version>${spring.cloud.version}</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  37. <version>${spring.cloud.version}</version>
  38. </dependency>
  39.  
  40. </dependencies>
  41.  
  42. <build>
  43. <finalName>${project.artifactId}</finalName>
  44. <plugins>
  45. <plugin>
  46. <groupId>org.apache.maven.plugins</groupId>
  47. <artifactId>maven-compiler-plugin</artifactId>
  48. <configuration>
  49. <source>${jdk.version}</source>
  50. <target>${jdk.version}</target>
  51. <encoding>UTF-8</encoding>
  52. </configuration>
  53. </plugin>
  54.  
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. <executions>
  59. <execution>
  60. <goals>
  61. <goal>repackage</goal>
  62. </goals>
  63. </execution>
  64. </executions>
  65. </plugin>
  66. </plugins>
  67. </build>
  68. </project>

3.   application.yml

  1. server:
  2. port: 9999

4.   HystrixDashboardStarter.java

  1. package com.java.hystrix.dashboard;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.builder.SpringApplicationBuilder;
  6. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  7. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  8.  
  9. @SpringBootApplication
  10. @EnableHystrixDashboard
  11. public class HystrixDashboardStarter extends SpringBootServletInitializer {
  12.  
  13. public static void main(String[] args) {
  14. SpringApplication.run(HystrixDashboardStarter.class, args);
  15. }
  16.  
  17. @Override
  18. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  19. return builder.sources(HystrixDashboardStarter.class);
  20. }
  21. }

5.  运行 HystrixDashboardStarter.java

浏览器打开URL

http://192.168.1.102:9999/hystrix/

http://127.0.0.1:9999/hystrix/

截图如下:

Hystrix Dashboard搭建成功!

二、搭建 Hystrix 

1.   新建 Maven 项目  hystrix

2.  pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.  
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.java</groupId>
  8. <artifactId>hystrix</artifactId>
  9. <version>1.0.0-SNAPSHOT</version>
  10. <name>${project.artifactId}</name>
  11.  
  12. <!-- 配置版本常量 -->
  13. <properties>
  14. <jdk.version>1.8</jdk.version>
  15. </properties>
  16.  
  17. <parent>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-parent</artifactId>
  20. <version>2.0.5.RELEASE</version>
  21. </parent>
  22.  
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  31. <version>2.0.0.RELEASE</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  36. <version>2.0.0.RELEASE</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-actuator</artifactId>
  41. </dependency>
  42.  
  43. <!-- 热部署 -->
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>springloaded</artifactId>
  47. <version>1.2.8.RELEASE</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-devtools</artifactId>
  52. </dependency>
  53.  
  54. </dependencies>
  55.  
  56. <build>
  57. <finalName>${project.artifactId}</finalName>
  58. <plugins>
  59. <plugin>
  60. <groupId>org.apache.maven.plugins</groupId>
  61. <artifactId>maven-compiler-plugin</artifactId>
  62. <configuration>
  63. <source>${jdk.version}</source>
  64. <target>${jdk.version}</target>
  65. <encoding>UTF-8</encoding>
  66. </configuration>
  67. </plugin>
  68.  
  69. <plugin>
  70. <groupId>org.springframework.boot</groupId>
  71. <artifactId>spring-boot-maven-plugin</artifactId>
  72. <executions>
  73. <execution>
  74. <goals>
  75. <goal>repackage</goal>
  76. </goals>
  77. </execution>
  78. </executions>
  79. </plugin>
  80.  
  81. <plugin>
  82. <groupId>org.apache.maven.plugins</groupId>
  83. <artifactId>maven-resources-plugin</artifactId>
  84. <configuration>
  85. <delimiters>
  86. <delimit>$</delimit>
  87. </delimiters>
  88. </configuration>
  89. </plugin>
  90. </plugins>
  91. </build>
  92. </project>

3.   application.yml

  1. server:
  2. port: 8888
  3.  
  4. spring:
  5. application:
  6. name: hystrix
  7.  
  8. eureka:
  9. client:
  10. service-url:
  11. defaultZone: http://192.168.1.102:8080/eureka
  12. instance:
  13. instance-id: hystrix.java.com
  14. prefer-ip-address: true #访问路径可以显示IP地址
  15.  
  16. info:
  17. app.name: hystrix
  18. app.update: 2018-10-07
  19. build.groupId: $project.groupId$
  20. build.artifactId: $project.artifactId$
  21. build.version: $project.version$

4.   HostController.java

  1. package com.java.hystrix.controller;
  2.  
  3. import java.net.InetAddress;
  4. import java.net.UnknownHostException;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.RestController;
  11.  
  12. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  13.  
  14. @RestController
  15. public class HostController {
  16.  
  17. @GetMapping("/getHostMessage/{id}")
  18. @HystrixCommand(fallbackMethod = "getHostMessageFallback")
  19. public Map<String, Object> getHostMessage(@PathVariable String id) {
  20.  
  21. if ("error".equals(id)) {
  22. throw new RuntimeException("测试异常演习!");
  23. }
  24.  
  25. Map<String, Object> map = new HashMap<>();
  26. try {
  27. InetAddress serverHost = InetAddress.getLocalHost();
  28. map.put("hostname", serverHost.getHostName());
  29. map.put("hostAddress", serverHost.getHostAddress());
  30. map.put("id", id);
  31.  
  32. return map;
  33. } catch (UnknownHostException e) {
  34. e.printStackTrace();
  35. map.put("msg", e.getMessage());
  36. throw new RuntimeException(e.getMessage());
  37. }
  38.  
  39. }
  40.  
  41. public Map<String, Object> getHostMessageFallback(@PathVariable String id) {
  42. Map<String, Object> map = new HashMap<>();
  43. map.put("id", id);
  44. map.put("description", "异常演习Fallback!");
  45. return map;
  46.  
  47. }
  48.  
  49. }

5.   ConfigBean.java

  1. package com.java.hystrix.config;
  2.  
  3. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
  8.  
  9. @Configuration
  10. public class ConfigBean {
  11.  
  12. @Bean
  13. public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() {
  14. HystrixMetricsStreamServlet servlet = new HystrixMetricsStreamServlet();
  15. ServletRegistrationBean<HystrixMetricsStreamServlet> bean = new ServletRegistrationBean<>(servlet);
  16. bean.addUrlMappings("/hystrix.stream");
  17. bean.setName("HystrixMetricsStreamServlet");
  18. return bean;
  19. }
  20.  
  21. }

6.   HystrixStarter.java

  1. package com.java.hystrix;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.builder.SpringApplicationBuilder;
  6. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  7. import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
  8. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  9. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  10.  
  11. @EnableEurekaClient
  12. @EnableCircuitBreaker
  13. @EnableDiscoveryClient
  14. @SpringBootApplication
  15. public class HystrixStarter extends SpringBootServletInitializer {
  16.  
  17. public static void main(String[] args) {
  18. SpringApplication.run(HystrixStarter.class, args);
  19. }
  20.  
  21. @Override
  22. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  23. return builder.sources(HystrixStarter.class);
  24. }
  25.  
  26. }

7.   运行测试

启动  Eureka   服务注册中心,参考  https://www.cnblogs.com/jonban/p/eureka.html

运行 HystrixStarter.java

浏览器输入URL

http://192.168.1.102:8888/getHostMessage/hello

http://127.0.0.1:8888/getHostMessage/hello

返回数据如下:

  1. {"hostname":"F6RK2EXYAFARPPS","hostAddress":"192.168.1.102","id":"hello"

截图如下:

Hystrix 搭建成功!

三、使用 Hystrix Dashboard 监控 Hystrix 服务

在 Hystrix Dashboard 监控页面 (http://127.0.0.1:9999/hystrix/)输入Hystrix 的监控地址

http://192.168.1.102:8888/hystrix.stream

截图如下:

单击【Monitor Stream】按钮,开始监控,截图如下:

连续多次请求Hystrix 服务的URL

http://192.168.1.102:8888/getHostMessage/hello

切换到仪表盘页面观察,监控开始变化。

监控仪表盘功能正常

搭建完毕!

技术要点提炼

只要在被监控项目上加入相关依赖和配置并开启注解即可

内容如下:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  4. <version>2.0.0.RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-actuator</artifactId>
  9. <version>2.0.5.RELEASE</version>
  10. </dependency>
  1. @EnableCircuitBreaker
  2. @EnableDiscoveryClient
  1. package com.java.hystrix.config;
  2.  
  3. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
  8.  
  9. @Configuration
  10. public class ConfigBean {
  11.  
  12. @Bean
  13. public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() {
  14. HystrixMetricsStreamServlet servlet = new HystrixMetricsStreamServlet();
  15. ServletRegistrationBean<HystrixMetricsStreamServlet> bean = new ServletRegistrationBean<>(servlet);
  16. bean.addUrlMappings("/hystrix.stream");
  17. bean.setName("HystrixMetricsStreamServlet");
  18. return bean;
  19. }
  20.  
  21. }

.

Hystrix + Hystrix Dashboard搭建(Spring Cloud 2.X)的更多相关文章

  1. maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

    项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发 项目的截图: 搭建开始: 能上图 我少打字 1.首先搭建maven的聚合工程 ...

  2. 搭建spring cloud config

    很久没更新了,因为不是专职研究spring cloud,因此更新速度得看工作强度大不大,每天能抽出的时间不多,如果更新太慢了,并且有小伙伴看的话,请见谅了. Spring Cloud简介 Spring ...

  3. 以zookeeper为注册中心搭建spring cloud环境

    在spring cloud体系中,有多种手段实现注册中心,本例中采用zookeeper作为注册中心的角色.服务提供者向zookeeper注册,服务消费者从zookeeper中发现服务提供者的相关信息, ...

  4. 搭建Spring Cloud+Dubbo

    公司要测试一下zipkin是否可以跟踪全流程,项目的架构比较复杂,不要问我为什么,基本架构如下:前端门户,调用spring cloud组件,spring cloud在调用dubbo,这样一套流程.于是 ...

  5. 只需五分钟-用Maven快速搭建Spring Cloud微服务

    Maven安装手册 1.准备安装包 安装包: apache-maven-3.5.4-bin.zip  (最好JDK 1.7及以上版本) 集成包: eclipse-maven3-plugin.zip 2 ...

  6. Docker composer搭建Spring Cloud Alibaba 运行环境(二)

    " Spring Cloud Alibaba要用到的组件很多,注册中心nacos, 限流sentinel, 数据库,网关等等.由于用到的组件相对较多,部署会很繁琐,最关键的是没有资源服务器, ...

  7. 从零搭建Spring Cloud Gateway网关(一)

    新建Spring Boot项目 怎么新建Spring Boot项目这里不再具体赘述,不会的可以翻看下之前的博客或者直接百度.这里直接贴出对应的pom文件. pom依赖如下: <?xml vers ...

  8. 从零搭建Spring Cloud Gateway网关(二)—— 打印请求响应日志

    作为网关,日志记录是必不可少的功能,可以在网关出增加requestId来查询整个请求链的调用执行情况等等. 打印请求日志 打印请求日志最重要的就是打印请求参数这些东西,不过RequestBody通常情 ...

  9. 从零搭建Spring Cloud Gateway网关(三)——报文结构转换

    背景 作为网关,有些时候可能报文的结构并不符合前端或者某些服务的需求,或者因为某些原因,其他服务修改报文结构特别麻烦.或者需要修改的地方特别多,这个时候就需要走网关单独转换一次. 实现 话不多说,直接 ...

  10. spring cloud 2.x版本 Hystrix Dashboard断路器教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

随机推荐

  1. 解读人:谭亦凡,Macrophage phosphoproteome analysis reveals MINCLE-dependent and -independent mycobacterial cord factor signaling(巨噬细胞磷酸化蛋白组学分析揭示MINCLE依赖和非依赖的分支杆菌索状因子信号通路)(MCP换)

    发表时间:2019年4月 IF:5.232 一. 概述: 分支杆菌索状因子TDM(trehalose-6,6’-dimycolate)能够与巨噬细胞C-型凝集素受体(CLR)MINCLE结合引起下游通 ...

  2. Java start和run启动线程的区别

    我们知道,我们通过调用线程的start方法启动一个线程,那么,我们可以直接调用run方法来启动一个线程吗? 先看下面一段代码: public class Test { public static vo ...

  3. Linux命令发送Http的get或post请求(curl和wget两种方法)

    Http请求指的是客户端向服务器的请求消息,Http请求主要分为get或post两种,在Linux系统下可以用curl和wget命令来模拟Http的请求.下面就来介绍一下Linux系统如何模拟Http ...

  4. C. Jury Marks 思维题

    http://codeforces.com/contest/831/problem/C 做的时候想不到,来了个暴力. 对于每个b[i],枚举每一个a[i],就有1个可能的情况. 然后用vector存起 ...

  5. 面试大全之JVM篇

    JVM 内存模型以及分区,需要详细到每个区放什么. JVM 分为堆区和栈区,还有方法区,初始化的对象放在堆里面,引用放在栈里面,class类信息常量池(static常量和static变量)等放在方法区 ...

  6. 将MySQL转化为mysqli

    <?php/** * Created by PhpStorm. * User: 大神 * Date: 2017/7/24 * Time: 11:29 */ header('content-typ ...

  7. electron 开发记录

    判断是否开发环境 安装 electron-is-dev npm install electron-is-dev // main.js const isDev = require('electron-i ...

  8. python_1基础学习

    2017年12月02日 20:14:48 独行侠的守望 阅读数:221 标签: python 更多个人分类: Python编辑版权声明:本文为博主原创文章,转载请注明文章链接. https://blo ...

  9. npm安装使用淘宝代理的方法(设置registry参数)

    公司防火墙问题导致 npm下载失败,安装使用cnpm不知道什么原因抽筋, 还有一个简单的办法,就是npm安装模块时,设置代理: npm install -g vue-cli --registry=ht ...

  10. 《Head First 设计模式》之单件模式

    单件模式(Singleton) ——确保一个类只有一个实例,并提供全局访问点. 有一些对象其实我们只需要一个,比如线程池.缓存.对话框.处理偏好设置和注册表的对象.日志对象.如果制造出多个实例,就会导 ...