这里我用了两个生产者和两个消费者进行演示,如下图(画的不好看,凑活看看):

这里我就只讲下怎么注册到dashbord和相关的配置,提供者和消费者等代码可以去下载查看:

  1. https://github.com/fengcharly/springCloud-ribbon-turbine.git

1.hystrix的配置:

这里我将熔断器(或者称为断路器配置到了消费者端):

启动类:

pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.consumer</groupId>
  6. <artifactId>stu-consumer-feign-hytrix</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>stu-consumer</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.5.10.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencyManagement>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-dependencies</artifactId>
  27. <version>Dalston.SR5</version>
  28. <type>pom</type>
  29. <scope>import</scope>
  30. </dependency>
  31. <dependency>
  32. <groupId>io.spring.platform</groupId>
  33. <artifactId>platform-bom</artifactId>
  34. <version>Brussels-SR9</version>
  35. <type>pom</type>
  36. <scope>import</scope>
  37. </dependency>
  38. </dependencies>
  39. </dependencyManagement>
  40. <dependencies>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-web</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-test</artifactId>
  48. <scope>test</scope>
  49. </dependency>
  50. <!--mybatis与mysql-->
  51. <dependency>
  52. <groupId>org.mybatis.spring.boot</groupId>
  53. <artifactId>mybatis-spring-boot-starter</artifactId>
  54. <version>1.2.0</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>mysql</groupId>
  58. <artifactId>mysql-connector-java</artifactId>
  59. </dependency>
  60. <!--druid依赖-->
  61. <dependency>
  62. <groupId>com.alibaba</groupId>
  63. <artifactId>druid</artifactId>
  64. <version>1.0.25</version>
  65. </dependency>
  66. <!--lombok-->
  67. <dependency>
  68. <groupId>org.projectlombok</groupId>
  69. <artifactId>lombok</artifactId>
  70. </dependency>
  71. <dependency>
  72. <groupId>org.springframework.security</groupId>
  73. <artifactId>spring-security-jwt</artifactId>
  74. </dependency>
  75. <!--feign-->
  76. <dependency>
  77. <groupId>org.springframework.cloud</groupId>
  78. <artifactId>spring-cloud-starter-feign</artifactId>
  79. </dependency>
  80. <!--session集群管理-->
  81. <dependency>
  82. <groupId>org.springframework.session</groupId>
  83. <artifactId>spring-session</artifactId>
  84. </dependency>
  85. <dependency>
  86. <groupId>org.springframework.session</groupId>
  87. <artifactId>spring-session-data-redis</artifactId>
  88. </dependency>
  89. <!--zipkin-->
  90. <dependency>
  91. <groupId>org.springframework.cloud</groupId>
  92. <artifactId>spring-cloud-starter-sleuth</artifactId>
  93. </dependency>
  94. <dependency>
  95. <groupId>org.springframework.cloud</groupId>
  96. <artifactId>spring-cloud-sleuth-zipkin</artifactId>
  97. </dependency>
  98. <!--eureka-->
  99. <dependency>
  100. <groupId>org.springframework.cloud</groupId>
  101. <artifactId>spring-cloud-starter-eureka</artifactId>
  102. </dependency>
  103. <dependency>
  104. <groupId>org.springframework.boot</groupId>
  105. <artifactId>spring-boot-starter-actuator</artifactId>
  106. </dependency>
  107. <!--ribbon-->
  108. <dependency>
  109. <groupId>org.springframework.cloud</groupId>
  110. <artifactId>spring-cloud-starter-ribbon</artifactId>
  111. </dependency>
  112. <!--配置huystrix-->
  113. <dependency>
  114. <groupId>org.springframework.boot</groupId>
  115. <artifactId>spring-boot-starter-web</artifactId>
  116. </dependency>
  117. <dependency>
  118. <groupId>org.springframework.cloud</groupId>
  119. <artifactId>spring-cloud-starter-eureka</artifactId>
  120. </dependency>
  121. <dependency>
  122. <groupId>org.springframework.boot</groupId>
  123. <artifactId>spring-boot-starter-actuator</artifactId>
  124. </dependency>
  125. <dependency>
  126. <groupId>org.springframework.cloud</groupId>
  127. <artifactId>spring-cloud-starter-feign</artifactId>
  128. </dependency>
  129. <dependency>
  130. <groupId>org.springframework.cloud</groupId>
  131. <artifactId>spring-cloud-starter-hystrix</artifactId>
  132. </dependency>
  133. </dependencies>
  134. <build>
  135. <plugins>
  136. <plugin>
  137. <groupId>org.springframework.boot</groupId>
  138. <artifactId>spring-boot-maven-plugin</artifactId>
  139. </plugin>
  140. </plugins>
  141. </build>
  142. </project>

StuConsumerApplication:

  1. package com.consumer.stuconsumer;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. import org.springframework.cloud.netflix.feign.EnableFeignClients;
  7. @SpringBootApplication
  8. @EnableEurekaClient
  9. @EnableFeignClients
  10. @EnableCircuitBreaker
  11. public class StuConsumerApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(StuConsumerApplication.class, args);
  14. }
  15. }

