简介

Hystrix的主要优点之一是它收集关于每个HystrixCommand的一套指标。Hystrix仪表板以有效的方式显示每个断路器的运行状况,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的断路器是否打开,请求响应时间, 请求失败率,请求超时个数等等数据。但是只使用Hystrix Dashboard的话, 你只能看到单个应用内的服务信息, 这明显不够. 我们需要一个工具能让我们汇总系统内多个服务的数据并显示到Hystrix Dashboard上, 这个工具就是Turbine,这节我们讨论一下,怎么用turbine+hystrix-dashboard监听两个消费者服务

一、监听模块microservice-consumer-movie-feign-with-hystrix断路器的运行状况

这个模块在前面博客中讨论,这里就不写怎么创建了

http://www.cnblogs.com/520playboy/p/8066618.html

二、监听模块microservice-consumer-movie-ribbon-with-hystrix1断路器的运行状况

2.1、创建模块microservice-consumer-movie-ribbon-with-hystrix1

项目结构如下:

 2.2、pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>microservice-spring-cloud</artifactId>
<groupId>com.jacky</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>microservice-consumer-movie-ribbon-with-hystrix1</artifactId>
<packaging>jar</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<executions>
<!--设置在执行maven 的install时构建镜像-->
<execution>
<id>build-image</id>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!--安装了docker的主机,并且打开了api remote接口设置-->
<dockerHost>http://192.168.6.130:5678</dockerHost>
<pushImage>true</pushImage><!--设置上传镜像到私有仓库,需要docker设置指定私有仓库地址-->
<!--镜像名称-->
<imageName>${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
<!--镜像的基础版本-->
<baseImage>java:openjdk-8-jdk-alpine</baseImage>
<!--镜像启动参数-->
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>

2.3、配置文件application.yml

spring:
application:
name: microservice-consumer-movie-ribbon-with-hystrix1
sleuth:
sampler:
percentage: 1.0
#zipkin:
#base-url: http://localhost:7788
server:
port: 8010
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://jacky:admin@peer1:8761/eureka/,http://jacky:admin@peer2:8762/eureka/,http://jacky:admin@peer3:8763/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
#security:
#oauth2:
# resource:
# id: microservice-consumer-movie-ribbon-with-hystrix1
# user-info-uri: http://localhost:9999/uaa/user
#prefer-token-info: false

2.4、实体类User.java

package com.jacky.cloud.entity;

import java.math.BigDecimal;

public class User {
private Long id; private String username; private String name; private Short age; private BigDecimal balance; public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getUsername() {
return this.username;
} public void setUsername(String username) {
this.username = username;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Short getAge() {
return this.age;
} public void setAge(Short age) {
this.age = age;
} public BigDecimal getBalance() {
return this.balance;
} public void setBalance(BigDecimal balance) {
this.balance = balance;
} }

2.5、控制层MovieController.java

package com.jacky.cloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import com.jacky.cloud.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate; @GetMapping("/movie/{id}")
@HystrixCommand(groupKey="UserGroup1", commandKey = "findUserByIdCommand1",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"),
@HystrixProperty(name = "execution.timeout.enabled", value = "false")},fallbackMethod = "findByIdFallback")
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);
} /**
* fallback方法
* @param id
* @return
*/
public User findByIdFallback(Long id) {
User user = new User();
user.setId(5L);
return user;
}
}

2.6、启动类

package com.jacky.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker //映入断路器注解
public class ConsumerMovieRibbonApplication { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(ConsumerMovieRibbonApplication.class, args);
}
}

三、创建hystrix+turbine模块(microservice-hystrix-turbine1)

项目结构如下:

3.1、pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>microservice-spring-cloud</artifactId>
<groupId>com.jacky</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>microservice-hystrix-turbine1</artifactId>
<packaging>jar</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-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-hystrix-dashboard</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<executions>
<!--设置在执行maven 的install时构建镜像-->
<execution>
<id>build-image</id>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!--安装了docker的主机,并且打开了api remote接口设置-->
<dockerHost>http://192.168.6.130:5678</dockerHost>
<pushImage>true</pushImage><!--设置上传镜像到私有仓库,需要docker设置指定私有仓库地址-->
<!--镜像名称-->
<imageName>${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
<!--镜像的基础版本-->
<baseImage>java:openjdk-8-jdk-alpine</baseImage>
<!--镜像启动参数-->
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build> </project>

3.2、配置文件application.yml

server:
port: 8031
spring:
application:
name: microservice-hystrix-turbine
eureka:
client:
serviceUrl:
defaultZone: http://jacky:admin@peer1:8761/eureka/,http://jacky:admin@peer2:8762/eureka/,http://jacky:admin@peer3:8763/eureka/
instance:
prefer-ip-address: true
turbine:
aggregator:
clusterConfig: default #指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
#配置Eureka中的serviceId列表,表明监控哪些服务
appConfig: microservice-consumer-movie-feign-with-hystrix,microservice-consumer-movie-ribbon-with-hystrix1
#1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称;
#2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;
#3. 当clusterNameExpression: metadata[‘cluster’]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC
clusterNameExpression: "'default'"

3.3、启动类

package com.jacky.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine; @EnableTurbine
@EnableHystrixDashboard
@SpringBootApplication
public class TurbineApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineApplication.class, args);
}
}

四、启动

五、测试

