Hystrix集羣監控Turbine

前面Dashboard演示的僅僅是單機服務監控,但實際項目基本都是集羣,集羣監控用的是Turbine,Turbine是基於Dashboard的

先搞個集羣,在microservice-student-provider-hystrix-1004項目的基礎上再搞一個microservice-student-provider-hystrix項目,將代碼和配置都複製一份,然後修改幾個地方(這裏用idea採用二合一形式,用一個工程啓動兩個不同的application.yml文件配置,加上之前的1004項目,就有三個項目可以搞集羣了

1、microservice-student-provider-hystrix項目的yml配置如下:

---
server:
  port: 1005
  context-path: /
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demosite1?useUnicode=true&characterEncoding=utf8
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  application:
    name: microservice-student
  profiles: provider-hystrix-1005

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1005
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka2001.test.com:2001/eureka/,http://eureka2002.test.com:2002/eureka/,http://eureka2003.test.com:2003/eureka/

info:
  groupId: com.ue.microservice
  artifactId: microservice-student-provider-hystrix-1005
  version: 1.0-SNAPSHOT
  聯繫人: Tom
  電話: 123456

---
server:
  port: 1006
  context-path: /
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demosite1?useUnicode=true&characterEncoding=utf8
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  application:
    name: microservice-student
  profiles: provider-hystrix-1006

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1006
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka2001.test.com:2001/eureka/,http://eureka2002.test.com:2002/eureka/,http://eureka2003.test.com:2003/eureka/

info:
  groupId: com.ue.microservice
  artifactId: microservice-student-provider-hystrix-1006
  version: 1.0-SNAPSHOT
  聯繫人: Tom
  電話: 123456

2、microservice-student-provider-hystrix項目的啓動類配置如下:

package com.ue;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EntityScan("com.ue.entity")
@EnableJpaRepositories("com.ue.repository")
@EnableEurekaClient
@EnableCircuitBreaker
public class MicroserviceStudentProviderHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceStudentProviderHystrixApplication.class, args);
    }

}

3、新建項目microservice-student-consumer-hystrix-turbine-91,在pom.xml里加入turbine的依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>

4、microservice-student-consumer-hystrix-turbine-91項目的application.yml配置如下:

server:
  port: 91
  context-path: /
eureka:
  client:
    service-url:
      defaultZone: http://eureka2001.test.com:2001/eureka/,http://eureka2002.test.com:2002/eureka/,http://eureka2003.test.com:2003/eureka/

turbine:
  #指定要監控的應用名稱
  app-config: microservice-student
  #表示集羣的名字爲default
  clusterNameExpression: "'default'"

spring:
  application:
    name: turbine

5、在microservice-student-consumer-hystrix-turbine-91項目的啓動類加上@EnableTurbine註解:

package com.ue;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableTurbine
public class MicroserviceStudentConsumerHystrixTurbine91Application {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceStudentConsumerHystrixTurbine91Application.class, args);
    }

}

6、測試,先啓動兩個eureka,然後把帶hystrix的1004、1005、1006服務提供者都啓動;普通消費者microservice-student-consumer-80也啓動,方便測試;最後將dashboard、turbine項目啓動:

在瀏覽器訪問http://localhost:91/turbine.stream可以監控數據,會實時ping並返回data:

然後輸入http://localhost:90/hystrix進入儀表盤,輸入http://localhost:91/turbine.stream,再點那個按鈕:

然後一邊請求http://localhost/student/getInfo接口調用服務集羣,一邊看集羣監控儀表:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章