JAVA springcloud ssm b2b2c多使用者商城系統-docker-hystrix-dashboard-turbine(九)

happywawa發表於2019-01-30

簡介

Hystrix的主要優點之一是它收集關於每個HystrixCommand的一套指標。Hystrix儀表板以有效的方式顯示每個斷路器的執行狀況,通過Hystrix Dashboard我們可以在直觀地看到各Hystrix Command的斷路器是否開啟,請求響應時間, 請求失敗率,請求超時個數等等資料。Spring Cloud大型企業分散式微服務雲構建的B2B2C電子商務平臺原始碼請加企鵝求求:一零三八七七四六二六。但是隻使用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;
  }
}複製程式碼

Spring Cloud大型企業分散式微服務雲構建的B2B2C電子商務平臺原始碼請加企鵝求求:一零三八七七四六二六


相關文章