微服务之springCloud-docker-hystrix-dashboard-turbine(九)的更多相关文章

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

    简介 上一篇介绍了 Hystrix Dashboard 监控单体应用的例子,在生产环境中,监控的应用往往是一个集群,我们需要将每个实例的监控信息聚合起来分析,这就用到了 Turbine 工具.Turb ...

  2. 我对微服务、SpringCloud、k8s、Istio的一些杂想

    一.微服务与SOA “微服务”是一个名词,没有这个名词之前也有“微服务”,一个朗朗上口的名词能让大家产生一个认知共识,这对推动一个事务的发展挺重要的,不然你叫微服务他叫小服务的大家很难集中到一个点上. ...

  3. 微服务、SpringCloud、k8s、Istio杂谈

    一.微服务与SOA “微服务”是一个名词,没有这个名词之前也有“微服务”,一个朗朗上口的名词能让大家产生一个认知共识,这对推动一个事务的发展挺重要的,不然你叫微服务他叫小服务的大家很难集中到一个点上. ...

  4. 微服务之SpringCloud基础

    SpringCloud微服务基础 微服务架构--SpringCloud网站架构模式 单点应用/分布式系统面向于服务架构(SOA) /微服务架构web项目三层架构1.控制层2.业务逻辑层3.数据访问层传 ...

  5. 微服务和SpringCloud入门

    微服务和SpringCloud入门 微服务是什么 微服务的核心是将传统的一站式应用,根据业务拆分成一个一个的服务,彻底去耦合,每个微服务提供单个业务功能的服务,一个服务做一件事情,从技术角度看就是一种 ...

  6. F版本SpringCloud1—大白话为啥要有微服务?啥是微服务?SpringCloud为什么有那么多组件?

    前言 为什么要有微服务呢? 什么是微服务? SpringCloud 中为什么会有那么多的组件? ...... 作为SpringCloud教程的第一篇,不讲解具体的技术使用,通过一个通俗易懂的小故事,来 ...

  7. 微服务项目的docker自动化部署流程

    目录 微服务的Docker自动化部署 制作JDK1.8的Docker镜像 Docker常用命令介绍 制作image的一般流程 将本地的image上传至私人仓库 使用Maven插件实现自动化docker ...

  8. 庐山真面目之十二微服务架构基于Docker搭建Consul集群、Ocelot网关集群和IdentityServer版本实现

    庐山真面目之十二微服务架构基于Docker搭建Consul集群.Ocelot网关集群和IdentityServer版本实现 一.简介      在第七篇文章<庐山真面目之七微服务架构Consul ...

  9. Taurus.MVC 微服务框架 入门开发教程:项目部署:6、微服务应用程序Docker部署实现多开。

    系列目录: 本系列分为项目集成.项目部署.架构演进三个方向,后续会根据情况调整文章目录. 开源地址:https://github.com/cyq1162/Taurus.MVC 本系列第一篇:Tauru ...

  10. springcloud-知识点总结(三):Hystrix & Dashboard & turbine & Zuul & SpringCloud Config

    1.Hystrix断路器简介 Hystrix断路器简介 hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因 ...

随机推荐

  1. java.lang.IllegalStateException: No typehandler found for mapping XXX

    前言:今天遇到了这个问题,刚开始觉得很容易解决的,毕竟能定位到出问题的文件和对应的字段,根据以往的经验也可以判断出是字段映射类型不匹配的问题,后来找了半天还是没找到问题的根源,从网上百度,也没看到令人 ...

  2. logstash_output_mongodb插件用途及安装详解

    安装详情参见:http://mojijs.com/2017/03/222639/index.html http://www.jianshu.com/p/8516e51e105d

  3. Kafka不只是个消息系统

    作者丨 Jay Kreps Confluent 联合创始人兼 CEO Jay Kreps 发表了一篇博文,给出了 Kafka 的真正定位——它不只是个消息系统,它还是个存储系统,而它的终极目标是要让流 ...

  4. android 一步一步教你集成tinker(热修复)

    这几天闲着没事,就看了下现在比较火的热修复,确实有了热修复就解决了android native的一个很尴尬问题,之前比起h5,android在用户体验上是有优势,但是过于复杂的版本更新,使用户烦不胜烦 ...

  5. SQL SERVER数据库维护与重建索引

    第一步:查看是否需要维护,查看扫描密度/Scan Density是否为100% declare @table_id int set @table_id=object_id('表名') dbcc sho ...

  6. Mysql:MyIsam和InnoDB的区别

    MyISAM: 这个是默认类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的 顺序访问方法) 的缩写,它是存储记录和文件的标准方法 ...

  7. 有关 WCF 的一些错误处理

    错误消息: System.ServiceModel.EndpointNotFoundException: 无法调度消息,因为终结点地址"net.tcp://localhost/xxx.svc ...

  8. RecyclerView 与 ItemTouchHelper 实现拖拽效果

    截图 需求 App 开发新的需求,要求 RecyclerView 实现的九宫格样式可以拖拽,松手以后变更位置,类似于手机桌面拖动 app 变更位置. 分析 经过搜索,发现 support 中带有一个类 ...

  9. 微信小程序调用蓝牙功能控制车位锁

    第一次学用微信小程序,项目需要,被逼着研究了一下,功能是调用微信小程序的蓝牙功能,连接上智能车位锁,控制升降,大概步骤及调用的小程序接口API如下: 1.打开蓝牙模块 wx.openBluetooth ...

  10. C和指针第七章第五题

    实现一个简化的printf函数,能够处理%d,%f,%s,%c等格式. /*************************************************************** ...