控制层:

  1. import com.consumer.stuconsumer.entity.Student;
  2. import com.consumer.stuconsumer.feign.UserFeignClient;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import javax.annotation.Resource;
  8. @RestController
  9. public class ConsumerController {
  10. @Resource
  11. private UserFeignClient userFeignClient;
  12. @RequestMapping("/getAll/{id}")
  13. public Student getAll(@PathVariable("id") Integer id) {
  14. Student stu = userFeignClient.getAll(id);
  15. return stu;
  16. }
  17. }

Feign:(这个是配置断路器的主要配置)

ConsumerFeign:

  1. import com.consumer.stuconsumer.entity.Student;
  2. import com.consumer.stuconsumer.feign.fallbackfactory.UserFallbackFactory;
  3. import org.springframework.cloud.netflix.feign.FeignClient;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. //使用FeignClient 告知发布方的应用名称 默认使用ribbon进行负载均衡
  9. @FeignClient(name = "stu-provide",fallbackFactory = UserFallbackFactory.class)
  10. public interface ConsumerFeign {
  11. @RequestMapping(value = "/getAll/{id}",method = RequestMethod.GET)
  12. public Student getAll(@PathVariable("id") Integer id);
  13. }

UserFeignWithFactory:

  1. import com.consumer.stuconsumer.feign.ConsumerFeign;
  2. //这个是hystrix的类
  3. public interface UserFeignWithFactory extends ConsumerFeign {
  4. }

UserFallbackFactory:

  1. import com.consumer.stuconsumer.entity.Student;
  2. import com.consumer.stuconsumer.feign.ConsumerFeign;
  3. import com.consumer.stuconsumer.feign.feignclientwithfactory.UserFeignWithFactory;
  4. import feign.hystrix.FallbackFactory;
  5. import org.springframework.stereotype.Component;
  6. //断路器设置
  7. //这个是hystrix的类
  8. @Component
  9. public class UserFallbackFactory implements FallbackFactory<ConsumerFeign> {
  10. @Override
  11. public ConsumerFeign create(Throwable throwable) {
  12. return new UserFeignWithFactory(){
  13. @Override
  14. public Student getAll(Integer id) {
  15. return null;
  16. }
  17. };
  18. }
  19. }

2.建立dashbord

pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com</groupId>
  6. <artifactId>stu-dashbord</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>stu-dashbord</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  22. </properties>
  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-hystrix-dashboard</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-starter-turbine</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.cloud</groupId>
  43. <artifactId>spring-cloud-netflix-turbine</artifactId>
  44. </dependency>
  45. </dependencies>
  46. <dependencyManagement>
  47. <dependencies>
  48. <dependency>
  49. <groupId>org.springframework.cloud</groupId>
  50. <artifactId>spring-cloud-dependencies</artifactId>
  51. <version>${spring-cloud.version}</version>
  52. <type>pom</type>
  53. <scope>import</scope>
  54. </dependency>
  55. </dependencies>
  56. </dependencyManagement>
  57. <build>
  58. <plugins>
  59. <plugin>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-maven-plugin</artifactId>
  62. </plugin>
  63. </plugins>
  64. </build>
  65. </project>

StuDashbordApplication:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  4. import org.springframework.cloud.netflix.turbine.EnableTurbine;
  5. @SpringBootApplication
  6. @EnableHystrixDashboard
  7. public class StuDashbordApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(StuDashbordApplication.class, args);
  10. }
  11. }

application.yml:

  1. server:
  2. port: 8030

3.启动dashbord并访问:http://localhost:8030/hystrix:

输入http://localhost:9301/hystrix.stream

然后 我们访问http://localhost:9301/getAll/1,可以在仪表盘中看到如下的信息:

这时,我们的dashbord单个应用监控完毕,但是我们在实际应用中往往不止用到一个应用,这时就需要我们来监控多个应用,这边我们可以配置turbine来进行应用的监控集群:

4.建立turbine

pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com</groupId>
  6. <artifactId>stu-turbine</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>stu-turbine</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.3.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  22. </properties>
  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-turbine</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-actuator</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. </dependencies>
  42. <dependencyManagement>
  43. <dependencies>
  44. <dependency>
  45. <groupId>org.springframework.cloud</groupId>
  46. <artifactId>spring-cloud-dependencies</artifactId>
  47. <version>${spring-cloud.version}</version>
  48. <type>pom</type>
  49. <scope>import</scope>
  50. </dependency>
  51. </dependencies>
  52. </dependencyManagement>
  53. <build>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. </plugin>
  59. </plugins>
  60. </build>
  61. </project>

StuTurbineApplication:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  4. import org.springframework.cloud.netflix.turbine.EnableTurbine;
  5. @EnableTurbine
  6. @EnableDiscoveryClient
  7. @SpringBootApplication
  8. public class StuTurbineApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(StuTurbineApplication.class, args);
  11. }
  12. }

application.yml(重点关注):

  1. server:
  2. port: 8031
  3. spring:
  4. application:
  5. name: stu-hystrix-turbine
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://user:password123@localhost:8761/eureka
  10. instance:
  11. prefer-ip-address: true
  12. turbine:
  13. aggregator:
  14. clusterConfig: default
  15. appConfig: stu-consumer-feign-hytrix,stu-consumer
  16. clusterNameExpression: "'default'"
  17. instanceUrlSuffix: /hystrix.stream
  18. #turbine.instanceUrlSuffix=/xxx/hystrix.stream

我们这边一定要配置 instanceUrlSuffix: /hystrix.stream,当然可以少一个/,这边不配置这个路径的话会报路径错误,这个是指定他路径后缀用的.

然后我们启动turbine,这时候我们在仪表盘再进行监控就可以看到多个应用的监控信息了:

配置监控的路径:http://localhost:8031/turbine.stream

仪表盘显示状态:

附上代码地址:https://github.com/fengcharly/springCloud-ribbon-turbine.git

feign中的hytrix和turbin配置的更多相关文章

  1. Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题【转】

    Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题 http://blog.didispace.com/Spring-Boot-And-Feign- ...

  2. SpringCloud之Feign声明式调用原理及配置

    1 什么是Feign Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. ...

  3. C#开发中使用配置文件对象简化配置的本地保存

    C#开发中使用配置文件对象简化配置的本地保存 0x00 起因 程序的核心是数据和逻辑,开发过程中免不了要对操作的数据进行设置,而有些数据在程序执行过程中被用户或程序做出的修改是应该保存下来的,这样程序 ...

  4. CentOS7中防火墙的一些常用配置

    # 启动 systemctl start firewalld # 查看状态 systemctl status firewalld # 停止关闭 systemctl disable firewalld ...

  5. Xcode8中处理打印日志的配置

    Xcode8中处理打印日志的配置

  6. SQL Server 2008 安装过程中遇到“性能计数器注册表配置单元一致性”检查失败 问题的解决方法

    操作步骤: 1. 在 Microsoft Windows 2003 或 Windows XP 桌面上,依次单击"开始"."运行",然后在"打开&quo ...

  7. Linux中环境变量文件及配置

    Linux中环境变量文件及配置   一.环境变量文件介绍 转自:http://blog.csdn.net/cscmaker/article/details/7261921 Linux中环境变量包括系统 ...

  8. Zend Studio 中导出 PHP 语法颜色配置

    Zend Studio 中,虽然可以自行配置 PHP 语法颜色,但是,没有导出配置的按钮.介个,总不能每次都配置一次吧,那不是累死伦家啦?有图有真相: 强迫症患者总是无法停止折腾,虽然内心总有个声音不 ...

  9. MyEclipse/Eclipse中XML文件的格式化配置

    Eclipse中XML文件的格式化配置 MyEclipse: 这一步的配置是使格式化的效果为控件的每个属性配置占一行.进入 Window/Preferences,展开到 XML/XML Resourc ...

随机推荐

  1. (字符串 KMP)Blue Jeans -- POJ -- 3080:

    链接: http://poj.org/problem?id=3080 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...

  2. MFC中的乱起八糟----字符编码:LPTSTR,LPCTSTR, TCHAR等

    注意,编写有 Unicode 意识的代码总是一件好事,比如: CString graycat = CString(_T("Gray")) + _T("Cat") ...

  3. Python WebDriver 文件上传(二)

    今天补充一种文件上传的方法 主要是因为工作中使用SendKeys方法不稳定,具体方法见: Python WebDriver 文件上传(一) 这种方法直接通过命令行执行脚本时没有问题,可以成功上传,但是 ...

  4. Ansible运维自动化工具

    1>Ansible 1>ansible简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabri ...

  5. Give $20/month and provide 480 hours of free education

    Hi , Hope all is well. Summer is right around the corner, and the Khan Academy team is excited to sp ...

  6. ffmpeg学习(二) 通过rtsp获取H264裸流并保存到mp4文件

    本篇将使用上节http://www.cnblogs.com/wenjingu/p/3977015.html中编译好的库文件通过rtsp获取网络上的h264裸流并保存到mp4文件中. 1.VS2010建 ...

  7. Spring学习(六)——集成memcached客户端

    memcached是高性能的分布式内存缓存服务器.许多Web应用都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示. 但随着数据量的增大.访问的集中,就会出现RDBMS的负担加重.数据 ...

  8. linux查看占用内存多的进程

    update一个简单的方法 ps aux | sort -k4nr | head -10 ps -e  -o "%C  : %p : %z : %a"|sort -k5 -nr|h ...

  9. 目前主流的四大浏览器内核Trident、Gecko、WebKit以及Presto

    “浏览器内核”主要指渲染引擎(Rendering Engine),负责解析网页语法(如HTML.JavaScript)并渲染.展示网页.因此,所谓的浏览器内核通常也就是指浏览器所采用的渲染引擎,渲染引 ...

  10. CF79D Password

    题目链接 题意:给定长度为n的0/1序列,初始值都为0.你每次可以在给定的l个长度中的\(a_i\)并将序列中长度为\(a_i\)的部分取反.使得最终状态为\(x_1\)~\(x_k\),求最少取反次 ...